开发者

jquery ajax with memcache and php objects

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 :)


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