Jump to content

[SOLVED] PHP Ad rotator


spikypunker

Recommended Posts

Hi Guys

 

I have a website setup which is starting to get some interest, i have a few local small businesses interested in having small ads up on it, and i need to develop a rotator.

 

I know a bit of php and have dropped in ASP bits in the past. I can see how i would develop a simple script by selecting a random row from a table perhaps? But the thing is i already have the pages setup as HTML (i know its a lame reason) but i was hoping there was a way i could do it without having to change ALLL the page names to .php (and then all the link too)

 

Also how would i set it up so that i can have six different ads spaces on the page, and it doesnt select the same ad twice? Also i want to have six ads of one size, then two ads of a different size at the top.

 

Can anyone push me in the right direction?

 

Peace!

Link to comment
Share on other sites

you would have to use mod-rewrite to output as html files, as for the ad rotator its a simple list of variables containing strings, displayed randomly using the random function :)

 

as for making sure 6 different ads are different on each load you could do a few if statements or just a while until the randomly generated numbers fit your criteria

Link to comment
Share on other sites

Ok, you don't need to change your .html files to anything at all. If you put PHP tags into a HTML file then they get parsed as PHP regardless of there being a HTML suffix to the file or not. Create ad_function.php and paste the following code into it.

 

<?php

function ads(){

$dbconnect; //your code to connect to your database

$limit = 6; //number of ad spaces on the page
$large = 3; //number of full sized add spaces on your page

$query = mysql_query("SELECT * FROM ads ORDER BY Rand() LIMIT $limit");

while($sql=mysql_fetch_array($query)){

	$i=0;

	if($i < $large){
		$ad[]=$sql["full_ad"]; //URL for full sized or large ad banner
		$i++;
	}else{
		$ad[]=$sql["small_ad"]; //URL for small sized or lcut down ad banner
		$i++;
	}
}

return $ad;

}

?>

 

In your HTML files put the following after the <body> tags.

 

<?php require_once("ad_function.php"); ?>

 

Then to display each add you need to do the following:

 

<img src="<?php $ad['0'] ?>"></img><br />
<img src="<?php $ad['1'] ?>"></img><br />
<img src="<?php $ad['2'] ?>"></img><br />
<img src="<?php $ad['3'] ?>"></img><br />
<img src="<?php $ad['4'] ?>"></img><br />
<img src="<?php $ad['5'] ?>"></img><br />

 

This code is untested as I'm at work, but something along these lines is what you're looking for. Good luck.

Link to comment
Share on other sites

I forgot to mention, you should have your ad images on your server with a database that has the paths and names of all the images, in large (column name full_ad) and small (column name small_ad) size, which is where the script gets the path of the image from to output to the HTML.

Link to comment
Share on other sites

I also forgot to mention, after

 

<?php require_once("ad_function.php"); ?>

 

but before

 

<img src="<?php $ad['0'] ?>"></img><br />
<img src="<?php $ad['1'] ?>"></img><br />
<img src="<?php $ad['2'] ?>"></img><br />
<img src="<?php $ad['3'] ?>"></img><br />
<img src="<?php $ad['4'] ?>"></img><br />
<img src="<?php $ad['5'] ?>"></img><br />

 

You need to put

 

<?php $ad=ads(); ?>

Link to comment
Share on other sites

Basically, what your DB needs to contain is the following.

 

(column name - column type)

 

ID - Auto Increment Int(11)

BusinessName - varChar(128)

full_ad - text

small_ad - text

 

As an example an entry in the database would be:

 

1, Amteus Secure Communications Ltd, images/amteus_large.jpg, images/amteus_small.jpg

 

You would then have a folder in your main directory called images, in the images folder you would have 2 images called amteus_large.jpg and amteus_small.jpg. That way, when the php function is run, one of the array values returned would be "images/amteus_large.jpg" or "images/amteus_small.jpg". that way when the HTML file is parsed and displayed the image tags look like <img src="images/amteus_large.jpg"></img> just like regular HTML image tags.

 

