0

I'm trying to get multiple strings of data and put them into there designated divs.

I'm building a view more comments function, so when someone clicks on view all it replaces the 2 compulsory shown comments and if there is more then replace the original two and add any more that may be on that post.

Its not working and I know for a fact its the server side php script that is the issue as Ive checked all the ajax with error reporting. I use the while loop to grab all the data against comment_streamitem and in my database there is four results. it gets these four results(Seen in firebug) but because the json_encode is in the loop i'm pretty sure this is why its not going into its div. But when I put it outside the div it only grabs 1 result.

How would I get around this?

if(isset($_GET['comment_streamitem'])){
$id=$id=mysqli_real_escape_string($mysqli,$_GET['comment_streamitem']);

$check = "select comment_id,comment_poster,comment_streamitem,comment_datetime,comment_content FROM streamdata_comments WHERE comment_streamitem='$id' order by comment_id";
$check1 = mysqli_query($mysqli,$check) or die(mysqli_error($mysqli));
    $json = array();
while($resultArr = mysqli_fetch_array($check1)){


$json[$resultArr['comment_id']]['comment_id'] = $resultArr['comment_id'];
$json[$resultArr['comment_poster']]['comment_poster'] = $resultArr['comment_poster'];
$json[$resultArr['comment_streamitem']]['comment_streamitem'] = $resultArr['comment_streamitem'];
$json[$resultArr['comment_datetime']]['comment_datetime'] = $resultArr['comment_datetime'];
$json[$resultArr['comment_content']]['comment_content'] = $resultArr['comment_content'];

$user=$resultArr['comment_poster'];
$check2= "SELECT * FROM user WHERE id='$user'";
$check22 = mysqli_query($mysqli,$check2);
$resultArr = mysqli_fetch_array($check22);
$json[$resultArr['username']]['username'] = $resultArr['username'];
$json[$resultArr['id']]['id'] = $resultArr['id'];
$json[$resultArr['first']]['first'] = $resultArr['first'];
$json[$resultArr['middle']]['middle'] = $resultArr['middle'];
$json[$resultArr['last']]['last'] = $resultArr['last'];


}
}
echo json_encode($json);

Ok with the above I now get the below response in firebug but its posting 1 comment into the div and everything is undefined. I've included the ajax call too

    {
    "1687": {"comment_id": "1687"},
    "33": {"comment_poster": "33", "id": "33"},
    "223": {"comment_streamitem": "223"},
    "2014-08-23 17:24:10": {"comment_datetime": "2014-08-23 17:24:10"},
    "ggg": {"comment_content": "ggg"},
    "luce": {"username": "luce"},
    "lucy": {"first": "lucy"},
    "": {"middle": ""},
    "ward": {"last": "ward"},
    "1688": {"comment_id": "1688"},
    "2014-08-23 17:24:13": {"comment_datetime": "2014-08-23 17:24:13"},
    "hh": {"comment_content": "hh"},
    "1689": {"comment_id": "1689"},
    "2014-08-23 17:24:15": {"comment_datetime": "2014-08-23 17:24:15"},
    "kkk": {"comment_content": "kkk"},
    "1690": {"comment_id": "1690"},
    "2014-08-23 17:24:17": {"comment_datetime": "2014-08-23 17:24:17"},
    "kk": {"comment_content": "kk"}
}

The Ajax script:

