Jump to content

Displaying multiple comments


richei

Recommended Posts

Not really sure what forum this would be a good fit for, but basically, I use a combination of jquery and php to display comments.  I was going fine displaying the last comment, but now I have to display all the comments.  I tried using some code I use to write pull down boxes, but that didn't work.  I'm sure it could be done using json arrays, but I don't know how to process the results using jquery.

 

jquery code:

 

//gets comments for initial secret
   var quid = "quid=<?=$ans['quid']?>";
   $.post("inc/comments_system.php", quid, function(data) {
    if(data.number > 0) {
     $("#comm").html("<p><a href='#'>"+data.number+"</a></p>");
     $("#cbox").show();
     $("#cdisplay").show().html(data.comment);
     $("#quid").attr("value", data.quid);
    } else {
    $("#comm").html("<p><a href='#'>"+data.number+"</a></p>");
    $("#quid").attr("value", data.quid);
   }
  }, "json");

PHP code

 

$q = mysql_query("SELECT COUNT(oid) as num, comment, username, oid, createdon FROM comments WHERE oid = $_POST[quid] ORDER BY createdon") or die(mysql_error());
 
  if(mysql_num_rows($q) != 0) {
   while($ans = mysql_fetch_assoc($q)) {
    $username .= "<div style='width: 95%; margin: 0 auto'>".$ans['comment']."<br /><div style='font-style: italic; text-align: right;'> -$ans[username]@ ".date('m-d-y G:i.s', strtotime($ans['createdon']))."</div></div>";
    $num = $ans['num'];
   }
   $ret = array("number" => $num, "comment" => stripslashes($comment), "quid" => $_POST['quid']);
  } else {
   $ret = array("number" => 0, "quid" => $_POST['quid']);
  }
echo json_encode($ret);

I just need to know what to do to get this to work.

Link to comment
https://forums.phpfreaks.com/topic/289138-displaying-multiple-comments/
Share on other sites

Simplest solution?

 

When before you were returning JSON for a single comment, now you return an array of the exact same stuff for each comment.

 

When before you were dealing with data as representing a single comment, now you loop over it as the array it is and inside do (roughly) the same logic you have now. But now you won't be able to use elements by ID because you'll need more than one, so first you'll have to restructure your HTML a bit to accommodate.

Well, removing the COUNT did the trick, and also realizing that i was using the wrong variable, had $username instead of $comment.  Once i made those changes, it started working.  Thanks again for the quick help :)

 

For some reason i can't mark Barand's suggestion as what solved it.

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.