mac007 Posted October 1, 2008 Share Posted October 1, 2008 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! Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 That wouldn't be random. If you wanted to start random and then cycle, through, I'd suggest using a session. If the session isn't set, use a random one. Otherwise, use the next one. Quote Link to comment Share on other sites More sharing options...
discomatt Posted October 1, 2008 Share Posted October 1, 2008 Sequentially per user? Try using sessions Quote Link to comment Share on other sites More sharing options...
mac007 Posted October 1, 2008 Author Share Posted October 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 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']] ?>"/> Quote Link to comment Share on other sites More sharing options...
discomatt Posted October 1, 2008 Share Posted October 1, 2008 Sequentially on anything other than a per-user basis is extremely difficult. What happens when 2 people hit the site at the same time? Quote Link to comment Share on other sites More sharing options...
mac007 Posted October 1, 2008 Author Share Posted October 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 <?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']] ?>"/> Quote Link to comment Share on other sites More sharing options...
mac007 Posted October 1, 2008 Author Share Posted October 1, 2008 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! Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 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; } ?> Quote Link to comment Share on other sites More sharing options...
mac007 Posted October 1, 2008 Author Share Posted October 1, 2008 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? Quote Link to comment Share on other sites More sharing options...
mac007 Posted October 1, 2008 Author Share Posted October 1, 2008 oh, it also shows a blank in between starting over... Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 1, 2008 Share Posted October 1, 2008 <?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. Quote Link to comment Share on other sites More sharing options...
mac007 Posted October 1, 2008 Author Share Posted October 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
mac007 Posted October 1, 2008 Author Share Posted October 1, 2008 OK, got i!! it was just the part of: else{ $_SESSION['banner'] = 1; } had to be set 0!! so it should be like: $_SESSION['banner'] = 0; Thanks a lot!! Quote Link to comment Share on other sites More sharing options...
discomatt Posted October 1, 2008 Share Posted October 1, 2008 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]; ?>"/> Quote Link to comment Share on other sites More sharing options...
mac007 Posted October 2, 2008 Author Share Posted October 2, 2008 OK, disregard my last comment about changing it to "0", original "1" is correct! I guess my sessions were not cleared, seems to be working ok now. Quote Link to comment Share on other sites More sharing options...
newdahas Posted October 3, 2008 Share Posted October 3, 2008 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? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 Show your code. All of it. Quote Link to comment Share on other sites More sharing options...
newdahas Posted October 6, 2008 Share Posted October 6, 2008 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? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 6, 2008 Share Posted October 6, 2008 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]; ?>"/> Quote Link to comment Share on other sites More sharing options...
newdahas Posted October 7, 2008 Share Posted October 7, 2008 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? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 7, 2008 Share Posted October 7, 2008 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']]; ?>"/> Quote Link to comment Share on other sites More sharing options...
newdahas Posted October 8, 2008 Share Posted October 8, 2008 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; Quote Link to comment Share on other sites More sharing options...
F1Fan Posted October 8, 2008 Share Posted October 8, 2008 That's shorthand and maybe your server doesn't like it. Replace that line with this: <?php if ($_SESSION['banner']==count($banner)) $_SESSION['banner'] = 0; else $_SESSION['banner']++; ?> Quote Link to comment 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.