在HTML表格中每行显示5个数组项目

时间:2012-02-16 10:44:42

标签: php arrays foreach

我有一系列产品,我需要每行显示5个项目。我怎么能做到,因为现在如果我做了

<?php foreach($data as $entries) : ?>
<td><?php echo $entries->name; ?>
<?php endforeach; ?>

它不起作用。我应该做一个柜台吗?

5 个答案:

答案 0 :(得分:5)

类似的东西:

$data = range(1, 17);
for($count = 0; $count < count($data);)
{
    echo "<tr>\n";
    for($i = 0; $count < count($data) && $i < 5; $count++, $i++) {
        echo "\t<td>$data[$count]</td>\n";
    }
    for(; $i < 5; $i++) {
        echo "\t<td>-</td>\n";
    }
    echo "</tr>\n";
}

输出

<tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
</tr>
<tr>
    <td>6</td>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
</tr>
<tr>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td>14</td>
    <td>15</td>
</tr>
<tr>
    <td>16</td>
    <td>17</td>
    <td>-</td>
    <td>-</td>
    <td>-</td>
</tr>

答案 1 :(得分:2)

也许更具可读性?

<table>
    <tr>
<?php 
$data = range(1, 17);
$counter = 1;

foreach($data as $value){
    if (!(($counter++ ) % 5)){
        echo "<td>$value</td></tr><tr>";
    }else{
        echo "<td>$value</td>";
    }
}
?>
</table>

Produces :

1   2   3   4   5
6   7   8   9   10
11  12  13  14  15
16  17

答案 2 :(得分:1)

希望这有帮助

echo"<table>";
        echo"<tr>";
for($i = 0; $i < count($productsArray); $i++){
    if($i % 4 ==0){
        echo"</tr><tr>"
     }

    echo "<td>$products[$i]; </td>";

}

答案 3 :(得分:0)

这是一个简单的计数器,如果布局要求发生变化,也很容易修改

<?php
//initialize a counter
$count = 0;
foreach($data as $entries){
//if it's divisible by 5 then echo a new row
if($count > 0 && $count % 5 == 0){
    echo("</tr><tr>\n");
}
$count++;
?>  

<td>
    <?php 
    // We're also creating a new cell for each item 
    echo $entries->name; ?>
</td>
<?php
}
?>

答案 4 :(得分:0)

  <div class="container-fluid">
             <?php
            $count = 1;
            foreach ($home_products as $pro_img) {
                 $parent_cat_link = base_url('category/view/'.$pro_img['category_id']);
                if ($count % 6 == 1) {
                    $class="cap1";
                    $class1="cap";
                    ?>
            <div class="row">
                <?php } 
                if($i %2 == 0){?>
                <div class="col-md-2 col-sm-4 <?php if($count >=6){ echo $class; } else{ echo 'cap'; } ?> ">
                    <div class="">
                        <a href="<?php echo $parent_cat_link; ?>"> <img src="<?php echo base_url() . 'uploads/category/' . $pro_img['image_name'] ?>" class="img-responsive" alt="logo"></a>
                    </div>
                </div><?php }else{ ?>
                <div class="col-md-2 col-sm-4 <?php if($count >=6){ echo $class1; } else{ echo 'cap1'; } ?>">
                    <div class="">
                        <a href="<?php echo $parent_cat_link; ?>"> <img src="<?php echo base_url() . 'uploads/category/' . $pro_img['image_name'] ?>" class="img-responsive" alt="logo"></a>
                    </div>
                </div><?php } $i++;
                if ($count % 6 == 0) {
                ?> </div> <?php } $count++; 
                } if ($count % 6 != 1) echo "</div>"; ?>



        </div>