elviapw Posted March 2, 2009 Share Posted March 2, 2009 I'm trying to make it possible for users to comment on other users' posts on a website. Both the comments and the posts are stored in a mysql database (the images are linked to a server). I've managed to get the page to display ONE comment linked with the correct post, but when more than one comment is made on the same post, the ENTIRE post displays itself twice. In fact, EVERY post displays itself twice. How can I make this work better? Here's the code: <?php $dbh = mysqli_connect("--", "--", "--", "--"); $sql = "SELECT Title,Content,Date, image_url, image_url2, image_url3, id, comment, comment_id, person FROM Posts, Comments ORDER BY id DESC"; $query = mysqli_query($dbh,$sql); echo "<html> <head></head><title>velcome</title> <body> if($query) { $rows = mysqli_num_rows($query); for($i = 0; $i < $rows; $i++) { $feed = mysqli_fetch_array($query); $title = $feed['Title']; $post = $feed['Content']; $date = $feed['Date']; $id = $feed['id']; $pic = $feed['image_url']; $pic2 = $feed['image_url2']; $pic3 = $feed['image_url3']; echo "<tr >\n"; echo "<td >\n"; echo "<p>$date</p><br/>"; echo " <p>$post</p><br/>\n"; if ( !empty($feed['image_url']) ) { echo "<img src= '$pic'/><br/><br/>"; } else { echo " "; } if ( !empty($feed['image_url2']) ) { echo "<img src= '$pic2'/><br/><br/>"; } else { echo " "; } if ( !empty($feed['image_url3']) ) { echo "<img src= '$pic3'/><br/><br/>"; } else { echo " "; } echo " <a href='delete.php?id=$id&Title=$title&Content=$post&Date=$date&image_url=$pic&image_url2=$pic2&image_url3=$pic3'>delete</a>\n"; echo " <a href='comment1.php?id=$id&Title=$title&Content=$post&Date=$date&image_url=$pic&image_url2=$pic2&image_url3=$pic3'>comment on this post</a>\n"; $comment = $feed['comment']; $person = $feed['person']; $commentid = $feed['comment_id']; if ( ($commentid == $id) ) { echo "comments <br/> $person <br/> $comment"; } else { echo " "; } echo "<hr />\n"; echo "</td >\n"; echo "</tr>\n"; } echo "</table>\n"; } echo"</body> </html>" ?> Link to comment https://forums.phpfreaks.com/topic/147514-php-comments-on-posts/ Share on other sites More sharing options...
Silverado_NL Posted March 2, 2009 Share Posted March 2, 2009 try $feed = mysqli_fetch_assoc($query); instead of $feed = mysqli_fetch_array($query); Link to comment https://forums.phpfreaks.com/topic/147514-php-comments-on-posts/#findComment-774355 Share on other sites More sharing options...
trq Posted March 2, 2009 Share Posted March 2, 2009 That code shouldn't (and in fact won't) work at all it is riddled with parse errors. Can you post the actual code you are using? Link to comment https://forums.phpfreaks.com/topic/147514-php-comments-on-posts/#findComment-774357 Share on other sites More sharing options...
elviapw Posted March 2, 2009 Author Share Posted March 2, 2009 yes, of course. i'm so sorry -- i tried to take out the html formatting because it is very clumsy and i figured it would just add to the confusion. but here's the whole ugly thing: <?php $dbh = mysqli_connect("--", "--", "--", "--"); $sql = "SELECT Title,Content,Date, image_url, image_url2, image_url3, id, comment, comment_id, person FROM Posts, Comments ORDER BY id DESC"; $query = mysqli_query($dbh,$sql); echo "<html> <head></head><title>velcome</title> <body><center> <font style='color:#00FFFF;font-family:futura,helvetica; font-size:240px'> EATERS DIGEST </font></center> <div style='background: white; border-width: medium; font-family:times; font-size:16px; float: left; position: absolute; top: 615px; width:80px'> <br/><br/><br/><br/> <a style='text-decoration:none' href='http://eadersdigest.com/projecks/projecks.html'><font color='#99CC00'><i>projecks</i></font></a><br/><br/> <a style='text-decoration:none' href='http://eadersdigest.com/members.html'><font color='#99CC00'><i>digestion</i></font></a> <br/><br/><br/> frendz >><br/> <a style='text-decoration:none' href='http://eadersdigest.com/login.html'><font color='#FF6600'><i>LAWG IN</i></font></a> <br/><br/> </div> <table style='border-style:solid; margin-left: auto; margin-right: auto; margin-top: 40px; width:800px' bordercolor='#999999' cellpadding='14'>"; if($query) { $rows = mysqli_num_rows($query); for($i = 0; $i < $rows; $i++) { $feed = mysqli_fetch_array($query); $title = $feed['Title']; $post = $feed['Content']; $date = $feed['Date']; $id = $feed['id']; $pic = $feed['image_url']; $pic2 = $feed['image_url2']; $pic3 = $feed['image_url3']; echo "<tr >\n"; echo "<td >\n"; echo "<a href=\'archive.php'><font face='Georgia, Times New Roman, Times, serif' color='#999999'>$title $id</font></a>\n"; echo "<p>$date</p><br/>"; echo " <p>$post</p><br/>\n"; if ( !empty($feed['image_url']) ) { echo "<img src= '$pic'/><br/><br/>"; } else { echo " "; } if ( !empty($feed['image_url2']) ) { echo "<img src= '$pic2'/><br/><br/>"; } else { echo " "; } if ( !empty($feed['image_url3']) ) { echo "<img src= '$pic3'/><br/><br/>"; } else { echo " "; } echo " <a href='delete.php?id=$id&Title=$title&Content=$post&Date=$date&image_url=$pic&image_url2=$pic2&image_url3=$pic3'>delete</a>\n"; echo " <a href='comment1.php?id=$id&Title=$title&Content=$post&Date=$date&image_url=$pic&image_url2=$pic2&image_url3=$pic3'>comment on this post</a>\n"; $comment = $feed['comment']; $person = $feed['person']; $commentid = $feed['comment_id']; if ( ($commentid == $id) ) { echo "comments <br/> $person <br/> $comment"; } else { echo " "; } echo "<hr />\n"; echo "</td >\n"; echo "</tr>\n"; } echo "</table>\n"; echo "</center>"; echo"<a style='text-decoration:none' href='http://eadersdigest.com/archive.php'><font color='#FF6600'><center>archive</center></font></a>"; } echo"</body> </html>" ?> Link to comment https://forums.phpfreaks.com/topic/147514-php-comments-on-posts/#findComment-774371 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.