<?php foreach ($rows as $id => $row): ?>
基本上我希望前5行的输出位于<div class = "firstcolumn">
一旦达到我的五行,我希望它为其余行生成第二个<div class = "secondcolumn">
。行输出也必须在div标签内。所以我只生成一个填充行的div
任何人都可以帮我实现这个目标吗?
答案 0 :(得分:1)
试试这个
$cnt = 1;//Counter variable
foreach ($rows as $id => $row){
if($cnt <= 5){
$var = '<div class = "firstcolumn">';
}
else if($cnt > 5){
$var = '<div class = "secondcolumn">'
}
$cnt++;
}
答案 1 :(得分:0)
根据您的编码风格,循环计数器可以解决问题。
<?php $i = 0; ?>
<?php foreach ($rows as $id => $row): ?>
<?php $i++; ?>
<?php $colName = $i > 5 ? "secondcolumn" : "firstcolumn"; ?>
<div class="<?php echo $colName; ?>">
<?php endforeach; ?>
答案 2 :(得分:0)
$count = 1;
foreach ($rows as $id => $row)
{
if($count <= 5)
{
if($count==1){
?>
<div class = "firstcolumn">
<?php }?>
<?php print $row; ?>
<?php
}
else
{
if($count==6){
?>
<div class = "secondcolumn">
<?php }?>
<?php print $row; ?>
<?php
}
if($count == 5){
?>
</div>
<?php
}
$count++;
}
if($count != 1 && $count!=6){
?>
</div>
<?php
}