Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Randomize image order and limit loaded images

    Hey guys, long story short, I tried to write a chunk of code to scan a directory and then display all found images on the webpage, kinda like an ugly collage.

    I managed to get the scanning and displaying working :

    PHP Code:
    <?php

       $files 
    glob("uploads/images/*.*");

       for (
    $i=0$i<count($files); $i++)

      {

      
    $image $files[$i];

      
      echo 
    '<img src="'.$image .'" alt="Random image" class="resize" style="max-height: 100%; max-width: 100%"/>'."";
       }

     
    ?>
    But then came the functions I could not make. There was two more things I needed to be done, which were; randomize the order of the images on each load, and to limit the amount of images loaded. I googled for a fair time looking for answers, I am sure they were and are there, but it was always either a case of me not understanding what was going on, or their solution always not being exactly the same as mine..

    Any advice at all is appreciated!

  • #2
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by catfisher75 View Post
    Hey guys, long story short, I tried to write a chunk of code to scan a directory and then display all found images on the webpage, kinda like an ugly collage.

    I managed to get the scanning and displaying working :

    PHP Code:
    <?php

       $files 
    glob("uploads/images/*.*");

       for (
    $i=0$i<count($files); $i++)

      {

      
    $image $files[$i];

      
      echo 
    '<img src="'.$image .'" alt="Random image" class="resize" style="max-height: 100%; max-width: 100%"/>'."";
       }

     
    ?>
    But then came the functions I could not make. There was two more things I needed to be done, which were; randomize the order of the images on each load, and to limit the amount of images loaded. I googled for a fair time looking for answers, I am sure they were and are there, but it was always either a case of me not understanding what was going on, or their solution always not being exactly the same as mine..

    Any advice at all is appreciated!
    I followed another tutorial to get to this now :

    PHP Code:
    <?php

    $dir 
    'uploads/images/';

    $file_display = array ('jpg''jpeg''png''gif');

    if (
    file_exists($dir) == false)

    {

          echo 
    'Directory \''$dir'\' not found';

    }

    else

    {

          
    $dir_contents scandir($dir);

          
    shuffle($dir_contents);

     

          foreach (
    $dir_contents as $file)

          {

                
    $filearray explode('.'$file);

                
    $file_type strtolower(end($filearray));

     

                if (
    $file !== '.' && $file !== '..' && in_array($file_type$file_display) == true)

                {

                      echo 
    '<img class="photo" src="'$dir'/'$file'" alt="'$file'class="resize" style="max-height: 100%; max-width: 100%" />';

                }

          }

    }

    ?>
    This has solved my random order issue I was having, perfectly. Now the only thing I can't seem to do is limit how many images can be on a page at one time...

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Posts
    459
    Thanks
    0
    Thanked 20 Times in 20 Posts
    It's called pagination and it won't work unless you shuffle the contents only 1 time then add the shuffled array to a file, session or something to maintain it while user is moving through pages.
    The only other way is to shuffle the list then add it to a javascript array, since javascript is client side the visitor will not have to reload the main page again and again just to move through the array. I think JavascriptKit has a tutorial on php image gallery and saving to javascript for paging.
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • #4
    New Coder
    Join Date
    Jul 2012
    Location
    Cape Town
    Posts
    12
    Thanks
    0
    Thanked 3 Times in 3 Posts

  • #5
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by c1lonewolf View Post
    It's called pagination and it won't work unless you shuffle the contents only 1 time then add the shuffled array to a file, session or something to maintain it while user is moving through pages.
    The only other way is to shuffle the list then add it to a javascript array, since javascript is client side the visitor will not have to reload the main page again and again just to move through the array. I think JavascriptKit has a tutorial on php image gallery and saving to javascript for paging.


    I ended up solving my problem with the following :


    PHP Code:
    <?php
                $dir 
    'uploads/images/';
                
    $file_display = array ('jpg''jpeg''png''gif');
                if (
    file_exists($dir) == false)
                {
                      echo 
    'Directory \''$dir'\' not found';
                }
                else
                {
                      
    $dir_contents scandir($dir);
                      
    shuffle($dir_contents);            
                      
    $count 1;
                      foreach (
    $dir_contents as $file)
                      {
                            
    $count $count 1;
                            if (
    $count == 36)
                            {
                                    break;
                            }else{
                                    
    $filearray explode('.'$file);
                                    
    $file_type strtolower(end($filearray));
                                    if (
    $file !== '.' && $file !== '..' && in_array($file_type$file_display) == true)
                                    {
                                          echo 
    '<img class="photo" src="  '$dir'/'$file'" alt="'$file'class="resize lazy" data-original="$dir/$file" style="max-height: 100%; max-width: 100%" />';
                                    }
                            }
                      }
                }
    ?>


    But now I have one little problem
    PHP Code:
      $count $count 1;
                            if (
    $count == 36
    This worked fantastically, until I notice it is only pulling in the FIRST 36 images in the folder, so in other words new content never gets displayed because its 37 - 38 - 39 in order etc


    How could I make the scan pick randomly?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •