Jump to content

PHP While Loop + AJAX


antfuentes87

Recommended Posts

So I have a simple php while loop that loops through a table and displays the data. I want to integrate it with AJAX, so every time a new entry is added to the table, it automatically displays without having to refresh the page.

 

$q = mysql_query("SELECT * FROM table");
while($f = mysql_fetch_array($q)){
echo $f['test_col'];
}

 

To be honest, I am not even sure if this is possible with AJAX (just started getting into it), but it seems like a good place to start. Any suggestions / help is much appreciated. I did Google this for awhile, but did not find anything (found things close, but nothing exactly like this).

 

Link to comment
https://forums.phpfreaks.com/topic/240024-php-while-loop-ajax/
Share on other sites

That's not exactly how you would achieve what you're looking for. Here's one way you can get the effect that you're after:

 

When the page loads have all the current entries listed output through PHP, and store the ID, for example, of the most recent entry in JavaScript. Create a PHP file that given an ID will return any entries added after that (e.g. SELECT ... FROM table WHERE id > $last_id). Then you can periodically query that page via AJAX, get any new entries and update the JavaScript variable that holds the ID of the last entry being displayed.

Link to comment
https://forums.phpfreaks.com/topic/240024-php-while-loop-ajax/#findComment-1233020
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.