views:

262

answers:

1

Hi

hope I'll be able to explain my needs clearly... :) . I have a HTML document generated by a PHP script that contains some objects stored in memcache.
The PHP script uses these objects to populate the HTML elements and create a structure like this:

//$day is an array of objects pulled from memcache
<ul id="day">
<?php foreach($day as $key => $object): ?>
        <li>
            <a href="<?php echo $object->getUrl(); ?>"><img src="<?php echo $object->getImageUrl(); ?>" alt ="bla bla bla" width="220"/></a>
            <div class="cover">
                <span class="service">this is our service: <?php echo $object->getServiceName() ?></span>
                <span class="count"><?php echo $object->count() ?> </span>
     <span class="link"><a href="http://mydomain.com?objecturl= <?php echo $object->getUrl()?></a></span>
            </div>
        </li>
        <?php endforeach; ?>
    </ul>

Basically my memcache updates every two minutes ...
Now, what I want to do is to dynamically update the contents of the HTML with the new objects from memcache using some kind of an animate() effect. I'm pretty new to jQuery and I do get the basics of it, but I couldn't find any reference for doing something like this (the nested structure the memcache and the fact I'm using objects... do make it a bit challenging..).

Any help, code samples or references will be (as always) highly appreciated :)

A: 

This is more mootools syntax, but the idea is the same, you are going to make an ajax request to fetch that html and then shove it into a div every so many seconds or on a onclick event or something.

        function updateHTML() {
            var display = $('somediv');
            var myRequest = new Request.JSON({
                url: '/getmyhtml.php',
                method: 'post',
                data: {
                    'postdata' : 'postvalue' // if you need to pass some post data
                },
                onComplete: function(response) {
                    if (response.success) {
                        display.set('html', response.data);
                        updateHTML().delay(120000) // 2 min
                    }
                }
            }).send();
        }
        window.addEvent('domready', function() {
            updateHTML();
        });

and then your getmyhtml.php would have your html in a $variable and then do

<?php 
function getHTML()
{
  $html = "my html";
  $ret = array(
     'success' => true,
     'data' => $html,
  );
  return json_encode($ret);
}
getHTML();
agentile
Thanks for that agentile, I will try to implement this method with Jquery... the down side of this is that it will update all the div contents at once since this is a list with 50 list item like the one described above i was hoping to show the update of every <li> apart hence create a more dynamic effect..
Yaniv
Than you can update the <li> instead of a div and have ajax calls to grab just those pieces and update if new.
agentile