0

My goal is go generate the following code when the user scrolls down:

<?php
    // get all files
        $imagesDir = 'uploads/';
    $images = glob($imagesDir . '* {jpg,jpeg,png,gif,JPG,JPEG,PNG,GIG}', GLOB_BRACE);
    $imagesCount = count($images);
    for($i = 0 ; $i < $imagesCount ; $i++) {
        // select image 
        $randomIndex = array_rand($images); // select image index
        $file_title = $randomImage = $images[$randomIndex]; // use selected image
        unset($images[$randomIndex]); // remove selected image
        // print_r($images); // you can see what left in $images
        // show image
    }   
?>

<li class="item-thumbs span3 img">
   <!-- Fancybox - Gallery Enabled - Title - Full Image -->
   <a class="hover-wrap fancybox" data-fancybox-group="gallery" 
       title="<?php echo $randomImage ?>" href="<?php echo $randomImage ?>">
   <span class="overlay-img"></span>
   <span class="overlay-img-thumb font-icon-plus"></span>
   </a>
   <!-- Thumb Image and Description -->
   <img src="<?php echo $randomImage ?>" alt="{Failed To Load Item Description}">
</li>

This code is found inside a block. I want to generate random images and have them show up in a nicely formatted area. Right now, everything works except that I need this code to be automatically generated so more show up as the user scrolls...Just like Facebook loads more posts on the wall when you scroll down. I've tried many scripts but none of them worked for me.

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Justin Miller
  • 54
  • 1
  • 9

1 Answers1

0

ou query answer is: first thing * you have to set a scroll handler in javascript that will be fired whenever user will scroll either up or down * you have to put ajax call that will fetch result for you when ever scroll handler will be called * and the last thing render your ajax response

But you have to manage in scroll handler that user is going up or down b / c you will need data whenever user will scroll down

so i am putting some code example , it uses jquery

Scroll handler

var lastScrollTop = 0;
var scrollHandler = function () {
var st = $(this).scrollTop();
var pageHeight = $(document).height();
var pageScrolled = $(window).scrollTop();
var pageView = $(window).height();
if (st > lastScrollTop) {
    // downscroll code
    if (st > lastScrollTop && (pageScrolled >= (pageHeight / 2))) {
        // calling scroll     after half page scrolled
        //  do nothing
    } else {
        loadMore(); // it will be called only in scroll down
    }
}


lastScrollTop = st;

}

put ajax call in this function

function loadMore() {
$(window).off("scroll", scrollHandler);
$.ajax({
    url: "",
    "data": '',
    success: function () {
        //  render you ajax response
        ....
        $(window).scroll(scrollHandler); // call event handle after first result render
    }

});

}

// attaching scroll event after window load

$(window).load(function () {
    $(window).scroll(scrollHandler);
});

only the remaining logic is how will you identify that how many result are loaded and wha was last result

so little logic is put an extra parameter in you last that will tell you it was las result and before calling loadMore(), read that parameter and send back to ajax call.

developerCK
  • 4,418
  • 3
  • 16
  • 35
  • I've read something that tells me exactly but it doesn't work. Either this just doesn't work or I am doing it wrong. Could you or someone implement this into my code before I go crazy? I'm having a hard time understanding how to load data, how to define what to load. The one tutorial I got to work, deletes all the images and replaces them with a string found at another php file loaded from **url.** I just want another 12 images to load bellow the last row each time a user scrolls. Nothing I'm finding explains how to achieve this very well. Thanks. – Justin Miller Jul 25 '13 at 17:09
  • okey i can help you but i need to understand exactly what do you have and what do you want to apply, as how many result you want to show etc.can we come over chat? – developerCK Jul 25 '13 at 17:15
  • Thanks. If you go here: http://192.119.43.239/ you can view the site I am working with. There is a list of thumbnail icons. These thumbnail icons will be replaced by a random image I load with . I want the site to automatically load 12 more random images (Images that have not been shown before) as the user scrolls down. When there are no more images to load without showing a duplicate image, I want a message to be show with something like, "No more content, please upload!" or something along those lines. Plus, just in case, I'd like to have a button that loads content – Justin Miller Jul 25 '13 at 17:26
  • Thank you! if you have any other questions, just let me know. – Justin Miller Jul 25 '13 at 17:26
  • I have done code for you but how can i provide you either send me mailid or tell me any way b/c I developed a demo for your problem. – developerCK Jul 26 '13 at 06:21
  • Thanks! You can email me at justin@jzapstudio.com. – Justin Miller Jul 26 '13 at 07:16