Jump to content

[SOLVED] rotate images sequentially on refresh, not randomly... please help


mac007

Recommended Posts

Hello all: need little help with this array. This example here rotates images randomly on refresh; my problem is that I want it to sequentially show images: first image1, then image2 and so forth and come back around all over again.

 

Can anybody help please?

 

<CODE>

 

<?

$banner1 = "../images/testimonialsBoxes/image1.gif";

$banner2 = "../images/testimonialsBoxes/image2.gif";

$banner3 = "../images/testimonialsBoxes/image3.gif";

$banner4 = "../images/testimonialsBoxes/image4.gif";

$banner5 = "../images/testimonialsBoxes/image5.gif";

 

$banner_array = array($banner1, $banner2, $banner3, $banner4, $banner5);

?>

 

<img src="<?php echo $banner_array[rand(0,4)] ?>"/>

 

 

</CODE>

 

Thank you!

well, it doesnt have to be "per user" defined, just generally when somebody loads page, I want the script to sequentially show each image in order, as opposed to randomly, which is how the script I placed here does... how can it be modified so it does it sequentially?

 

Thanks

Try this:

 

<?php
session_start();
if (isset($_SESSION['banner'])){
$_SESSION['banner'] = $_SESSION['banner']==5?1:$_SESSION['banner']+1;
}
else{
$_SESSION['banner'] = rand(0,4);
}
$banner1 = "../images/testimonialsBoxes/image1.gif";
$banner2 = "../images/testimonialsBoxes/image2.gif";
$banner3 = "../images/testimonialsBoxes/image3.gif";
$banner4 = "../images/testimonialsBoxes/image4.gif";
$banner5 = "../images/testimonialsBoxes/image5.gif";

$banner_array = array($banner1, $banner2, $banner3, $banner4, $banner5);
?>

<img src="<?php echo $banner_array[$_SESSION['banner']] ?>"/>

thanks, FiFan:

 

I guess this revised code says "if session exists run in this order; else run randomly"???

I dont want to have a random option at all, I want it so page will first load with image1, then image2, and so forth, and run back in same order again... 

 

does that make sense?

 

thanks

<?php
session_start();
if (isset($_SESSION['banner'])){
$_SESSION['banner'] = $_SESSION['banner']==5?1:$_SESSION['banner']+1;
}
else{
$_SESSION['banner'] = 1;
}
$banner1 = "../images/testimonialsBoxes/image1.gif";
$banner2 = "../images/testimonialsBoxes/image2.gif";
$banner3 = "../images/testimonialsBoxes/image3.gif";
$banner4 = "../images/testimonialsBoxes/image4.gif";
$banner5 = "../images/testimonialsBoxes/image5.gif";

$banner_array = array($banner1, $banner2, $banner3, $banner4, $banner5);
?>

<img src="<?php echo $banner_array[$_SESSION['banner']] ?>"/>

Thanks F1fan... gonna try it a bit later!

while we are at it, can you explain the usage of "==5?1:" in the line here:

 

$_SESSION['banner'] = $_SESSION['banner']==5?1:$_SESSION['banner']+1;

 

I havent seen this characters used this way before, specially the ? in there, can you quickly explain that?  I assume the == sign asigns 5 to the session, but what is the ?1: part doing?

 

thanks again!

Sure. It's a short-hand way to assign values like an IF. So, this:

<?php
$_SESSION['banner'] = $_SESSION['banner']==5?1:$_SESSION['banner']+1;
?>

does the same thing as this:

<?php
if ($_SESSION['banner']==5){
  $_SESSION['banner'] = 1;
}
else{
  $_SESSION['banner'] = $_SESSION['banner']+1;
}
?>

I see, pretty cool. I really need to get onto php more heavily.

 

You know I just tried it, it kind of works, but it only starts from image 3 forward to image 5, then starts over; completely bypasses images 1 & 2

 

trying to see why it's doing that, any ideas?

<?php
session_start();
if (isset($_SESSION['banner'])){
$_SESSION['banner'] = $_SESSION['banner']==5?1:$_SESSION['banner']+1;
}
else{
$_SESSION['banner'] = 1;
}
$banner[1] = "../images/testimonialsBoxes/image1.gif";
$banner[2] = "../images/testimonialsBoxes/image2.gif";
$banner[3] = "../images/testimonialsBoxes/image3.gif";
$banner[4] = "../images/testimonialsBoxes/image4.gif";
$banner[5] = "../images/testimonialsBoxes/image5.gif";
?>

<img src="<?php echo $banner[$_SESSION['banner']]; ?>"/>

 

Try that. I cleaned it up a little and added a missing ; for you.

almost!!!  It still starts off from image 3, but now it does goes thru all images afterwards in order... so the only issue I have is just that, that it begins on image 3...

 

thanks for all your help and patience.

 

 

