TheBrandon Posted August 24, 2010 Share Posted August 24, 2010 Hello all, I need to make a rotating banner system for a client. They want about 5 banners to rotate with Javascript. However they want the visitor to sort of step through each one on each refresh. So if I have 5 banners and I go to the site, I see banner 1 and then the javascript rotates through each one. When I go back to the site, they want it to start with banner 2 and then the javascript will rotate through each one. Then on and on. Go back and see banner 3, then step through again. I have a nice Javascript image slider I'd like to use. It basically just requires the images in order. Like: <img src="1.jpg"/> <img src="2.jpg"/> <img src="3.jpg"/> <img src="4.jpg"/> <img src="5.jpg"/> I've thought about it and I think the best way to do this would just be using PHP and a cookie. So when the viewer first goes to the site, PHP checks for the cookie. If it exists, it pulls the starting image #. If it doesn't, it starts at 0 and sets the cookie. As for the images part, I figured an associate array would work well. The key would be what the cookie logic is based on, and then the image HTML would be in the array. So you visit the site without a cookie it would be... 0 => <img src="1.jpg"/> 1 => <img src="2.jpg"/> 2 => <img src="3.jpg"/> 3 => <img src="4.jpg"/> 4 => <img src="5.jpg"/> Then when you go back to the site, I need the array to be re-ordered like this: 0 => <img src="2.jpg"/> 1 => <img src="3.jpg"/> 2 => <img src="4.jpg"/> 3 => <img src="5.jpg"/> 4 => <img src="1.jpg"/> What's the best way to go about this array logic? My experience with manipulating data inside arrays is rather limited. Basically I need it to say "Start with key # and then reorder array based on that" Quote Link to comment https://forums.phpfreaks.com/topic/211647-display-array-in-order-and-then-reorder-array-by-1/ Share on other sites More sharing options...
jcbones Posted August 24, 2010 Share Posted August 24, 2010 An interesting problem. I like helping people with interesting problems. So I wrote a little bit of code for you. You will need to make it interact with your javascript, but you can see how it works. <?php session_start(); //Start a session. $images = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg'); //An array that holds all of your images. $start = (isset($_SESSION['rotate'])) ? (int)$_SESSION['rotate'] : 0; //if the session is set, then return the session, otherwise return 0. $banner_array = array(); //start a banner array. $image_count = count($images); //count how many images are in the image array. $start = ($start > $image_count) ? 0 : $start; //if our start count is higher than the number of images, reset it back to 0. for($i = $start; $i < $image_count; $i++) { $banner_array[] = $images[$i]; //starting at our count, put the images in the banner array. } if($start > 0) { for($i = 0; $i < $start; $i++) { $banner_array[] = $images[$i]; //if our start count was higher than the first image, then get the remaining images onto the end of the banner array. } } $_SESSION['rotate'] = ($start + 1); //save the new start point to the session. echo '<pre>'; print_r($banner_array); echo '</pre>'; //print the array to the page, and lets see it work. ?> Quote Link to comment https://forums.phpfreaks.com/topic/211647-display-array-in-order-and-then-reorder-array-by-1/#findComment-1103340 Share on other sites More sharing options...
TheBrandon Posted August 24, 2010 Author Share Posted August 24, 2010 Wow, thank you! Your code does exactly what I needed it to! However just so I learn (not just copy and paste lol) what do these operators mean? $start = ($start > $image_count) ? 0 : $start; //if our start count is higher than the number of images, reset it back to 0. I guess what I'm confused about is how the syntax works. $var1 = ($var2 > $var3) includes the if statement? And then the ? 0: $start is the consequence of that if check? I normally use this as my reference: http://www.w3schools.com/PHP/php_operators.asp and sadly none of them are on there. Quote Link to comment https://forums.phpfreaks.com/topic/211647-display-array-in-order-and-then-reorder-array-by-1/#findComment-1103353 Share on other sites More sharing options...
jcbones Posted August 24, 2010 Share Posted August 24, 2010 It is boolean logic for an if/else statement. $var = (condition) ? true : false; Quote Link to comment https://forums.phpfreaks.com/topic/211647-display-array-in-order-and-then-reorder-array-by-1/#findComment-1103354 Share on other sites More sharing options...
Alex Posted August 24, 2010 Share Posted August 24, 2010 Also known as the ternary operator. You can read more about it on this page: http://php.net/manual/en/language.operators.comparison.php (scroll down to "Ternary Operator"). Quote Link to comment https://forums.phpfreaks.com/topic/211647-display-array-in-order-and-then-reorder-array-by-1/#findComment-1103355 Share on other sites More sharing options...
TheBrandon Posted August 25, 2010 Author Share Posted August 25, 2010 Hmm, it works well and I implemented it but it takes 2 times for it to move past 1. So it goes 1, 1, 2, 3, 4, 5, 1, 1, 2, 3, 4, 5 Any ideas on how to fix that? I'm using this code. I don't think I modified it. <?php session_start(); //Start a session. $images = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg'); //An array that holds all of your images. $start = (isset($_SESSION['rotate'])) ? (int)$_SESSION['rotate'] : 0; //if the session is set, then return the session, otherwise return 0. $banner_array = array(); //start a banner array. $image_count = count($images); //count how many images are in the image array. $start = ($start > $image_count) ? 0 : $start; //if our start count is higher than the number of images, reset it back to 0. for($i = $start; $i < $image_count; $i++) { $banner_array[] = $images[$i]; //starting at our count, put the images in the banner array. } if($start > 0) { for($i = 0; $i < $start; $i++) { $banner_array[] = $images[$i]; //if our start count was higher than the first image, then get the remaining images onto the end of the banner array. } } $_SESSION['rotate'] = ($start + 1); //save the new start point to the session. echo '<pre>'; print_r($banner_array); echo '</pre>'; //print the array to the page, and lets see it work. ?> Quote Link to comment https://forums.phpfreaks.com/topic/211647-display-array-in-order-and-then-reorder-array-by-1/#findComment-1103645 Share on other sites More sharing options...
AbraCadaver Posted August 25, 2010 Share Posted August 25, 2010 Here's a different take: session_start(); if(empty($_SESSION['images'])) { $_SESSION['images'] = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg'); } $banner = array_shift($_SESSION['images']); echo $banner; Or if you don't want to store the array in the session: session_start(); $images = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg'); if(!isset($_SESSION['count']) || ++$_SESSION['count'] >= count($images)) { $_SESSION['count'] = 0; } $banner = $images[$_SESSION['count']]; echo $banner; Quote Link to comment https://forums.phpfreaks.com/topic/211647-display-array-in-order-and-then-reorder-array-by-1/#findComment-1103658 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.