简体   繁体   中英

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

then I created a post:

id - 1 poster - Gary Title - What is the concept of machine learning? 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. 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:

<?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

is there any way to do this?

Short Answer:

You will need to use where clause in query.

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.

create one more database table named users with columns like id, unique_id, name, user_status, showhide. Use unique random string for unique_id column for each poster details.

Now display names of users (posters) with link to them with GET value.

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

is it index.php or index.html??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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