这是我从数据库中获取帖子的代码。我把我的帖子称为快照。
$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一样。
答案 0 :(得分:0)
在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>';
}