开发者

Multiple rows with PHP foreach?

I am trying to make a table with 4 rows from a foreach-call.

My problem is, that in the result I get each ID twenty times in the same column.

I'm using this code:

<table  width="80%" border="0" cellpadding="10px">

   <?php foreach (array_chunk($items, 4) as $row) { ?>

      <?php     
         $i = 0;    
            foreach ($items as $item):      
            $class = null;      
            if ($i++ % 2 == 0) {            
            $clas开发者_运维百科s = ' class="altrow"';         
      } ?>

     <tr
        <?php echo $class;?>
     >  

  <?php foreach ($row as $item){ ?>         
     <td>
        <?php echo htmlentities ($item['Item']['id']); ?>
     </td>
  <?php } ?> 
<?php endforeach; ?>    
</tr> 
<?php } ?> 
</table>

Any idea how I could get each ID just once?


You are incrememnting $i at every $item as opposed to every $row

Is this the fix you are looking for?

Edit: Mikel has your fix, add this to fix the row bug (Typical of me to notice that first eck!)

<table  width="80%" border="0" cellpadding="10px">
<?php 
    $i = 0;
    $chunkedarray = array_chunk($items, 4);
    foreach ($chunkedarray as $row) {      
        $class = null;      
        if ($i++ % 2 == 0)           
            $class = ' class="altrow"';
        echo "<tr ".$class.">";
        foreach ($row as $item){ 
            echo "<td>";
            echo htmlentities ($item['Item']['id']); 
            echo "</td>";
        }
    echo "</tr>";
    }?>  
</table>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