joshgarrod Posted February 2, 2008 Share Posted February 2, 2008 ello everyone, does anyone know of a decent simple script for random banner rotation script please? thanks Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/ Share on other sites More sharing options...
The Little Guy Posted February 2, 2008 Share Posted February 2, 2008 http://phpsnips.com/snippet.php?id=14 Any questions with that, please ask. All images/banners must be in the same folder. Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456100 Share on other sites More sharing options...
joshgarrod Posted February 2, 2008 Author Share Posted February 2, 2008 ok, so how would i format that to display in a table? Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456106 Share on other sites More sharing options...
joshgarrod Posted February 2, 2008 Author Share Posted February 2, 2008 also where is the link included in that? Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456109 Share on other sites More sharing options...
The Little Guy Posted February 2, 2008 Share Posted February 2, 2008 That is just a base to get a random image from a folder of images and display it on the page. You will have to add the link, and the table. <?php //This will get an array of all the .gif images in a folder $img_array = glob("/path/to/images/*.gif"); //Pick a random image from the array $img = array_rand($img_array); //Display the image on the page echo '<table><tr><td><a href="http://site.com/their/link.html"><img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" /></a></td></tr></table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456114 Share on other sites More sharing options...
joshgarrod Posted February 2, 2008 Author Share Posted February 2, 2008 thanks v much Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456127 Share on other sites More sharing options...
joshgarrod Posted February 2, 2008 Author Share Posted February 2, 2008 sorry, i cant get it to work, the image just shows up with the x???? Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456149 Share on other sites More sharing options...
The Little Guy Posted February 2, 2008 Share Posted February 2, 2008 can you show the current code you used? Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456163 Share on other sites More sharing options...
joshgarrod Posted February 2, 2008 Author Share Posted February 2, 2008 <?php //This will get an array of all the .gif images in a folder $img_array = glob("/images/banners/home/*.gif"); //Pick a random image from the array $img = array_rand($img_array); //display image echo '<td rowspan=7 valign=top><table><tr><td><a href=www.google.com><img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" /></a></td></tr></table></td></tr>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456180 Share on other sites More sharing options...
AndyB Posted February 2, 2008 Share Posted February 2, 2008 $img_array = glob("/images/banners/home/*.gif"); That assumes the random images are in a folder named home, which is in a folder named banners which is in a folder named images. You need to adjust that to match where your images are. Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456201 Share on other sites More sharing options...
MadnessRed Posted February 2, 2008 Share Posted February 2, 2008 I tried the script and got Warning: array_rand() [function.array-rand]: First argument has to be an array in /home/grewa/public_html/beltd.co.uk/includetop.php on line 98 line 98 is $img = array_rand($img_array); My whole script is <a href="gallery/index.php"> <?php //This will get an array of all the .jpg images in a folder $img_array = glob("gallery/images/*.jpg"); //Pick a random image from the array $img = array_rand($img_array); //Display the image on the page echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" width=100 />'; ?> </a> Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456220 Share on other sites More sharing options...
The Little Guy Posted February 2, 2008 Share Posted February 2, 2008 Try this: <a href="gallery/index.php"> <?php if(!file_exists('gallery/images')){echo 'Directory gallery/images Not Found';} //This will get an array of all the .jpg images in a folder $img_array = glob("gallery/images/*.jpg"); //Pick a random image from the array $img = array_rand($img_array); //Display the image on the page echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" width=100 />'; ?> </a> Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456234 Share on other sites More sharing options...
MadnessRed Posted February 2, 2008 Share Posted February 2, 2008 its ok I fixed it. Somehow I had target="0" in the link tag so when I removed that its worked fine. Thanks for the help though Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456235 Share on other sites More sharing options...
joshgarrod Posted February 3, 2008 Author Share Posted February 3, 2008 what do i do if i want to have a different lin k depending on what banner shows up? Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-456711 Share on other sites More sharing options...
MadnessRed Posted February 6, 2008 Share Posted February 6, 2008 Provided the link is the same as the image you can do what I have done. echo '<a href="'.$img_array[$img].'.php">'; echo '</a>'; If you put that either side of the image, then it will take you to the image with a .php at the end, for example for image logo.png you would be take to logo.png.php which you could set to redirect to where you wanted. So example code would be <?php if(!file_exists('gallery/images')){echo 'Directory gallery/images Not Found';} //This will get an array of all the .jpg images in a folder $img_array = glob("gallery/images/*.jpg"); //Pick a random image from the array $img = array_rand($img_array); //Display the image on the page echo '<a href="'.$img_array[$img].'.php">'; echo '<img alt="'.$img_array[$img].'" src="'.$img_array[$img].'" width=100 />'; echo '</a>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-460080 Share on other sites More sharing options...
cooldude832 Posted February 6, 2008 Share Posted February 6, 2008 I wrote this one session based to never show the same banner twice <?php $header_img = $_SESSION['head_img']; while($header_img === $_SESSION['head_img']){ $header_img = rand(0,count(glob(ROOT."images/headers/*.jpg"))-1); } $_SESSION['head_img'] = $header_img; ?> So you simply use the $header_img as the img source properly adjusted to the right folder Quote Link to comment https://forums.phpfreaks.com/topic/89059-solved-random-banner-rotator-script/#findComment-460085 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.