Jump to content

How to make this random image array do it in multiple locations on a page


drums

Recommended Posts

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.

 

Link to comment
Share on other sites

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 ::D

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :D

 

 

-edit i changed it i read splice but it was slice :)it works now

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.