I am trying to loop through data to make a chat system. I have made a php function:
function getLatestMessageTime() {
$handler = new PDO('mysql:host=localhost;dbname=*****', '******', '*******');
// Set the query \\
$query = $handler->query('SELECT `time` FROM `messages`');
// Loop through the data \\
$latestTime = 0;
while ($row = $query->fetch()) {
if ($row['time'] > $latestTime) {
$latestTime = $row['time'];
};
};
// Return the latest message time \\
Return $latestTime;
}
And I set my looping jQuery code:
var latestTime = <?php echo getLatestMessageTime(); ?>;
latestTimeLoop();
function latestTimeLoop() {
if (latestTime < <?php echo getLatestMessageTime(); ?>) {
$("#container").load('ajaxLoad.php?time='+latestTime);
};
document.log(latestTime + " - " + <?php echo getLatestMessageTime(); ?>);
setTimeout(latestTimeLoop, 200);
}
But when I change the time in phpMyAdmin to be much higher than the rest of the data, it doesn't update in my console.logs. It seems like my query isn't occuring more than once within my function, it only grabs the data once instead of requesting it each time my javascript code loops.
Is there any way to reset the query each time to it grabs new info each loop?