richei Posted June 13, 2014 Share Posted June 13, 2014 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 13, 2014 Share Posted June 13, 2014 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. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted June 13, 2014 Solution Share Posted June 13, 2014 Remove "COUNT(oid) as num, " from your query. Using an aggregation function (COUNT) in a query without a GROUP BY clause will return only a single row. Quote Link to comment Share on other sites More sharing options...
richei Posted June 13, 2014 Author Share Posted June 13, 2014 I guess i could get that with mysql_num_rows(). If that's all it was, i'm going to scream. Quote Link to comment Share on other sites More sharing options...
richei Posted June 13, 2014 Author Share Posted June 13, 2014 (edited) 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. Edited June 13, 2014 by richei Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted June 13, 2014 Share Posted June 13, 2014 For some reason i can't mark Barand's suggestion as what solved it. Hmm...I'm not sure why it wouldn't you do that. Anyways, Barand's suggestion is not marked as the solution. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.