我已将导航菜单存储到数据库中,并希望分成3列col-md-3,就像每3个li将被包装到col-md-3中让我告诉你它是静态制作的
<div class="col-sm-3">
<ul>
<li><a href="">Careers</a></li>
<li><a href="">Services</a></li>
<li><a href="">Make a Booking</a></li>
</ul>
</div>
<div class="col-sm-3">
<ul>
<li><a href="">Find a Store</a></li>
<li><a href="">Blog</a></li>
<li><a href="">Contact us</a></li>
</ul>
</div>
<div class="col-sm-3">
<ul>
<li><a href="">Terms and Conditions</a></li>
<li><a href="">Privacy Policy</a></li>
<li><a href="">Delivery and Returns</a></li>
</ul>
</div>
所有这些导航菜单都在数据库中我如何使它们动态我尝试使用for循环但不使用它
<?php
$nav = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '');
$total = count($nav);
$i = 1;
for ($i; $i <= $total; $i++) {
$ans = $i / 3;
?>
<?php if(round($ans) == 3) {
?>
<div class="col-sm-3">
<ul>
<?php for($j = 1; $j <= $ans; $j++) { ?>
<li><a href=""><?php echo $nav[$j]; ?></a></li>
<?php } ?>
</ul>
</div>
<?php } ?>
<?php if($i == 1) { ?>
<div class="col-sm-3">
<ul>
<?php for($k = 0; $k <= $ans; $k++) { ?>
<li><a href=""><?php echo $nav[$k]; ?></a></li>
<?php } ?>
</ul>
</div>
<?php } ?>
<?php } ?>
我尝试使用自定义数组值进行简单测试,但这不符合我应该按顺序执行
答案 0 :(得分:3)
试试这个:
<?php
$nav = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '');
$total = count($nav);
$i = 0;
foreach($nav as $sub) {
if($i % 3 == 0) {
if($i != 0) { ?> </ul></div><?php }
?>
<div class="col-sm-3">
<ul>
<?php } ?>
<li><a href=""><?php echo $sub; ?></a></li>
<?php $i++;
}
?>
输出:
答案 1 :(得分:0)
您也可以使用此功能。
<?php
$nav=array("Careers","Services","Make a Booking","Find a Store","Blog","Contact us","Terms and Conditions","Privacy Policy","Delivery and Returns");
$data= array_chunk($nav,3);
for($i=0;$i<3;$i++){
echo '<div class="col-sm-3"><ul>';
foreach($data[$i] as $menu){?>
<li><a href=""><?php echo $menu; ?></a></li>
<?php }
echo '</ul></div>';
} ?>