0

This works:

while($row = mysql_fetch_array($results)){
    if($isfirst){
        $last_id = $row['id'];
    }
    $isfirst = 0;
    $msg_id = $row['id'];
    $message = nl2br($row['post']);
    $time = $row['post_time'];
    $name = $row['firstname']." ".$row['lastname'];
    if($row['updated']=='1'){
        $updated = "- <font style='color:#DB4937;'>CORRECTED</font>";
    }
    else{
        $updated='';
    }
    $newPosts .= "<li id='msgblk_$msg_id'><span id='message_$msg_id'>$message</span><p class='time' id='time_$msg_id'>$name - $time $updated</p></li>";
}
$newID = $last_id;

//Escape Line Breaks For Valid JSON - This function is defined in an external file.
$newPosts = parse($newPosts);
$newID = parse($newID);

//Return JSON
echo "[{";
    echo "\"newPosts\": \"".$newPosts."\",\n";
    echo "\"newID\": \"" . $newID . "\"";
echo "}]";

The above returns:

[{"newPosts": "<li id='msgblk_42'><span id='message_42'>testing 8</span><p class='time' id='time_42'>my name - Apr 16th, 2012 08:19:54 PM </p></li>",
"newID": "42"}]    

This DOESN'T work: (using json_encode)

while($row = mysql_fetch_array($results)){
    if($isfirst){
        $last_id = $row['id'];
    }
    $isfirst = 0;
    $msg_id = $row['id'];
    $message = nl2br($row['post']);
    $time = $row['post_time'];
    $name = $row['firstname']." ".$row['lastname'];
    if($row['updated']=='1'){
        $updated = "- <font style='color:#DB4937;'>CORRECTED</font>";
    }
    else{
        $updated='';
    }
    $data['newPosts'] .= "<li id='msgblk_$msg_id'><span id='message_$msg_id'>$message</span><p class='time' id='time_$msg_id'>$name - $time $updated</p></li>";
}
$data['newID'] = $last_id;


echo json_encode($data);

The above returns:

{"newPosts":"<li id='msgblk_41'><span id='message_41'>testing 7.<\/span><p class='time' id='time_41'>Dana Schuett - Apr 16th, 2012 08:10:04 PM <\/p><\/li>","newID":"41"}

And The jQuery:

$.getJSON("scripts/publicfeed2.php?last_id="+last_id,
function(data){
  $.each(data, function(i,item){
  if(item.newPosts){
    $("#updates").prepend(item.newPosts);
  }
  if(item.newID){
   $(".last_id").attr("id",item.newID);
  }
  if(item.id){
    $("#msgblk_"+item.id).fadeOut();
  }
});

The only thing I notice the the square brakets on the manually buit json... other that, i have no idea why it is not working. Any help is greatly apprceciated!

Edited by dschuett

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by dschuett
0

Hi,

The json string without square brakets is correctly because it represents a single object with key:value , not an array with multiple elements. Maybe the problem is in javascript code, but i don't know.

0

ardav, I actually did try doing that still with no avail. Which really makes things even more weird for the simple fact that by doing that, it makes the two JSON arrays identical. (besides the character escapes).

Not real big deal, becasue the first way works, but I being left with the unknown. Especially on a "built in" function.

0

I just wanted to post back and say that I actually did get this working with by doing the following:

....
$data[]['newPosts'] .= "<li id='msgblk_$msg_id'><span id='message_$msg_id'>$message</span><p class='time' id='time_$msg_id'>$name - $time $updated</p></li>";
}
$data[]['newID'] = $msg_id;
}

echo json_encode($data);

Note the empty [ ] right after $data

Hope this helps someone out!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.