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?
 
leakim971Connect With a Mentor PluritechnicianCommented:
>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
 
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
leakim971PluritechnicianCommented:
don't generate the random image to return on the PHP side
0
 
graziaziAuthor Commented:
i.e. take out shuffle?
0
 
hmarcbowerConnect With a Mentor Commented:
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
 
graziaziAuthor Commented:
Thanks guys. Understood!
0
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!

>7 - Happy end! Ready for a new click

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.