What is the best way to create a banner ad using information stored in a database? I have the information I was to use and already pulled from the database using PHP. How can I put this into a 460x80 banner in .gif or .png? I want to banner to shower something different from the database everytime the page is refreshed. Is this a pretty easy thing to do or require a lot of work?

Recommended Answers

All 8 Replies

i recommend you use .png.... about the banner will it's not really hard... because you use database...all you do is some mysql query and PHP predefined function rand();

here ill show you the code
lets assume you had these table

banner
banner_id - int (primary)(autoincrement)
src = varchar

<?php

mysql_connect('localhost','root',''); // i use the default
mysql_select_db('your_database_name'); //enter your database
$sql = mysql_query("SELECT banner_id FROM banner");
$max = mysql_num_rows($sql);
$sql2 = mysql_query( "SELECT src FROM banner WHERE banner_id='".rand(1,$max)."'" );
$row = mysql_fetch_array($sql2);
echo "<img src='".$row['src']."' width='300' height='200' />";

?>

put a banner folder in your root document....
insert the images path in the src field of your banner table
ex. banner/your_images.png

Member Avatar for diafol

Do you have images or urls in your DB - or just string info? If you need to create images on the fly - use GD2.
Like VD says - you can use a randomizer to display a different image each time. Over time, the number of displays should pan out.

$r = mysql_query( "SELECT urlfield FROM bannertable ORDER BY RAND() LIMIT 1");

This way you avoid the mistake of linking the number of records in the table with the 'id'. I'll explain - if the last 'id' in the table is, say 234 and the number of records in the table is only 201 (coz you deleted a few or whatever), you will never display image with an id of 234 or any number greater than 201 for than matter.

ur so amazing ardav... i bow to you... you always have the most proficient approach on every problems... your code are always impressive. u solve it with jus less code. and i always end up embarassed... hehe... well that's ok though... it's not ur fault to be smart.. hehe

Member Avatar for diafol

Sorry VD - I'm no great shakes, but I notice that you are making an amazing contribution at the moment with so many replies - well done! It's just that I've been there and done it with some of these questions - I've had the number of records != max id number in table problem and was stuck for hours trying to figure it out. However, I think there may be an even more sophisticated way of doing it.

I'm not sure that RAND() is truly random - more a quick and dirty randomizer *I think*.

Sorry VD - I'm no great shakes, but I notice that you are making an amazing contribution at the moment with so many replies - well done! It's just that I've been there and done it with some of these questions - I've had the number of records != max id number in table problem and was stuck for hours trying to figure it out. However, I think there may be an even more sophisticated way of doing it.

I'm not sure that RAND() is truly random - more a quick and dirty randomizer *I think*.

There's no such thing as truly random when it comes to the implementations in PHP or MySQL, it'll always be pseudorandom

thanks, ill try this out and see what happens. part of my database info is text, like the users name, then the other part is a .gif file. I just want to combine these and create a clickable banner ad to promote around the web. Does this affect anything?

Member Avatar for diafol

There's no such thing as truly random when it comes to the implementations in PHP or MySQL, it'll always be pseudorandom

Thanks Shawn, doubts confirmed.:)

thanks, ill try this out and see what happens. part of my database info is text, like the users name, then the other part is a .gif file. I just want to combine these and create a clickable banner ad to promote around the web. Does this affect anything?

No - fine.

is this thread solved? if so... mark this solved then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.