<script type="text/javascript">
$(function()
{
$(".view_comments").click(function()
{
var ID = $(this).attr("id");

$.ajax({
type: "GET",
url: 'viewmorecommentslink.php?comment_streamitem='+ ID,
dataType: 'json',
success: function(response){
$("#comment_list_"+ID).html('<div class="stream_comment" id="comment_'+response['comment_id']+'" style="margin-top:0px;">\
<table width=100%><tr><td valign=top width=30px><img class="stream_profileimage" style="border:none;padding:0px;display:inline;" border=\"0\" src=\"userimages/cropped'+response['comment_poster']+'.jpg\" onerror=this.src=\"userimages/no_profile_img.jpeg\" width=\"40\" height=\"40\" ></a><td valign=top align=left>\
<a href="/profile.php?username='+response['username']+'">'+response['first']+' '+ response['middle']+' '+response['last']+'</a> - <abbr class="timeago" title='+response['comment_datetime']+'>'+response['comment_datetime']+'</abbr>\<div class="commentholder">'+response['comment_content']+'</div><br/>\<div id="commentactivitycontainer">\
<a style="cursor:pointer;" onClick=\"deletecomment('+response['comment_id']+',comment_'+response['comment_id']+');\">Delete</a><a id="likecontext_'+response['comment_id']+'" style="cursor:pointer;" onClick=\"likestatuscomment('+response['comment_id']+',this.id);\">\
<div style="width:80px; position:relative; float:left; left:40px" id="likescommentprint'+response['comment_id']+'">Like</div></a><div style="width:80px; position:relative; float:left; left:40px" id="likescommentprint'+response['comment_id']+'"></div>\
</form><a id="dislikecontext_'+response['comment_id']+'" style="cursor:pointer;" onClick=\"dislikestatuscomment('+response['comment_id']+',this.id);\"><div style="width:90px; position:relative;top:-0px; float:left; left:200px" id="dislikescommentprint'+response['comment_id']+'">Dislike</div>\
</a><div style="width:90px; position:relative; top:-0px; float:left; left:200px" id="dislikescommentprint'+response['comment_id']+'"></div></form></div></table></div></div>');
$("#view"+ID).remove();

}   
});
return false
});
});
</script>
6
  • Actually no andrew before I protect my $id=mysqli_real_escape_string($mysqli,$_GET['comment_streamitem']); I like to make sure my scripts work. Thanks for you nonconstructive answer. Commented Aug 25, 2014 at 22:40
  • 1
    yeah? $id=$_GET['comment_streamitem']; "WHERE comment_streamitem='$id'" ok then dave Commented Aug 25, 2014 at 22:42
  • When you look at your created json, you quickly see that these keys don't make any sense. Why would you use the data as the key? What if the same data exists - the data will overwrite itself because it's using the same key. Think about which data structure you want to have, then build it. Suggestion: You probably only want to use the comment ID as the key, or maybe don't use any key at all (simple json array starting at index 0, with the comment id being inside the data of that index). Commented Aug 25, 2014 at 23:34
  • dave your json is incorrect, what you really need is an array of objects Commented Aug 25, 2014 at 23:41
  • Can you give me an example of what you both mean and how you would do it. I have no clue. Commented Aug 25, 2014 at 23:48

2 Answers 2

1

Just this simple loop should give you the array of objects

As for the user data that you are trying to merge, it is better if you can create a JOIN query.

Running SELECT query in a loop could be expensive.


$sql="
    SELECT streamdata_comments.comment_id
          ,streamdata_comments.comment_poster
          ,streamdata_comments.comment_streamitem
          ,streamdata_comments.comment_datetime
          ,streamdata_comments.comment_content 
          ,user.username
          ,user.id
          ,user.first
          ,user.middle
          ,user.last
    FROM streamdata_comments 
    INNER JOIN user
    ON streamdata_comments.comment_poster=user.id
    WHERE streamdata_comments.comment_streamitem = $id
    ORDER BY streamdata_comments.comment_id
"
;



$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
$json = array();
while($row = mysqli_fetch_array($result)){
    array_push($json, $row);
}
Sign up to request clarification or add additional context in comments.

3 Comments

The comment_poster in streamdata_comments is linked up with id in the user table.. It just then grabs all the other users data from that id.
I can tell by looking at the join query its wrong. The user table and streamdata_comments table are two separate tables and your selecting all the content from just streamdata_comments so it won't work. And also your inner joining the same table to itself.
sorry you right I had a syntax error, but I do select all fields, try again now
1

You are overwriting your results in the loop:

$json['comment_id'] = $resultArr['comment_id'];
// etc.

Should be something like:

$json[$resultArr['comment_id']]['comment_id'] = $resultArr['comment_id'];
      ^^^^^^^^^^^^^^^^^^^^^^^^ or you use a counter or something similar
// etc.

You should also use a prepared statement or escape your variables before you use them in an sql query.

3 Comments

Ok I've updated that with very little change. I've updated my question with the response in firebug and also the ajax and what its doing if you could kindly take a look jereon. Thank you
@Dave If you get more results like with the code I used, you need to access your variables like response[SOME_NUMBER][SOME_KEY]. Most likely, you would want to loop over response.
I really don't understand how to do this. I understand the concept because the 4 results will over right each other so they each need a set key to be separated but putting the code together isn't going to be easy. I'm browsing the net as we speak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.