random ad banners in PHP

General discussions related to php

Moderators: macek, egami, gesf

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Tue Jan 28, 2003 11:31 pm

where is the information on the banners stored. in a database or what..

You can make an array and randomly access the content.

see http://www.php.net/array for more info.

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Wed Jan 29, 2003 12:54 am

You must create table with banners nanners id links images...

after that use rand() function of rand query to show tha banner..

furiousweebee
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Feb 14, 2003 10:10 pm

Postby furiousweebee » Fri Feb 14, 2003 10:13 pm

Could you please give me more information?

I was hoping it would be as simple as a command along the lines of:

select rand() from $table_name

or something like that.

I have a table with fields: id, banner_imgurl, banner_link, and banner_alttext

I just want to work out how to randomly pull one record from this database for a banner rotation system on my site. Also in the future I would like to be able to set a priority for each entry, so that a priority rating of (for example) 100, would appear about 100 times as often as something with a rating of one. Any help would be great. grant_o@hotmail.com

furiousweebee
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Feb 14, 2003 10:10 pm

Postby furiousweebee » Sat Feb 15, 2003 3:16 pm

Ok here's what I've got: (sorry for the length)
-----------------------
<?
$db_name = "******";
$table_name = "advertising";

$connection = @mysql_connect("localhost", "******", "******")
or die("Couldn't connect.");

$db = @mysql_select_db($db_name, $connection)
or die("Couldn't select database.");

$sql = "SELECT id, banner_url, banner_h, banner_w, banner_alt, banner_link
FROM $table_name
ORDER by rand() LIMIT 1
";

$result = @mysql_query($sql,$connection)
or die("Couldn't execute query.");

$num=mysql_numrows($result);
$i=0;
while ($i < $num) {

$banner_url=mysql_result($result,$i,"banner_url");
$banner_h=mysql_result($result,$i,"banner_h");
$banner_w=mysql_result($result,$i,"banner_w");
$banner_alt=mysql_result($result,$i,"banner_alt");
$banner_link=mysql_result($result,$i,"banner_link");
?>

-----------------------
Then putting the variables into the page:
-----------------------

<a href="http://<? echo "$banner_link"; ?>" target="_blank"><img src="http://<? echo "$banner_url"; ?>" width="<? echo "$banner_w"; ?>" height="<? echo "$banner_h"; ?>" alt="<? echo "$banner_alt"; ?>" border="0"></a>

-----------------------
Then iterating
-----------------------
<?

++$i;

}

?>

----------------------------------------------
So can anyone tell me where I've gone wrong? I'm a bit of a novice, sorry.

Hurkle
New php-forum User
New php-forum User
Posts: 36
Joined: Tue Jan 07, 2003 8:45 am
Location: Minnesota, USA

Postby Hurkle » Wed Feb 19, 2003 7:49 am

It looks like the approach you're using is just about right. I suspected that you might not be getting true randomness, as you're not seeding the rand() function. I did a quick search, and found this post which explains one thing to try quite elegantly. Rather than pasting the text, I'll just give you the link, so you can see the whole thing in context. Hope this helps.

http://codewalkers.com/archives/phpcoding/1596.html

furiousweebee
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Feb 14, 2003 10:10 pm

Postby furiousweebee » Tue Mar 25, 2003 5:30 pm

Hi, thanks for that! You've been a big help.

For some reason I was never emailed about a reply to this topic.... oh well. Thanks!


Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 3 guests