simple eh....

Link to comment
Share on other sites

Just re-read your question. Yes it would be simple.

 

you need to add another column to that DB.

 

ad_url - text

 

data put into this should just be the website like http://www.website.com so that when the <a href> is compiled it looks like a normal href...

 

Change the ads() function to the following:

 

<?php

function ads(){

$dbconnect; //your code to connect to your database

$limit = 6; //number of ad spaces on the page
$large = 3; //number of full sized add spaces on your page

$query = mysql_query("SELECT * FROM ads ORDER BY Rand() LIMIT $limit");

while($sql=mysql_fetch_array($query)){

	$i=0;

	if($i < $large){
		$ad["ad"]=$sql["full_ad"]; //URL for full sized or large ad banner
		$ad["url"]=$sql["ad_url"]; //URL for full sized or large ad banner to redirect to website
		$i++;
	}else{
		$ad["ad"]=$sql["small_ad"]; //URL for small sized or cut down ad banner
		$ad["url"]=$sql["ad_url"]; //URL for full sized or large ad banner to redirect to website
		$i++;
	}
}

return $ad;

}

?>

 

Then in your HTML change your image tags to this...

 

<a href="<?php $ad['url']['0'] ?>"><img src="<?php $ad['ad']['0'] ?>"></img></a><br />

Link to comment
Share on other sites

Ah mate thats wkd, i understand everything you've said so far and think will be able to implement it, the only thing i think i would get stuck on (i know you already helped loads!) is that not all the ads will have BOTH a large and small version, in fact i think each business will have either a large or a small ad on the site, so i'm guessing your method works by a table setup like:

 

 

Business      |  Large Ad            |  Small  Ad            | URL                                |

shoe shop    |  images/large.jpg  |  images/small.jpg  | http://www.shoeshop.html  |

 

Is this right? If do you think it would be fairly simple to add another field that says large YES/NO

Then could add to the first part of the script saying  If $ i < $large and Large == YES then....

 

or something along those lines?

 

Ahhh if you can help me out with that then thats all and i can start making it!!

 

woo!

 

 

Link to comment
Share on other sites

I think the best way to sort that would be to do the following, that way you don't need to change anything else. Add a new column to the database as you said. Then change ad_functions.php to this...

 

<?php

function ads(){

$dbconnect; //your code to connect to your database

$limit = 6; //number of ad spaces on the page
$large = 3; //number of full sized add spaces on your page
$small = $limit - $large;

$query_large = mysql_query("SELECT * FROM ads WHERE large = \"YES\" ORDER BY Rand() LIMIT $large");

while($sql=mysql_fetch_array($query_large)){

	$i=0;

	if($i < $large){
		$ad["ad"]=$sql["full_ad"]; //URL for full sized ad banner
		$ad["url"]=$sql["add_url"]; //URL for full sized  ad banner to redirect to website
		$i++;
	}
}

$query_small = mysql_query("SELECT * FROM ads WHERE large = \"NO\" ORDER BY Rand() LIMIT $small");

while($sql=mysql_fetch_array($query_small)){

	$i=0;

	if($i < $small){
		$ad["ad"]=$sql["small_ad"]; //URL for small sized banner
		$ad["url"]=$sql["add_url"]; //URL for small sized ad banner to redirect to website
		$i++;
	}
}

return $ad;

}

?>

Link to comment
Share on other sites

Hey guys, right it's not working currently, one thing i've found is that is does seem to need to be a .php file (how the code is inserted currently anyway) but even with it being a .php file it's still not showing anything where the PHP code is inserted. It's driving me crazy! Could someone please cast an eye over it?

 

here is the file ad_function.php

 

<?php

