这是我从数据库获取帖子的代码。我将这里的帖子称为快照。
$getuser = mysql_query("SELECT userID FROM user WHERE username= '$user' ");
$get_row = mysql_fetch_assoc($getuser);
$userID = $get_row['userID'];
$getposts = mysql_query("SELECT * FROM snaps WHERE userID='$userID' ORDER BY snapID DESC ") or die(mysql_error());
while ($row = mysql_fetch_assoc($getposts)) {
$title = $row['title'];
$date_added = $row['date_added'];
$time = $row['time'];
$photo = "userdata/user_snaps/".$row['photos'];
我用于显示快照的 html 的格式为每行 3 个快照。
<!-- Projects Row -->
echo ("
<div class="row">
<div class="col-md-4 portfolio-item">
<div class="thumbnail">
<a href=''>
<img src="$photo" class="portrait" alt="Image" />
</div>
<h3>
'$title'</a>
</h3>
<p>$content</p>
</div>
<div class="col-md-4 portfolio-item">
<div class="thumbnail">
<a href=''>
<img src="$photo" class="portrait" alt="Image" />
</div>
<h3>
'$title'</a>
</h3>
<p>'$content'</p>
</div>
<div class="col-md-4 portfolio-item">
<div class="thumbnail">
<a href="">
<img src="$photo" class="portrait" alt="Image" />
</div>
<h3>
'$title'</a>
</h3>
<p>$content</p>
</div>
</div>
");
如何以每行三个帖子的方式显示它?就像 Instagram 一样。
最佳答案
通过在 while 循环中使用计数器。
$counter = 0;
while ($row = mysql_fetch_assoc($getposts)) {
$counter++;
if($counter > 3) { //if your counter goes to 4 then it will add a new line in your html
$counter = 0;
echo '<div class="row"></div>';
}
echo '<div class="col-md-4 portfolio-item">
<div class="thumbnail">
<a href="">
<img src="$photo" class="portrait" alt="Image" />
</div>
<h3>
$title</a>
</h3>
<p>$content</p>
</div>';
}
关于php - 按组显示要配置的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28711847/