duanmiyang6201 2015-06-18 11:57
浏览 11
已采纳

如何在循环内包装第二列和第二列?

I'm trying to wrap two columns inside of a row div from inside a php loop.

I came up with this test but it fails no matter what I do.. This is the closest I can come, I can't see where this logic fails.

Take a look:

<?php $i = 0; ?>
<?php while($i < 11) : ?>

  <?php if ($i % 2 === 0) : ?>

    <div class="row">row

  <?php endif; ?>

  <span><?php echo "[" . $i . "]"; ?></span>

  <?php if (!$i % 2 === 0) : ?>

     /row
    </div>

  <?php endif; ?>

  <?php $i++; ?>
<?php endwhile; ?>

The produced result:

row [0] [1] /row
row [2] /row
[3] /row
row [4] /row
[5] /row
row [6] /row
[7] /row
row [8] /row
[9] /row
row [10] /row

Here we can see that the first row is working great but then it becomes flawed somewhere in the logic, question is where?

  • 写回答

4条回答 默认 最新

  • dse55384 2015-06-18 12:09
    关注

    Made a little changes to your code.

      <?php $i = 0; 
           while($i <= 11) :  
           if($i%2==0){
            echo '<div class="row"> Row';
           } 
             echo "[" . $i . "]"; 
    
           if($i%2!=0){
            echo " row </div>";
           }
           $i++; 
         endwhile; ?>
    

    It will output

     Row[0][1] row
     Row[2][3] row
     Row[4][5] row
     Row[6][7] row
     Row[8][9] row
     Row[10][11] row
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?