Jump to content
Larry Ullman's Book Forums

Alternating 2 Logo Images On Site When Page Changes


Recommended Posts

After reading this book and practicing, I am creating a site that has more than one main logo, I want them to alternate when the page changes to another page or is refreshed. So I thought of using the ternary operator, but I can't get it to work. 

Since there is no while loop involved in my header page where this code will go, I guess that's why it doesn't work. I got it to work with php's shuffle function, but that doesn't create the effect I want exactly. Anyways here is where I am at with this, any help to get this working would be appreciated.

<header>

$imglogo1 = '<img src="images/logo5214.jpg" alt="logo" class="logo2">';
$imglogo2 = '<img src="images/logo5213.jpg" alt="logo" class="logo2">';

$bg = $imglogo1;
$bg = ($bg==$imglogo1 ? $imglogo2 : $imglogo1);

</header>
Link to comment
Share on other sites

I did something similiar..I used cookies and set the pictures to change based on time.

I used an  if (isset($_COOKIE['picture'])) 

                  {

                   display pix 1

                  }else{

                    display pix 2

                   }

I am thinking there is a better way using javascript....

Link to comment
Share on other sites

To me, it depends upon what "alternation" means. It is every other page view (viewing the same page again) or every other page. And is it per user or for everyone? But a session variable is probably the solution. 

 

All that being said, I question of the merits of having true alternation. You'd have a statistically comparable result using a random generator to pick the image, and I wonder whether it will matter to the end user which image is shown (and when/how) or not.

Link to comment
Share on other sites

I tinkered with the Session variable idea, but my being new at php, I couldn't figure out the best way to go about it. So I stuck with the shuffle function, the code I am using is below. The only problem with the code below is that when the second image comes in, it kind of jumps in the browser sometimes, I don't know why it does that. And when I have an error in my page the images show up multiple times on the error page.

 

function array_random2($arr){
shuffle($arr);
 
return $arr[0];
}
 
$imglogo1 = '<img src="images/logo5214.jpg" alt="logo" class="logo2">';
$imglogo2 = '<img src="images/logo5213.jpg" alt="logo" class="logo2">';
$a = array($imglogo1, $imglogo2);
 
echo array_random2($a);
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...