Jump to content

Recommended Posts

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"

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.
?>

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.

 

 

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.

?>

 

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;

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.