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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.