Alternately

 

<?php
session_start();

$banner[0] = "../images/testimonialsBoxes/image1.gif";
$banner[1] = "../images/testimonialsBoxes/image2.gif";
$banner[2] = "../images/testimonialsBoxes/image3.gif";
$banner[3] = "../images/testimonialsBoxes/image4.gif";
$banner[4] = "../images/testimonialsBoxes/image5.gif";

if ( !isset($_SESSION['banner']) )
$_SESSION['banner'] = count( $banner );

$key = $_SESSION['banner'] % count( $banner );
$_SESSION['banner']++;

?>

<img src="<?php echo $banner[$key]; ?>"/>

I'm looking for a similar solution as the thread starter. I've tried to use the code shown above, but it doesn't work for me. Here is the error I get:

 

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 5

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 6

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 7

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 8

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 9

 

If I change "banner" to something else, the code "works" but only once -- as soon as I reload the page the above error reappears.

 

Any suggestions?

Show your code. All of it.

 

It's the same as above (as I'm only testing it at this stage):

 

<?php
session_start();

$banner[0] = "../images/testimonialsBoxes/image1.gif";
$banner[1] = "../images/testimonialsBoxes/image2.gif";
$banner[2] = "../images/testimonialsBoxes/image3.gif";
$banner[3] = "../images/testimonialsBoxes/image4.gif";
$banner[4] = "../images/testimonialsBoxes/image5.gif";

if ( !isset($_SESSION['banner']) )
$_SESSION['banner'] = count( $banner );

$key = $_SESSION['banner'] % count( $banner );
$_SESSION['banner']++;

?>

<img src="<?php echo $banner[$key]; ?>"/>

 

And this is the error I get:

 

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 5

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 6

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 7

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 8

Warning: Cannot use a scalar value as an array in /usr/www/users/cvdadmin/fn/test.php on line 9

 

Now if I change the code ("banner" to "b23") to:

 

<?php

session_start();

$b23[0] = "../images/testimonialsBoxes/image1.gif";
$b23[1] = "../images/testimonialsBoxes/image2.gif";
$b23[2] = "../images/testimonialsBoxes/image3.gif";
$b23[3] = "../images/testimonialsBoxes/image4.gif";
$b23[4] = "../images/testimonialsBoxes/image5.gif";

if ( !isset($_SESSION['b23']) )
$_SESSION['b23'] = count( $b23 );

$key = $_SESSION['b23'] % count( $b23 );
$_SESSION['b23']++;

?>

<img src="<?php echo $b23[$key]; ?>"/>

 

The code "works" without the error ONCE, but on each page reload the error reappears.  If I then change "b23" to something else again it goes through without an error again, until the next reload when the error reappears.

 

Any smart ideas?

What lines are 5-9? I'm assuming that they're the lines where $banner is being set. Try this:

 

<?php
session_start();
$banner = array();
$banner[0] = "../images/testimonialsBoxes/image1.gif";
$banner[1] = "../images/testimonialsBoxes/image2.gif";
$banner[2] = "../images/testimonialsBoxes/image3.gif";
$banner[3] = "../images/testimonialsBoxes/image4.gif";
$banner[4] = "../images/testimonialsBoxes/image5.gif";

if ( !isset($_SESSION['banner']) )
$_SESSION['banner'] = count( $banner );

$key = $_SESSION['banner'] % count( $banner );
$_SESSION['banner']++;

?>

<img src="<?php echo $banner[$key]; ?>"/>

Hey, thanks for your help! Appreciated!  :)

 

This solved that error. But the code doesn't go through all 5 banners. It starts at banner 1, it shows banner 2 on page reload, and then keeps showing 2 on each reload, while it should move on to 3, 4 and 5.

 

Any ideas?

Try this:

<?php
session_start();
$banner = array();
$banner[0] = "../images/testimonialsBoxes/image1.gif";
$banner[1] = "../images/testimonialsBoxes/image2.gif";
$banner[2] = "../images/testimonialsBoxes/image3.gif";
$banner[3] = "../images/testimonialsBoxes/image4.gif";
$banner[4] = "../images/testimonialsBoxes/image5.gif";
if (isset($_SESSION['banner'])){
   $_SESSION['banner'] = $_SESSION['banner']==count($banner)?0:$_SESSION['banner']+1;
}
else{
   $_SESSION['banner'] = 0;
}
?>
<img src="<?php echo $banner[$_SESSION['banner']]; ?>"/>

Okay, I've tried the new code. This is what happens; When I first open the page image1 appears, without any errors. On reload I get the following error though;

 

Fatal error: Unsupported operand types in /usr/www/users/cvdadmin/fn/test3.php on line 10

 

Line 10 is this:

 

   $_SESSION['banner'] = $_SESSION['banner']==count($banner)?0:$_SESSION['banner']+1;

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.