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!