php - 按组显示要配置的帖子

标签 php mysql

这是我从数据库获取帖子的代码。我将这里的帖子称为快照。

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

相关文章:

MySQL REGEXP 匹配逗号分隔列表中的零

带有 BLOB 列的 MySQL 5.7 批量插入

php - 如何从 Windows 访问 PHP 的 GUI 以便为 Magento 设置 cron 作业

php - fatal error : Uncaught Error: Unsupported operand types

php - 多查询计数sql

PHP 函数作为 if 语句中的参数(面向对象)

mysql - 使用聚合、子查询和分组进行查询不起作用

php - 使用 PHP 和 MySQL 存储敏感数据

php - 无法在服务器中设置 session ,在本地主机上工作

php - 无法通过 PHP 连接到 msSQL 数据库