JSON 配列に問題があります。最初のロードでは、(SQL クエリで定義されているように) 順序が正しくありません。
したがって、主な問題は、ページの読み込み時、つまり、SQL クエリが lastPoll=null で呼び出された場合です。結果は time1 DESC ではソートされませんが、s.id ASC でソートされます。新しい結果を入力し、クエリに lastPoll を設定してクエリを実行すると、最新の結果が最初に追加されます。
奇妙な部分は、URL に正しいパラメーターを指定して push.php で未加工の JSON 応答を表示すると、応答が正しく、正しい順序で表示されることです。では、問題は解析にあるのでしょうか?
SQL クエリは次のとおりです。
$getActivityUpdates1 = mysql_query("
SELECT
s.id, u.name, u.profilePic, s.userid, s.content, s.time1, s.imageKey
FROM
status_updates s
INNER JOIN
users1 u ON u.id = s.userid
WHERE
competitionId = '$competitionId' AND s.time1 > '$lastPoll'
ORDER BY s.time1 DESC");
$results = array('items' => array());
while ($row = mysql_fetch_array($getActivityUpdates1))
{
$results['items'][] = array(
'statusid' => $row['id'],
'name' => $row['name'],
'profilePic' => $row['profilePic'],
'content' => $row['content'],
'time1' => $row['time1'],
'imageKey' => $row['imageKey'],
);
}
die(json_encode($results));
?>
これは、クエリを再実行する Javascript です。
var lastPoll = null;
function loadActivity(onDone) {
var competitionId = $("body").attr("data-competitionId");
console.log(lastPoll);
if (lastPoll == null) { // We have never polled, we want to pull everything and populate the list.
url = "push.php?competitionId=" + competitionId + "&lastPoll=1999-01-01 22:00:00";
} else { // We have polled once, send the date and time of that last poll to capture only new entries.
url = "push.php?competitionId=" + competitionId + "&lastPoll=" + lastPoll;
}
$.get(url, function(data) {
jsonData = $.parseJSON(data);
var spot = $("#activityspot");
var template = spot.find(".template");
for (var j = 0; j < jsonData.items.length; j++) {
var entryData = jsonData.items[j];
var entry = template.clone();
entry.removeClass("template");
entry.find(".message").text(entryData.statusid);
spot.prepend(entry);
}
if (onDone) {
onDone();
}
lastPoll = js_yyyy_mm_dd_hh_mm_ss();
});
}