function ads(){

mysql_connect("localhost", "****", "****"); 

@mysql_select_db("****") or die ("unable to connect"); 




   $limit = 8; //number of ad spaces on the page
   $large = 2; //number of full sized add spaces on your page
   $small = $limit - $large;





   $query_large = mysql_query("SELECT * FROM ads WHERE large = \"YES\" ORDER BY Rand() LIMIT $large");







$i=0;
   while($sql=mysql_fetch_array($query_large)){

      

      if($i < $large){
         $ad["ad"]=$sql["full_ad"]; //URL for full sized ad banner
         $ad["url"]=$sql["add_url"]; //URL for full sized  ad banner to redirect to website
         $i++;

      }
   }
   
   $query_small = mysql_query("SELECT * FROM ads WHERE large = \"NO\" ORDER BY Rand() LIMIT $small");
   
    $i=0;

   while($sql=mysql_fetch_array($query_small)){

  

      if($i < $small){
         $ad["ad"]=$sql["small_ad"]; //URL for small sized banner
         $ad["url"]=$sql["add_url"]; //URL for small sized ad banner to redirect to website
         $i++;
      }
   }

   return $ad;

}


?>

 

And this is the page that i'm using to test the ad display:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ad rotator test</title>
</head>

<body>

THIS IS A TEST PAGE<br /><br />

<?php require_once("ad_function.php"); ?>


<?php $ad=ads(); ?>

<a href="<?php $ad['url']['0'] ?>"><img src="<?php $ad['ad']['0'] ?>"></img></a><br />
<a href="<?php $ad['url']['1'] ?>"><img src="<?php $ad['ad']['1'] ?>"></img></a><br />
<a href="<?php $ad['url']['2'] ?>"><img src="<?php $ad['ad']['2'] ?>"></img></a><br />
<a href="<?php $ad['url']['3'] ?>"><img src="<?php $ad['ad']['3'] ?>"></img></a><br />
<a href="<?php $ad['url']['4'] ?>"><img src="<?php $ad['ad']['4'] ?>"></img></a><br />
<a href="<?php $ad['url']['5'] ?>"><img src="<?php $ad['ad']['5'] ?>"></img></a><br />
<a href="<?php $ad['url']['6'] ?>"><img src="<?php $ad['ad']['6'] ?>"></img></a><br />
<a href="<?php $ad['url']['7'] ?>"><img src="<?php $ad['ad']['7'] ?>"></img></a><br />


</body>
</html>

 

 

Link to comment
Share on other sites

do a print_r on the array to check it's being populated correctly and that all the associations are what you are assuming them to be. Post the results of the print_r here.

 

Note, your print_r call needs to be on the HTML page you're trying to display the images on...

Link to comment
Share on other sites

OK dude, done that, here's where i put the print_r

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ad rotator test</title>
</head>

<body>

THIS IS A TEST PAGE<br /><br />

<?php require_once("ad_function.php"); ?>


<?php $ad=ads(); 

Print_r ($ad);

?>

<a href="<?php $ad['url']['0'] ?>"><img src="<?php $ad['ad']['0'] ?>"></img></a><br />
<a href="<?php $ad['url']['1'] ?>"><img src="<?php $ad['ad']['1'] ?>"></img></a><br />
<a href="<?php $ad['url']['2'] ?>"><img src="<?php $ad['ad']['2'] ?>"></img></a><br />
<a href="<?php $ad['url']['3'] ?>"><img src="<?php $ad['ad']['3'] ?>"></img></a><br />
<a href="<?php $ad['url']['4'] ?>"><img src="<?php $ad['ad']['4'] ?>"></img></a><br />
<a href="<?php $ad['url']['5'] ?>"><img src="<?php $ad['ad']['5'] ?>"></img></a><br />
<a href="<?php $ad['url']['6'] ?>"><img src="<?php $ad['ad']['6'] ?>"></img></a><br />
<a href="<?php $ad['url']['7'] ?>"><img src="<?php $ad['ad']['7'] ?>"></img></a><br />


</body>
</html>

 

And heres the source of what came out:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Ad rotator test</title>

</head>

 

<body>

 

THIS IS A TEST PAGE<br /><br />

 

 

 

Array

