graziazi
asked on
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
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;
}
ASKER
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?
Surely I don't have to use random as I want the array to work sequentially, as it already is?
don't generate the random image to return on the PHP side
ASKER
i.e. take out shuffle?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for that. So set up a while or foreach loop?
>i.e. take out shuffle?
yes and do it on the client side with Math.random
yes and do it on the client side with Math.random
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.
ASKER
ok thanks guys. So on the client side I can keep track of what element I am at in the array?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys. Understood!
You're welcome! Thanks for the points!
>7 - Happy end! Ready for a new click
>7 - Happy end! Ready for a new click
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