2
<?php foreach ($rows as $id => $row): ?>

基本上我希望前 5 行的输出在里面<div class = "firstcolumn">

一旦达到我的五行,我希望它<div class = "secondcolumn">为其余的行生成第二个。行输出也必须在 div 标签内。所以我只生成一个充满行的 div

谁能帮我实现这一目标?

4

3 回答 3

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++;
}
于 2013-10-24T12:35:14.280 回答
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
}
于 2013-10-24T13:08:10.897 回答
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; ?>
于 2013-10-24T12:36:48.753 回答