(

    [ad] => Images/images/ads/saints.jpg

    => http://www.google.com

)

 

<a href=""><img src=""></img></a><br />

 

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

 

 

</body>

</html>

 

 

Cheers so much for helping seriously!

 

For extra insight the testpage is here:

 

http://www.maddogmagazine.co.uk/rotator.php

Link to comment
Share on other sites

Oh yeah also, here is a copy of my mysql table, i've checked that the linking details are correct (plau the print_r showed data from the table)

 

id name full_ad small_ad large ad_url

Edit Delete 1 Dining Room Blank http://www.maddogmagazine.co.uk/Images/images/ads/... NO http://www.google.com

Edit Delete 2 Access to Music Blank http://www.maddogmagazine.co.uk/Images/images/ads/... NO http://www.google.com

Edit Delete 3 Bettys Blank http://www.maddogmagazine.co.uk/Images/images/ads/... NO http://www.google.com

Edit Delete 4 Dusk till Dawn Blank http://www.maddogmagazine.co.uk/Images/images/ads/... NO http://www.google.com

Edit Delete 5 Dnd Records Blank http://www.maddogmagazine.co.uk/Images/images/ads/... NO http://www.google.com

Edit Delete 6 Saints Bar Blank http://www.maddogmagazine.co.uk/Images/images/ads/... NO http://www.google.com

Edit Delete 7 Norwich Arts Centre http://www.maddogmagazine.co.uk/Images/images/ads/... Blank YES http://www.google.com

Edit Delete 8 Rockstar http://www.maddogmagazine.co.uk/Images/images/ads/... Blank YES http://www.

Link to comment
Share on other sites

ok, replace ad_function.php with the following.

 

<?php

function ads(){

mysql_connect("localhost", "****", "****"); 

@mysql_select_db("****") or die ("unable to connect"); 

   $limit = 8; //number of ad spaces on the page
   $large = 2; //number of full sized add spaces on your page
   $small = $limit - $large;

   $query_large = mysql_query("SELECT * FROM ads WHERE large = \"YES\" ORDER BY Rand() LIMIT $large");
   
   $i=0;
   
   while($sql=mysql_fetch_array($query_large)){

      

      if($i < $large){
         $ad["ad"][$i]=$sql["full_ad"]; //URL for full sized ad banner
         $ad["url"][$i]=$sql["add_url"]; //URL for full sized  ad banner to redirect to website
         $i++;

      }
   }
   
   $query_small = mysql_query("SELECT * FROM ads WHERE large = \"NO\" ORDER BY Rand() LIMIT $small");
   
   $i=0;

   while($sql=mysql_fetch_array($query_small)){

      if($i < $small){
         $ad["ad"][$i+$large+1]=$sql["small_ad"]; //URL for small sized banner
         $ad["url"][$i+$large+1]=$sql["add_url"]; //URL for small sized ad banner to redirect to website
         $i++;
      }
   }

   return $ad;

}


?>

Link to comment
Share on other sites

Wkd man, it's now getting all the rows in but still not putting anything into the IMG SRC bit or the URL, also it's pulling in the BLANKS meaning the IF LARGE == YES is not working? I dunno man but i've played with it and cant get any further! Nearly there tho, really appreciate everything so far!

 

<body>

 

THIS IS A TEST PAGE<br /><br />

 

 

 

Array

(

    [ad] => Array

        (

            [0] => http://www.maddogmagazine.co.uk/Images/images/ads/rockstar.jpg

            [1] => Blank

            [2] => http://www.maddogmagazine.co.uk/Images/images/ads/atm.jpg

            [3] => Blank

            [4] => http://www.maddogmagazine.co.uk/Images/images/ads/dinningroom.jpg

            [5] => http://www.maddogmagazine.co.uk/Images/images/ads/bettys.jpg

            [6] => http://www.maddogmagazine.co.uk/Images/images/ads/saints.jpg

            [7] => http://www.maddogmagazine.co.uk/Images/images/ads/dusktilldawn.jpg

        )

 

    => Array

        (

            [0] => http://www.google.com

            [1] => http://www.google.com

            [2] => http://www.google.com

            [3] => http://www.google.com

            [4] => http://www.google.com

            [5] => http://www.google.com

            [6] => http://www.google.com

            [7] => http://www.google.com

        )

 

)

 

 

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

