简体   繁体   English

仅显示一位用户的帖子

[英]Show Posts from Only One User

hello I have this table in my database called posts:你好,我的数据库中有这张表,叫做帖子:

1-id 2-poster 3-Title 4-date 5-hour 6-imagem 7-desc 1-id 2-poster 3-Title 4-date 5-hour 6-imagem 7-desc

then I created a post:然后我创建了一个帖子:

id - 1 poster - Gary Title - What is the concept of machine learning? id - 1张海报- Gary Title - 机器学习的概念是什么? date - 2021/05/19 hour - 4:32 PM imagem -https://www.iberdrola.com/wcorp/gc/prod/pt_BR/comunicacion/machine_learning_mult_1_res/machine_learning_746x419.jpg desc -Machine learning (in English, machine learning) is a method of data analysis that automates the construction of analytical models.日期- 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 -机器学习(英语,机器学习) 是一种自动构建分析模型的数据分析方法。 It is a branch of artificial intelligence based on the idea that systems can learn from data, identify patterns and make decisions with minimal human intervention.它是人工智能的一个分支,其理念是系统可以从数据中学习、识别模式并在最少的人工干预下做出决策。

then I created a php file called Index.html that does an encoding in Pdo:然后我创建了一个名为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>

the result was great because it really displays the Posts, but I only wanted to display the posts of just one example user:结果很棒,因为它确实显示了帖子,但我只想显示一个示例用户的帖子:

I just want to show Gary posts, if in case another user with another name example Fred if Fred posts something the post doesn't appear in index.php我只想显示 Gary 的帖子,以防万一另一个用户名为 Fred

is there any way to do this?有什么办法吗?

Short Answer:简短的回答:

You will need to use where clause in query.您将需要在查询中使用 where 子句。

eg例如

  $result_msg_cont = "SELECT * FROM posts where poster= 'Gary' ORDER BY id Desc";

Long Answer:长答案:

Add poster_unique_id column in your posts table.在您的帖子表中添加 poster_unique_id 列。

create one more database table named users with columns like id, unique_id, name, user_status, showhide.再创建一个名为 users 的数据库表,其中包含 id、unique_id、name、user_status、showhide 等列。 Use unique random string for unique_id column for each poster details.为每个海报详细信息的 unique_id 列使用唯一的随机字符串。

Now display names of users (posters) with link to them with GET value.现在显示用户(海报)的名称,并带有 GET 值的链接。

eg例如

     <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

Then in your above current code, add然后在您上面的当前代码中,添加

 $poster = $_GET['poster'];
 // then use query like

 $result_msg_cont = "SELECT * FROM posts where poster_unique_id = '$poster'  ORDER BY id Desc";

** Data sanitization etc not considered in this example. ** 此示例中未考虑数据清理等。

I didn't get your this line - then I created a php file called Index.html我没有得到你的这一行 - 然后我创建了一个名为 Index.html 的 php 文件

is it index.php or index.html??是 index.php 还是 index.html?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM