Grow your CSS skills. Land your dream job.

Autoupdate wall using Ajax?

  • # February 22, 2012 at 7:13 am

    So I’m using a while statement to line every wall post in a table. Now I want to refresh this every 10 seconds using ajax, possibly setInterval.
    Here is my wall script:


    < ?php
    $limit = $_GET;
    if (empty($limit)){
    $limit = "20";
    }
    $wallquery = mysql_query("SELECT * FROM users_wall
    WHERE id = '$pageidz' ORDER BY unique_id DESC LIMIT 0, $limit");
    while ($row = mysql_fetch_assoc($wallquery)){
    $wname = $row;
    $comment = $row;
    $widz = $row;
    $uniz = $row;
    $isreply = $row;
    $commentlimit = substr($comment, 0, 100). "...Full Post";
    $wid = $row;
    $wimg2 = $row;
    $timestamp1 = $row;
    $timestamp2 = substr($timestamp1, 5, -3);
    $timestamp3 = str_replace("-", ".", $timestamp2);
    $timestampf = substr($timestamp1, 0, 10);
    $timestampc = substr($timestamp3, 6, 11);
    $timestamp4 = "" .$timestampc. " " .$timestampf. "";
    $timestamp = str_replace("-", ".", $timestamp4);




    $wallimg = mysql_query("SELECT * FROM users
    WHERE id = '$wid'");
    while ($row = mysql_fetch_assoc($wallimg)){
    $wimg = $row;
    }

    ?>


    ">




    ">
    < ?php echo($wname); ?>

    < ?php
    // Ignore all of this, CSS-Tricks! Just some user validation things like delete and reply

    if($username == $wname || $username == $uname){ ?> |

    < ?php } ?>< ?php if($username && $password){ ?> |
    < ?php } ?>< ?php if($isreply > 0){ ?> | " style="color:#aaa;font-size:12px;">Replies (< ?php echo($isreply); ?>) < ?php } ?> | < ?php echo($timestamp); ?>


    < ?php if (strlen($comment) > 100){ echo($commentlimit); }else{ echo($comment); } ?>



    < ?php
    }
    ?>

    Any ideas? :)

    - Thanks :)

    # February 22, 2012 at 5:45 pm

    No one :/?

    # February 22, 2012 at 9:41 pm

    you need to give the table an id so you can refer to it and then put that inside another div that will function as the container
    and then use the jquery load() function to load the page via ajax and insert just the table into the div


    function refreshPosts(){
    $('#container').load('posts.php #table');
    }

    setInterval('refreshPosts()',10000);
    # February 22, 2012 at 9:55 pm

    So that would be




    < ?php
    [...]
    ?>
    [...]
    < ?php
    }
    ?>



    Also the posts.php, will it just include the php or what?

    # February 22, 2012 at 10:16 pm

    posts.php could just be the original page – your index.php if that is what it is called, it can reload itself effectively as it only pulls out the bits it needs

    I would normally however create a stripped down version for ajax purposes so that it loads faster. A thought would be to then include that posts.php from your original file so you only have one copy of the code to maintain.

    # February 22, 2012 at 10:30 pm

    Ok.. So the page is profile.php, and I put the table in a div with the id of “wallcontainer” as well as the table id is “wallcont”. The code is as following:


    function refreshPosts(){
    $('#wallcontainer').load('profile.php?id=< ?php echo($id); ?> #wallcont');
    }

    setInterval('refreshPosts()',10000);

    I have jQuery loaded and everything, and this is inside the $(document).ready(function(){ — Now what am I doing wrong? It doesn’t seem to update.

    # February 22, 2012 at 10:40 pm

    can you use firebug to see if the ajax is requesting the page?

    To fault find I would add a button to trigger the refreshPosts function manually, make sure that there isn’t an issue with that first

    # February 22, 2012 at 10:58 pm

    Doesn’t seem to react at all. Or well, where do I look in Firebug?

    # February 22, 2012 at 11:57 pm

    The network pane in Firebug should show you the ajax calls being made

    # February 23, 2012 at 12:15 am

    [06:14:33.085] (6:14:33.84) WalkingDomProcessor.process: Performing data capturing on: http://schart.net/profile.php?id=1#
    [06:14:33.090] (6:14:33.90) WalkingDomProcessor.process: Can’t find engine for: http://schart.net/profile.php?id=1#
    [06:14:33.097] (6:14:33.96) WalkingDomProcessor.process: Performing data capturing on: http://schart.net/profile.php?id=1#
    [06:14:33.103] (6:14:33.102) WalkingDomProcessor.process: Can’t find engine for: http://schart.net/profile.php?id=1#

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

You must be logged in to reply to this topic.

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