<a href=""><img src=""></img></a><br />

 

 

</body>

</html>

Link to comment
Share on other sites

Woo hoo!!

 

It's nearly there man, it's now bringing in the images! and the URLS!

 

The only glitch now is the Blanks it's still bringing in from the DB, so is this a problem with the Large = YES bit?

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Ad rotator test</title>

</head>

 

<body>

 

THIS IS A TEST PAGE<br /><br />

 

 

 

Array

(

    [ad] => Array

        (

            [0] => Blank

            [1] => Blank

            [2] => Blank

            [3] => Blank

            [4] => http://www.maddogmagazine.co.uk/Images/images/ads/atm.jpg

            [5] => http://www.maddogmagazine.co.uk/Images/images/ads/saints.jpg

            [6] => http://www.maddogmagazine.co.uk/Images/images/ads/bettys.jpg

            [7] => http://www.maddogmagazine.co.uk/Images/images/ads/dnd.jpg

        )

 

    => Array

        (

            [0] => http://www.google.com

            [1] => http://www.google.com

            [2] => http://www.google.com

            [3] => http://www.google.com

            [4] => http://www.google.com

            [5] => http://www.google.com

            [6] => http://www.google.com

            [7] => http://www.google.com

        )

 

)

<br /><br />

 

<a href="http://www.google.com"><img src="Blank"></img></a><br />

<a href="http://www.google.com"><img src="Blank"></img></a><br />

<a href="http://www.google.com"><img src="Blank"></img></a><br />

<a href="http://www.google.com"><img src="Blank"></img></a><br />

<a href="http://www.google.com"><img src="http://www.maddogmagazine.co.uk/Images/images/ads/atm.jpg"></img></a><br />

<a href="http://www.google.com"><img src="http://www.maddogmagazine.co.uk/Images/images/ads/saints.jpg"></img></a><br />

<a href="http://www.google.com"><img src="http://www.maddogmagazine.co.uk/Images/images/ads/bettys.jpg"></img></a><br />

<a href="http://www.google.com"><img src="http://www.maddogmagazine.co.uk/Images/images/ads/dnd.jpg"></img></a><br />

 

</body>

</html>

Link to comment
Share on other sites

Ahh i see, sorry i've been not paying as much attention to the code as i should have. I see the LARGE = YES but has been removed from the PHP function page? Here is the function page...

 

<?php

function ads(){

mysql_connect("localhost", "maddogma_admin", "connect"); 

@mysql_select_db("maddogma_ads") or die ("unable to connect"); 




   $limit = 8; //number of ad spaces on the page
   $large = 2; //number of full sized add spaces on your page

   $query = mysql_query("SELECT * FROM ads ORDER BY Rand() LIMIT $limit");
   
   $i=0;

   while($sql=mysql_fetch_array($query)){

            if($i < $large){
         $ad["ad"][$i]=$sql["full_ad"]; //URL for full sized or large ad banner
         $ad["url"][$i]=$sql["ad_url"]; //URL for full sized or large ad banner to redirect to website
         $i++;
      }else{
         $ad["ad"][$i]=$sql["small_ad"]; //URL for small sized or cut down ad banner
         $ad["url"][$i]=$sql["ad_url"]; //URL for full sized or large ad banner to redirect to website
         $i++;
      }
   }

   return $ad;

}

?>

 

All that needs to happen now is that the first IF part (the large ads) need to only select from the DB where column LARGE is equal to YES, then only select the ELSE bits where the LARGE is equal to NO. I'm not too sure how to code this, can anyone help finish this off?

 

Many thanks and kind regards!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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