<?php
$countries = array (
"US" => "United States Of America",
"GB" => "United Kingdom",
"CA" => "Canada",
"AU" => "Australia",
"AT" => "Austria",
"BE" => "Belgium",
"CN" => "China",
"DK" => "Denmark",
"EE" => "Estonia",
"FI" => "Finland",
"FR" => "France",
"DE" => "Germany",
"HK" => "Hong Kong",
"HU" => "Hungary",
"IS" => "Iceland",
"IN" => "India",
"ID" => "Indonesia",
"IE" => "Ireland",
"IT" => "Italy",
"JP" => "Japan",
"KR" => "Republic of Korea",
"KW" => "Kuwait",
"LV" => "Latvia",
"LU" => "Luxembourg",
"MY" => "Malaysia",
"MV" => "Maldives",
"MU" => "Mauritius",
"MX" => "Mexico",
"MC" => "Monaco",
"NL" => "Netherlands",
"NZ" => "New Zealand",
"NO" => "Norway",
"OM" => "Oman",
"PH" => "Philippines",
"PL" => "Poland",
"PT" => "Portugal",
"PR" => "Puerto Rico",
"QA" => "Qatar",
"RU" => "Russia",
"RS" => "Serbia",
"SG" => "Singapore",
"ZA" => "South Africa",
"ES" => "Spain",
"LK" => "Sri Lanka",
"SE" => "Sweden",
"CH" => "Switzerland",
"TW" => "Taiwan",
"TH" => "Thailand",
"AE" => "United Arab Emirates",
"VN" => "Vietnam"
);
$i = 1;
$locale ='<div class="section" style="clear:both;">';
$locale .= '<div class="grid" style="width:100px; display:inline-block;">';
foreach($countries as $code => $name) {
if($i % 10 == 0) {
$locale .= '<span class="item" locale="'.$code.'"><img src="/site/world/'.strtolower($code).'.png">'.$name.'</span>';
$locale .= '</div>';
$locale .= '<div class="grid" style="width:100px; display:inline-block;">';
} else {
$locale .= '<span class="item" locale="'.$code.'"><img src="/site/world/'.strtolower($code).'.png">'.$name.'</span>';
}
$i++;
}
$locale .='</div>';
$locale .='</div>';
echo $locale;
?>
在上面的代码中,我实际上是在每10个国家/地区名称后创建一个新列。它工作正常,但它似乎是添加一个空白的额外列,你可以在图像中看到。这是怎么回事?如何才能做到这一点?
答案 0 :(得分:2)
当您的国家/地区数量为10的倍数时,即使没有其他国家/地区,也会创建下一个div。
试试这个:只创建另一个div,如果这不是最后一个。
if ($i<count($countries)){
$locale .= '</div>';
$locale .= '<div class="grid" style="width:100px; display:inline-block;">';
}
答案 1 :(得分:2)
从0开始$ i,使用这种方法我们可以避免一些代码重复。
$i = 0;
$locale ='<div class="section" style="clear:both;">';
foreach($countries as $code => $name) {
if($i % 10 == 0) {
$locale .= '<div class="grid" style="width:100px; display:inline-block;">';
}
$locale .= '<span class="item" locale="'.$code.'"><img src="/site/world/'.strtolower($code).'.png">'.$name.'</span>';
if($i % 10 == 9) {
$locale .= '</div>';
}
$i++;
}
$locale .='</div>';
答案 2 :(得分:0)
尝试此解决方案。
<?php
$countries = array (
"US" => "United States Of America",
"GB" => "United Kingdom",
"CA" => "Canada",
"AU" => "Australia",
"AT" => "Austria",
"BE" => "Belgium",
"CN" => "China",
"DK" => "Denmark",
"EE" => "Estonia",
"FI" => "Finland",
"FR" => "France",
"DE" => "Germany",
"HK" => "Hong Kong",
"HU" => "Hungary",
"IS" => "Iceland",
"IN" => "India",
"ID" => "Indonesia",
"IE" => "Ireland",
"IT" => "Italy",
"JP" => "Japan",
"KR" => "Republic of Korea",
"KW" => "Kuwait",
"LV" => "Latvia",
"LU" => "Luxembourg",
"MY" => "Malaysia",
"MV" => "Maldives",
"MU" => "Mauritius",
"MX" => "Mexico",
"MC" => "Monaco",
"NL" => "Netherlands",
"NZ" => "New Zealand",
"NO" => "Norway",
"OM" => "Oman",
"PH" => "Philippines",
"PL" => "Poland",
"PT" => "Portugal",
"PR" => "Puerto Rico",
"QA" => "Qatar",
"RU" => "Russia",
"RS" => "Serbia",
"SG" => "Singapore",
"ZA" => "South Africa",
"ES" => "Spain",
"LK" => "Sri Lanka",
"SE" => "Sweden",
"CH" => "Switzerland",
"TW" => "Taiwan",
"TH" => "Thailand",
"AE" => "United Arab Emirates",
"VN" => "Vietnam"
);
$i = 1;
$c = count($countries);
$locale ='<div class="section" style="clear:both;">';
$locale .= '<div class="grid" style="width:100px; display:inline-block;">';
foreach($countries as $code => $name) {
if($i % 10 == 0 && $i != $c) {
$locale .= '<span class="item" locale="'.$code.'"><img src="/site/world/'.strtolower($code).'.png">'.$name.'</span>';
$locale .= '</div>';
$locale .= '<div class="grid" style="width:100px; display:inline-block;">';
} else {
$locale .= '<span class="item" locale="'.$code.'"><img src="/site/world/'.strtolower($code).'.png">'.$name.'</span>';
}
$i++;
}
$locale .='</div>';
$locale .='</div>';
echo $locale;
?>