grahamgr3 Posted December 6, 2014 Share Posted December 6, 2014 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 More sharing options...
Duke Posted December 7, 2014 Share Posted December 7, 2014 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 More sharing options...
HartleySan Posted December 7, 2014 Share Posted December 7, 2014 I don't think you need to use JS for this, but you do need a way of recording the logo used from page to page so that you can alternate. A cookie is fine, of course, but I'd probably just use a session variable instead. All the same, it all works fine. Link to comment Share on other sites More sharing options...
Larry Posted December 8, 2014 Share Posted December 8, 2014 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 More sharing options...
grahamgr3 Posted December 8, 2014 Author Share Posted December 8, 2014 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 More sharing options...
Duke Posted December 8, 2014 Share Posted December 8, 2014 would jQuery Anything slider work for you...? Link to comment Share on other sites More sharing options...
HartleySan Posted December 8, 2014 Share Posted December 8, 2014 You really don't need (nor do I think you want) jQuery or anything JS for this task. Link to comment Share on other sites More sharing options...
Recommended Posts