我有一个要循环的项目(客户端)列表,但我希望每 5 个都在一个<li>
标签中,如果最后一个标签少于 5 个(例如 2 个),它会自动关闭。
我做了以下代码,它有效,但我该如何优化它,尤其是最后一部分?
foreach ($clients AS $k=>$client){
$liopen ='';
if ($k % 5 == 0){
echo "<li>\n";
$liopen = true; // we have an opened an li tag
}
echo '<div class="item" style="background:url('.$client['image'].') center center no-repeat;"></div>'."\n";
if ( $k == 4 || $k == 9 || $k == 14 || $k == 19 || $k == 24 || $k == 29 || $k == 34 || $k == 39 || $k == 44 || $k == 49 || $k == 54 || $k == 59 || $k == 64 || $k == 69 ){
echo "</li>\n";
$liopen = false;
}
}
if ($liopen == true) { // if an li tag is still open , close it
echo "</li>\n";
$liopen = false;
}