如何以不同的方式在主页上显示div中的订阅者帖子?

问题描述 投票:-1回答:1

我有一些项目,但卡在两者之间。基本上我试图让用户在div中发帖,但我希望在一个div中我只获取我关注的一个特定用户的数据并循环此查询,以便显示我在div中关注的另一个用户的帖子。

但是我得到的结果只是我在一个div中关注的用户列表,我希望它的显示方式不同基本上应该有div的数量等于我跟随的用户数作为用户名div的标题。

查询我运行:

我有3张桌子:

=> |表| 1 |用户名|

 |ID    | USERNAME | SUBSCRIBERS |
 |------|----------|-------------|
 |1     | USER1    | 5000     
 |2.    | USER2    | 10000   |

=> |表| 2 |订阅者|

 |ID   | subscriber_from | subscriber_to  |
 |------|-----------------------------|---------------------|
 |1     | User_example     | USER1           |
 |2.    | User_example    | USER2          |
----------------------------------------------

=> |表| 3 |发布|

 |ID   | POST_CONTENT | USERNAME | VIEWS
 |------|-----------------------------|---------------------|
 |1     | This is post no.1   | USER1             |  500
 |2.    | This is post no.2   | USER2          |600
 |3     | This is post no.3   | USER2          |200
 |4     | This is post no.4   | USER1          |800

我的疑问是

$sql = " SELECT * 
FROM subscribers 
INNER JOIN post 
    ON subscribers.subscriber_to=post_username 
WHERE subscribe_from='User_example ";

我的输出:

This is post no.1 
This is post no.2
This is post no.3 
This is post no.4

但我希望它以div用户的方式安排:

现在我打算像“User_example”登录他的帐户一样。我想在div中显示他的粉丝/订阅者帖子。但是应该按照该用户的最新帖子的顺序显示一个div中的特定用户的所有帖子以及其他div中的所有其他帖子等等。

PHP代码:

<?php
$link = mysqli_connect("localhost", "root", "", "databse");
if($link === false){
    die("ERROR: Could not connect. "
                . mysqli_connect_error());
}
$sql = "SELECT * FROM subscribers INNER JOIN post ON subscribers.subscribe_to=post.username WHERE subscribe_from='user_example' ORDER BY post.views DESC";


if($res = mysqli_query($link, $sql)){

    if(mysqli_num_rows($res) > 0){   
        while($row = mysqli_fetch_array($res)){
               echo $row["post_content"].$row["username"].'<br>';
        }

        mysqli_free_result($res);
    } else{
        echo "No Matching records are found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " 
                                . mysqli_error($link);
}

mysqli_close($link);


?>
php mysql mysqli
1个回答
1
投票

ORDER BY子句在MySQL中用于对结果进行排序。只有当特定字段(或多个字段)的值发生更改时,break-sort才会输出一些代码。

<?php
$link = mysqli_connect("localhost", "root", "", "databse");
if($link === false){
    die("ERROR: Could not connect. "
                . mysqli_connect_error());
}
//  Add order by clause to sort results by username so they can be grouped by username in output.
$sql = "SELECT * FROM subscribers INNER JOIN post ON subscribers.subscribe_to = post.username WHERE subscribe_from='user_example' ORDER BY post.username;";

if($res = mysqli_query($link, $sql)){
    if(mysqli_num_rows($res) > 0){
        $lastusername = "";
        while($row = mysqli_fetch_array($res)){
            //  Break-sort. Output only when value of username changes.
            if($lastusername != $row["username"]) {
                if($lastusername != '') {
                    //  Close previous div
                    echo "</div>\n";
                }
                echo "<div class='someclass'>\n";
                $lastusername = $row["username"];
            }
            echo $row["post_content"].$row["username"].'<br>';
        }
        //  Close last div.
        echo "</div>\n";
        mysqli_free_result($res);
    } else{
        echo "No Matching records are found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " 
                                . mysqli_error($link);
}

mysqli_close($link);
?>
© www.soinside.com 2019 - 2024. All rights reserved.