Grow your CSS skills. Land your dream job.

Update PHP Automatically?

  • # February 24, 2012 at 5:43 pm

    I want to automatically update my PHP “wall” (like Facebook).
    Here is a a basic version of how my script works:



    < ?php
    $query = mysql_query("SELECT * FROM wall");
    while ($row = mysql_fetch_assoc($query)){
    $post = $row;
    $name = $row;
    ?>






    < ?php
    }
    ?>

    < ?php echo($name); ?>

    < ?php echo($post); ?>

    Any ideas how I can automatically get new information from my database and update the HTML table. Transitions are not necessary but would be cool. If you could explain and give me a example code, that would truly be awesome :)

    - Thanks!

    # February 25, 2012 at 5:13 am

    You’ll need to do a JavaScript AJAX request to get the new information, then append that to the table

    # February 25, 2012 at 7:10 am

    OK, but how?

    # February 25, 2012 at 7:28 am

    give each of the posts a unique ID, Then use XHR to send the last ID to another PHP file which can workout from the last ID if there has been any new posts since and if there are return it to the page as JSON/XML and use JS to append/prepend it to the document

    jQuery will make this a lot easier -> http://api.jquery.com/jQuery.ajax/

    # February 26, 2012 at 3:34 pm

    Each post has a unique ID, but how would I do that? I really understand if you couldn’t be bothered explaining with examples but I think I need that to understand this probably.

    # February 26, 2012 at 7:40 pm

    are you outputting the id to the html?
    it doesnt have to be in the id field, just create you own one in the TR, lets say

     rest of the post in here

    then you can grab the last tr in the table

    var postID = $('table tr:last-child').attr('postID');

    Then post this value back to your server and use your PHP to workout what to send back

    # February 27, 2012 at 12:56 am

    Hmm, first of all, it would be first-child since it’s DESC.. And I get sending the value to the server, but not quite what I’m supposed to send back. I don’t know ajax at all you see.

    # February 27, 2012 at 2:49 am

    Maybe take a look at numbers 2 and 5 here http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".