2

I am using CodeIgniter. In my controller I am decoding a JSON response and simply displaying the values upon loading a view. I am simply displaying all my content from a JSON array (no database used). It is completely an array from which i am rendering the data.I want to autoload the content whilst scrolling.

Example:

 $resultjson = $this->curl->simple_get("http://www.test.com/api/records.php?ak=XXXXXXXX&ts=XXXXXXXX&sig=XXXXXXXX&postano=XXXXXX&count=100");

How can I fetch 10 records when autoloaded? Just like the http://pinterest.com/ mechanism. How can I achieve this?

Are there any plugins? Does CodeIgniter already have a solution for it such that I can use classes or libraries?

Ramaraju.d
  • 1,301
  • 6
  • 26
  • 46

2 Answers2

1

You can use the jQuery scroll event, scrollTop() and the $.ajax();

Basicly catch the scroll event, see if $(window).scrollTop() is higher than <div id="auto_load"></div> (through .offset()) and run ajax once if that's the case.

div#auto_load would be places bellow loaded content, and will "jump" down once content is added.

Example:

var ajax_once = false;
$(window).bind('scroll', function() {
    if (ajax_once)
        return;

    if ($(this).scrollTop() >= $('div#auto_load').offset().top) {
        ajax_once = true;
        $.ajax({
            /* Url, dataType json etc. */
        }).done(function(data) {
            /* use the data */
            ajax_once = false;
        });
    }
});
Community
  • 1
  • 1
Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
  • Thank you @Robin. But can you please tell me where should the ajax places. Either in view or in controller class. I never used ajax concepts before.. I hope this should be included in controller am i right? the url i am decoding will have count value &count=20 – Ramaraju.d Dec 10 '12 at 13:39
  • $(window).bind('scroll', function() { alert('one'); if (ajax_once) return; if ($(this).scrollTop() >= $('div#auto_load').offset().top()) { alert('two'); ajax_once = true; $.ajax({ /* Url, dataType json etc. */ }).done(function(data) { /* use the data */ ajax_once = false; }); This condition always fails y? alert "one prints but "two doesn't"! – Ramaraju.d Dec 16 '12 at 17:49
  • do you have mentioned `
    – Robin Castlin Dec 16 '12 at 21:40
  • Yes i mentioned auto_load div. but it doesn't print alert named two – Ramaraju.d Dec 17 '12 at 05:55
  • Then troubleshot `$(this).scrollTop()` and `$('div#auto_load').offset().top()` till you know what's up. – Robin Castlin Dec 17 '12 at 09:01
-1

Codeigniter is a php framework which means server side therefore no client side logic built in.

You can use a jQuery plugin for that called infinite scroll. Here is the link to it: http://www.infinite-scroll.com/

Lenin
  • 570
  • 16
  • 36
Udan
  • 5,429
  • 2
  • 28
  • 34
  • Actually i gone through this http://www.quora.com/CodeIgniter/How-to-auto-load-the-page-when-scrolling. But i dont think this plugin really works fine. The documentation is not clear. Let me try again – Ramaraju.d Dec 10 '12 at 13:06
  • Interesting typo in the name of the framework... `GodeIgniter` alsmot like `GodIgniter`. :) – Maxime Morin Dec 10 '12 at 14:18