[英]Show Posts from Only One User
你好,我的数据库中有这张表,叫做帖子:
1-id 2-poster 3-Title 4-date 5-hour 6-imagem 7-desc
然后我创建了一个帖子:
id - 1张海报- Gary Title - 机器学习的概念是什么? 日期- 2021/05/19 小时 - 下午 4:32 imagem -https://www.iberdrola.com/wcorp/gc/prod/pt_BR/comunicacion/machine_learning_mult_1_res/machine_learning_746x419.jpg desc -机器学习(英语,机器学习) 是一种自动构建分析模型的数据分析方法。 它是人工智能的一个分支,其理念是系统可以从数据中学习、识别模式并在最少的人工干预下做出决策。
然后我创建了一个名为Index.html的 php 文件,它在 Pdo 中进行编码:
<?php
include_once 'con.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<?php
$result_msg_cont = "SELECT * FROM posts ORDER BY id Desc";
$resultado_msg_cont = $conn->prepare($result_msg_cont);
$resultado_msg_cont->execute();
while ($row_msg_cont = $resultado_msg_cont->fetch(PDO::FETCH_ASSOC)) {
$post_id = $row_msg_cont['poster'];
$Post_Title = $row_msg_cont['Title'];
$data = $row_msg_cont['date'];
$hora = $row_msg_cont['hour'];
$desc = $row_msg_cont['desc'];
$imagem = $row_msg_cont['imagem'];
echo "<br><p> Posted in " . $data . " at " . $hora."</p><br>";
echo "<h2> $Post_Title</h2><br>";
echo "<img src='" .$imagem. "' class='img_posts'><br>";
echo " <br><h4 class='posts_desc'> " . $desc . "</h4><br>";
echo "poster: " . $post_id. "<br><br><hr>";
}
?>
</body>
</html>
结果很棒,因为它确实显示了帖子,但我只想显示一个示例用户的帖子:
我只想显示 Gary 的帖子,以防万一另一个用户名为 Fred
有什么办法吗?
简短的回答:
您将需要在查询中使用 where 子句。
例如
$result_msg_cont = "SELECT * FROM posts where poster= 'Gary' ORDER BY id Desc";
长答案:
在您的帖子表中添加 poster_unique_id 列。
再创建一个名为 users 的数据库表,其中包含 id、unique_id、name、user_status、showhide 等列。 为每个海报详细信息的 unique_id 列使用唯一的随机字符串。
现在显示用户(海报)的名称,并带有 GET 值的链接。
例如
<a href="index.php?poster=<?php echo $userdata['unique_id'];?>"> <?php echo $userdata['name']:?></a>
// HERE USER NAME AND UNIQUEID IS FETCHED FROM users TABLE
然后在您上面的当前代码中,添加
$poster = $_GET['poster'];
// then use query like
$result_msg_cont = "SELECT * FROM posts where poster_unique_id = '$poster' ORDER BY id Desc";
** 此示例中未考虑数据清理等。
是 index.php 还是 index.html?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.