Random rotation banner script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
RacerX
Forum Newbie
Posts: 21
Joined: Tue Jul 01, 2003 9:21 am
Location: NorthEast USA

Random rotation banner script

Post 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?
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post 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.
User avatar
RacerX
Forum Newbie
Posts: 21
Joined: Tue Jul 01, 2003 9:21 am
Location: NorthEast USA

Post 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
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post 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.
User avatar
RacerX
Forum Newbie
Posts: 21
Joined: Tue Jul 01, 2003 9:21 am
Location: NorthEast USA

Post 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...
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post 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.
User avatar
RacerX
Forum Newbie
Posts: 21
Joined: Tue Jul 01, 2003 9:21 am
Location: NorthEast USA

Post by RacerX »

I think it could be shuffle() But I don't know the exact syntax...
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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)
User avatar
RacerX
Forum Newbie
Posts: 21
Joined: Tue Jul 01, 2003 9:21 am
Location: NorthEast USA

Post 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; 
}

?>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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];
Post Reply