Jump to content

php comments on posts


elviapw

Recommended Posts

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

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

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.