drums Posted March 9, 2011 Share Posted March 9, 2011 I have an old script that I do not recall where I got it but would like to enhance it. I thought of creating multiple ones and just give them different names but that seems redundant and certainly less elegant than doing it in one script. I have 5 images running down each side of a wordpress site (left & right sidebar) though the platform is not relevant in this application, I don't think... I want to be able to randomize each of those 10 images on each page so that each time a user visits the page or refreshes all the images change. Is there a way to randomize all those images without special casing each one like the code I have and my lack of PHP knowledge would dictate? $cpics = array ( "images/pic1.jpg", "images/pic2.jpg", "images/pic3.jpg" ); $index = rand(0, 2); $cbanner = $cpics[$index]; And then on the page I put <img src="$cbanner"> So with my lack of PHP programming skills I would create an array for each location giving each array a different name so that my sidebar code would look like this: <li><img src="$1banner"></li> <img src="$2banner"> <img src="$3banner"> <img src="$4banner"> <img src="$5banner"> Thanks very much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/ Share on other sites More sharing options...
cssfreakie Posted March 10, 2011 Share Posted March 10, 2011 hehe well my php knowledge is not good either but i think i can provide the code for this. What i do know is mark-up and css, and what you are at least missing is the alt=" " attribute! OK here goes nothing echo '<ul id="photos">'; //start list $cpics = array ("images/pic1.jpg","images/pic2.jpg","images/pic3.jpg"); shuffle($cpics); // shuffle the b***ches foreach ($cpics as $image) { echo '<li><img src="'.$image.'" alt=" " /></li>'; } echo '</ul>'; Hope this works for you cssfreakie Edit: to make it realy awesome you could even add a title tag echo '<ul id="photos">'; //start list $cpics = array ("images/pic1.jpg","images/pic2.jpg","images/pic3.jpg"); shuffle($cpics); // shuffle the b***ches foreach ($cpics as $image) { echo '<li><img src="'.$image.'" alt=" " title="'.basename($image).'"/></li>'; } echo '</ul>'; and know you may use that last one with my copyright haha : Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185320 Share on other sites More sharing options...
drums Posted March 10, 2011 Author Share Posted March 10, 2011 Wow, very cool, works like a charm! And hey, don't underestimate yourself Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185406 Share on other sites More sharing options...
cssfreakie Posted March 10, 2011 Share Posted March 10, 2011 Wow, very cool, works like a charm! And hey, don't underestimate yourself wicked have fun with it! Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185410 Share on other sites More sharing options...
drums Posted March 10, 2011 Author Share Posted March 10, 2011 I got so excited I forgot to ask about a couple things... How do a limit the number of images (I have 30 images that will be rotating through 5 spots on one side of the page and 5 on the other)? And where is or how do you get the basename? the title="'.basename($image).'" thing? Thanks very much! Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185411 Share on other sites More sharing options...
cssfreakie Posted March 10, 2011 Share Posted March 10, 2011 well basename() is just a nice php function i found a week ago to get the name of the file. so in case you have echo basename(http://www.lalala.lalalala.alalala.com/lalalala/lala/cows.jpg); it will echo cows.jpg now as far as limiting the numer of images, it depends on how you set the array of results, are they from a data base use LIMIT 5 for instance in your query. you could also try a for loop or while loop or place an if statement in the foreach loop that executes code as long as a condition (number) is as you expect. But i think a for or while loop is specially designed for that. Just google for loop or while loop and you see how it can be limited or you could try array slice, maybe easier in this case: http://www.jonasjohn.de/snippets/php/array-slice.htm Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185421 Share on other sites More sharing options...
drums Posted March 10, 2011 Author Share Posted March 10, 2011 Eek, :'( that got my head spinning. It's late (I'm old, ok ) so I have to maybe try tomorrow and see if I can wrap my head around it :'( Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185425 Share on other sites More sharing options...
cssfreakie Posted March 10, 2011 Share Posted March 10, 2011 i am old too look at my profile 1010 years and that's light years 1 method using array_splice $limit = 10; echo '<ul id="photos">'; //start list $cpics = array ("images/pic1.jpg","images/pic2.jpg","images/pic3.jpg"); shuffle($cpics); // shuffle the b***ches $cpics = array_slice($cpics,0, $limit); //limit it; foreach ($cpics as $image) { echo '<li><img src="'.$image.'" alt=" " title="'.basename($image).'"/></li>'; } echo '</ul>'; you may thank me by posting as first on my blog cssfreakie.blogspot.com haha -edit i changed it i read splice but it was slice :)it works now Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185427 Share on other sites More sharing options...
cssfreakie Posted March 10, 2011 Share Posted March 10, 2011 i edited a bit code above works now. Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185432 Share on other sites More sharing options...
drums Posted March 10, 2011 Author Share Posted March 10, 2011 Thanks dude! I just posted on your blog, happy to be the first! Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185449 Share on other sites More sharing options...
cssfreakie Posted March 10, 2011 Share Posted March 10, 2011 Thanks dude! I just posted on your blog, happy to be the first! ah sweet! ty i started yesterday so I'm in marketing now See ya Quote Link to comment https://forums.phpfreaks.com/topic/230148-how-to-make-this-random-image-array-do-it-in-multiple-locations-on-a-page/#findComment-1185450 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.