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!

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.

Member Avatar for diafol

I can't see why it doesn't work either. How about trying:

 echo '[' .  json_encode($data) . ']';

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.

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.