php - 如何像 youtube 在主页上一样以不同的方式显示订阅者在 div 中的帖子?
问题描述
我有一些项目,但介于两者之间。基本上我试图让用户在一个 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);
?>
解决方案
MySQL 中使用 ORDER BY 子句对结果进行排序。仅当特定字段(或多个字段)的值发生更改时,中断排序才会输出一些代码。
<?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);
?>
推荐阅读
- android - 嵌套协调器布局内的 AppbarLayout
- node.js - 一旦在节点 js 中打开 Solr 连接,我们如何关闭它
- ios - 在文本字段中更新输出作为用户类型
- javascript - 具有放大或缩小时间的批量数据问题的谷歌地图聚类
- php - laravel(5.5) 集合的保存方法没有结果
- c++ - 函数返回的临时 unique_ptr 的句柄
- jquery - “错误:表单 set() 的调用与定义集不匹配(对象项,可选函数回调)”
- javascript - 是什么让函数表达式返回值?
- powershell - 使用“ñ”或重音符号连接文件时使用 PowerShell 时的稀有字符
- elasticsearch - 用于模式注册表的嵌套 json 的索引映射