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

发布于 2025-01-06 08:16:09 字数 220 浏览 0 评论 0原文

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

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

这样做是行不通的。我应该做一个柜台吗?

I have an array of products and I need to display them 5 items per row. How can I make it, because now if i do

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

it doesn't work. Should I make a counter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梨涡少年 2025-01-13 08:16:09

类似于:

$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>

Something like:

$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";
}

Output

<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>
莳間冲淡了誓言ζ 2025-01-13 08:16:09

也许更具可读性?

<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>

<代码>
产生:

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

Perhaps a bit more readable?

<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
忘东忘西忘不掉你 2025-01-13 08:16:09

希望这有帮助

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

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

}

Hope this helps

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

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

}
送舟行 2025-01-13 08:16:09

这是一个简单的计数器,如果布局要求发生变化,也可以轻松修改

<?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
}
?>

Here's a simple counter that's also easy to modify if layout requirements change

<?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
}
?>
暖树树初阳… 2025-01-13 08:16:09
  <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>
  <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文