Displaying elements from an array sequentially

Hi. I'd like to receive an image from an array, sequentially, after I click a button.
So when I first click on a button an image (Axajafied) will appear on my page. When I hit the button again I'd like the next image in the array to appear, and so on. I have the javascript / Ajax part working correctly. However my PHP code (where the request is received) is where I need to implement this. Currently I have a technique where I shuffle the array and choose the last element of the array. See attached code.
Thanks
// MAKE AN ARRAY OF THE JPG IMAGE FILE NAMES
    $b = array();
    while (($filename = readdir($dh)) !== FALSE)
    {
        // CHOOSE ONLY JPG IMAGES
       $file_ext = strtoupper(end(explode('.', $filename)));
        if ($file_ext != 'JPG') continue;
   
      
        $b[] = $filename;
		
    }
    closedir($dh); 

    // MAKE AN ARRAY OF THE JPG IMAGE FILE NAMES
 
     
    // WITH ALL FILE NAMES IN AN ARRAY, RANDOMIZE THE LIST AND CHOOSE ONE
    shuffle($b);
    $banner_name  = array_pop($b);
    $banner_url   = $my_dir . $banner_name;
    
}

Open in new window

graziaziAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
Do the random on the client side, initialy you should be able to provide the number of images available : Math.random() * NumberOfImagesAvailable
http://www.w3schools.com/jsref/jsref_random.asp

put each new number in an array to check if you already get it with the random function, if not send your request to php
0
graziaziAuthor Commented:
I'm not too sure what you mean (down to my lack of knowledge).
Surely I don't have to use random as I want the array to work sequentially, as it already is?
0
leakim971PluritechnicianCommented:
don't generate the random image to return on the PHP side
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

graziaziAuthor Commented:
i.e. take out shuffle?
0
hmarcbowerCommented:
You could try this:

shuffle($b); // randomizes order of array and re-keys values
$banner_name = $b[0];  // since you shuffled the array, you might as well start at the first element now
$banner_url   = $my_dir . $banner_name;

Open in new window


Just by starting at 0 it makes it easier to loop through.  Include a line to figure out how many images names are in the array

$numberofimages = count($b); // how many images are we working with

and then you can increment the array key sequence in the loop and send the next image.  Or, perhaps, if you're using JSON or something (I don't know much about that) you can pass it the whole array and let it deal with the coding - which might be what leakim971 is suggesting, but I don't know about that side of it enough to be sure. :)
0
graziaziAuthor Commented:
Thanks for that. So set up a while or foreach loop?
0
leakim971PluritechnicianCommented:
>i.e. take out shuffle?

yes and do it on the client side with Math.random
0
hmarcbowerCommented:
I think you're going to have to take it client-side as leakim has suggested, and just program your other to accept the whole array.  The server-side nature of PHP means you can't really ask it to sit and wait in the middle of a loop for someone to click a button.
0
graziaziAuthor Commented:
ok thanks guys. So on the client side I can keep track of what element I am at in the array?
0
leakim971PluritechnicianCommented:
>So on the client side I can keep track of what element I am at in the array?
not really

the user click

1 - you generate a random number : Math.random() * NumberOfImagesAvailable
2 - you check your array to see if you already get this number
3 - if not you you save this number in an array : myarray.push(newNumber)
4 - else you generate a new number -> part 2
5 - you send the number to php to get the associated image
6 - you display the image
7 - you wait for a new click

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
graziaziAuthor Commented:
Thanks guys. Understood!
0
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!

>7 - Happy end! Ready for a new click

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.