简体   繁体   中英

How to populate unordered list with mysql records?

I am making a facebook like wallpost for a simple social network system that we are building. It works pretty well so far except for the displaying of comments. In my database, I have a table for the posts and another one for the comments. The way I displayed the comments in their specific post is that, in my comments table, I have a column named comment_id . So, example if the id of the post is 7 and the comment_id is also 7 then the comment will appear below that post with the same id. Now, my problem is that I can successfully display the comments in their respective posts but only 1 is being displayed. I wanted all the comments to be displayed in every posts they belong but I can't seem to make it work.

<?php

require_once 'dbconfig.php';



$user = $_SESSION['user'];; 

echo '<form action="post.php" method="post" class="wallpost"><input 
type="text" name="post" size="50"><input type="submit" name="wallpost" 
value="Post"></form><br/>';

$query = $con->query("SELECT * FROM statuspost LEFT JOIN comments ON 
statuspost.id=comments.comment_id group by statuspost.id DESC");

while($i = $query->fetch_object()){

    echo '<span class="user">'.$i->user.'</span>'.'<br>'.'<span 
class="post">'.$i->post.'</span>'.' <form action="editpost.php?type=post&
id='.$i->id.'" method="post"><span id="edit'.$i->id.'" class="editfield" 
style="display:none;"><input type="text" name="edit"><input   
type="submit" 
value="Edit" class="editb"><br/></span><a href="#" 
 onclick="showEdit(edit'.$i->id.');">Edit </a><a 
href="remove.php?type=post&
id='.$i->id.'" >Remove</a><a href="comment.php"> Comment</a></form>'.'<br
/>'.'<form action="comment.php?for='.$i->id.'" method="post" 
id="comment">
<input type="text" name="comment" id="comment">
    <input type="submit" value="Comment" id="commentb"></form>'.'<br
/><ul><li>'.$i->comment.'</li></ul>';


}


?>

So, example if the id of the post is 7 and the comment_id is also 7 then the comment will appear below that post with the same id. [...] but only 1 is being displayed

If you're linking the comments with the post ID, you will only get one comment per post since you can only have one unique post ID (assuming that you have the post ID as primary key).

You should have a column post in your comments table, and have that column reference the post they're commented on.

When you do your query, you can inner join the comments with the posts like so :

INNER JOIN comments ON comments.post = posts.id 

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