Page 1 of 1
Random rotation banner script
Posted: Wed Aug 06, 2003 2:04 pm
by RacerX
Can someone point me in the direction to find a rotating banner script? Or paste the code here?
I need to rotation script to randomly sort 3 or 4 text files when the page gets refreshed.
Any ideas?
Posted: Wed Aug 06, 2003 2:18 pm
by oldtimer
I use this for my banner add rotation.
Code: Select all
<?php
$sqlquery = "SELECT * From banners where active='Y' ORDER BY RAND() LIMIT 1";
$result=mysql_query($sqlquery);
$bid=mysql_result($result,0,'bid');
$image=mysql_result($result,0,'image');
$sql=("update banners set views=views+1 where bid='$bid'");
$result2=mysql_query($sql);
echo "<a href="http://www.domain.com/banners/banners.php?bid=$bid" target="_blank">
<img src="http://www.coastgames.com/banners/images/$image" border="0"></a>";
?>
Now I am showing a graphic but you could easily adapt that to show a text file. This script is pulling from a database but a hard coded array would work as well.
Posted: Wed Aug 06, 2003 2:25 pm
by RacerX
Thank you!
Forgive my ignorance, but which areas would I swap out?
Example files:
/data/msg-1.txt
/data/msg-2.txt
/data/msg-3.txt
Posted: Wed Aug 06, 2003 2:31 pm
by oldtimer
Okay. lets say you have the names of the text files in a database. As that is what I have. Then you could have something like this.
Code: Select all
<?php
$sqlquery = "SELECT * From banners where active='Y' ORDER BY RAND() LIMIT 1";
$result=mysql_query($sqlquery);
$bid=mysql_result($result,0,'bid');
$text=mysql_result($result,0,'text');
$sql=("update banners set views=views+1 where bid='$bid'");
$result2=mysql_query($sql);
echo $text;
?>
First line pulls out a random text file. Last line prints it to the screen.
Posted: Wed Aug 06, 2003 2:35 pm
by RacerX
Ok.
So what you're saying is this has to use a database. Is there an alternative? Maybe just a random rotate of sorts?
We are building a DB to do this properly, but in the meantime I'd like to see what I can do...
Posted: Wed Aug 06, 2003 2:52 pm
by oldtimer
Yes there is. You can use an array but I dont have the random function for it on hand. You can build the array as
$files= array("one.txt","two.txt","three.txt","four.txt")
Then randomize it.
Posted: Wed Aug 06, 2003 3:17 pm
by RacerX
I think it could be shuffle() But I don't know the exact syntax...
Posted: Wed Aug 06, 2003 3:20 pm
by m3rajk
instead of randomizing the array, call a part fo the array at random......
$link=$link_array[rand(0, (count($link_array)-1))];
randomly giving you an element within the array (note, count()-1 used because count gives you the number of elements, the highest number that wont give an error is count-1 since arrays start counting at 0)
Posted: Thu Aug 07, 2003 7:02 am
by RacerX
Thanks for the script m3rajk.
I am getting an error though...
Parse error: parse error in /home/u5/franchise/html/rhcol/rh-sponsors-default.php on line 13
Can anyone see my mistake. I am sure it is obvious to you pro's.
Code: Select all
<?php
$sponsor=randSponsor();
funtion randSponsor(){
$spon=array('sponsor-80.txt', 'sponsor-115.txt', 'sponsor-300.txt');
$randspon=$spon[rand(0, (count($spon)-1))];
return $randspon;
}
?>
Posted: Thu Aug 07, 2003 10:25 am
by m3rajk
funtion should be function, but that could just be in typing it up here....
if line 13 is the line $randspon is set, then it could be getting the random number. so if that's 13, try replacing it with
Code: Select all
$randIndex=rand(0, (count($spon)-1));
$randspon=$spon[$randIndex];