Jump to content
Larry Ullman's Book Forums

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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...