Alright, so I feel like a bit of a doof here... but I had been having problems with AJAX getting data from a PHP-coded JSON page. After figuring out the right combination of PHP code and jQuery code, I now have a working product... Almost!
The problem here is getting new results to populate without repeating the existing data. I know it is because I am using the tr.append method.
This is the doof part:
I don't know the proper way of iterating the rows from the JSON page but do not repeat the results on each check, or how to 'translate' my code into that.
Here is the PHP code:
$sth = mysql_query("SELECT * FROM incidents2014 ORDER by id DESC LIMIT 5");
$rows = array();
while($r=mysql_fetch_assoc($sth)){
$rows[]=$r;
}
$b=json_encode($rows, JSON_HEX_TAG);
echo $b;
The JSON output:
[{"id":"1","date":"date1","time":"time1","city":"city1","fire":"fire1","addy":"addy1","level":"level1","desc":"desc1","ipaddress":"ip1","who":"who1","last_update":"lu1","RSS_time":"rss1","lat":"","lng":""},
{"id":"2","date":"date2","time":"time2","city":"city2","fire":"fire2","addy":"addy2","level":"level2","desc":"desc2","ipaddress":"ip2","who":"who2","last_update":"lu2","RSS_time":"rss2",lat":"","lng":""},
etc...]
The jQuery Code:
$(document).ready(function get(){
$.getJSON('jsontest1.php',
function (json){
var tr;
for (var i = 0; i < json.length; i++){
tr = $('<tr/>');
tr.append("<td id='id'>" + json[i].id + "</td>");
tr.append("<td id='date'>" + json[i].date + "</td>");
tr.append("<td id='time'>" + json[i].time + "</td>");
tr.append("<td id='city'>" + json[i].city + "</td>");
tr.append("<td id='fire'>" + json[i].fire + "</td>");
tr.append("<td id='level'>" + json[i].level + "</td>");
tr.append("<td id='desc'>" + json[i].desc + "</td>");
tr.append("<td id='addy'>" + json[i].addy + "</td>");
tr.append("<td id='who'>" + json[i].who + "</td>");
$('table').append(tr);
}
});
}
// setInterval(get,3000);
});
The result looks like it should, but the grouping of the above rows repeats itself ever 3 seconds (if uncommented).
I would appreciate any and all help!!!
Thank you!!! - Tim