Html 在引导程序中显示列的更好方法,而不是在每3行之后使用php创建行
对不起,问题的标题。我真的不知道如何用一句话来形容它 基本上,我想循环浏览我的每个图像并显示它们。但是,在进行前端处理时,我做到了以下几点:Html 在引导程序中显示列的更好方法,而不是在每3行之后使用php创建行,html,css,twitter-bootstrap,Html,Css,Twitter Bootstrap,对不起,问题的标题。我真的不知道如何用一句话来形容它 基本上,我想循环浏览我的每个图像并显示它们。但是,在进行前端处理时,我做到了以下几点: <div class="row"> <div class="col-sm-4"> <img .../> </div> <div class="col-sm-4"> <img .../> </div> &
<div class="row">
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
</div>
因为我的设计一行只能有3列,所以效果很好。但是现在如果我想在php中这样做,我必须检查循环的$count。如果是3的倍数显示
<div class="row">
如果是3+1的倍数,则显示
</div>
关闭该行
是否有任何方法可以使标记看起来与上面的示例相同,但使用下面的html:
<div class="row">
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
<div class="col-sm-4">
<img .../>
</div>
</div>
这只是让我的分数看起来很乱。在bootstrap中有没有更好的方法不用php就可以做到这一点
谢谢您希望在PHP循环中包含条件,以便PHP知道何时添加到行中 下面,我列出了一组未经测试的PHP代码,希望能够解释您的错误所在
<?php
$images = array(...);
$images_per_row = 3;
// Here is where we determine how the images
// should be divided into the rows.
$total_images = count($images);
$total_rows = ceil($total_images / $images_per_row);
// Loop through each `row`
for ($row=0; $row<$total_rows; $row++)
{
// Open the `row`
echo '<div class="row">';
// We have an `offset` to avoid displaying the same image
// multiple times in the loop.
$offset = $row * $images_per_row;
// Loop through each `col-sm-4` and `img`
for ($i=0; $i<$images_per_row; $i++)
{
echo '<div class="col-sm-4">';
echo "<img src='{$images[$offset + $i]}' />";
echo '</div>';
}
// Closing the `row`
echo '</div>';
}
有无数种方法可以做到这一点。如果在循环中使用适当的条件,甚至可以在单个循环中完成所有操作
关键是,你需要一种方法来告诉PHP嘿,我们需要把所有这些都分成三分之一,否则我们永远不会有任何进展
顺便说一句,PHP+HTML是一对丑陋的婚姻。除非你只有10分钟的时间,并且你正在试图帮助某人解决堆栈溢出问题,否则请不惜一切代价避免它。为什么要投反对票?你的“问题”没有意义。如果目标是删除PHP,为什么还要使用它?另外,你没有在你的问题中添加任何PHP逻辑,那么有人应该如何帮助你提供建议呢?我认为这是有意义的,人们不能先评论说这没有意义,即使对你的问题进行了编辑,我也不清楚。也许您正在寻找第n个子css选择器[?我没有否决投票。你想使用PHP动态添加行,对吗?你想每行添加3行,对吗?因此,每行有3的倍数,然后剩下2个或1个图像。如果有3个,这是一整行-你想如何表示2个或1个图像?2列,1列,还是保留3列?