Jump to content

Simple Banner Roation script


conker87

Recommended Posts

This is a script that I've edited a little bit to suit my needs, however, it's not working at all. It's not echoing the correct value (it's just echoing "1"). There's obviously something wrong, I just can't put my finger on it. All the print_r()'s are there for debugging, trying to find out what is wrong!

 

	require("_mysql.php");

$query = "SELECT COUNT(*) FROM `adverts` WHERE `align` = 'hor'";
$result = mysql_query($query);
$banner_no = mysql_fetch_array($result);

$query = "SELECT * FROM `adverts` WHERE `align` = 'hor'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);;

$no = rand(0, $banner_no[0]);

$hor_ads = $row[$no];

echo $hor_ads['code'];

Link to comment
https://forums.phpfreaks.com/topic/205648-simple-banner-roation-script/
Share on other sites

$query = "SELECT * FROM adverts WHERE align = 'hor' order by rand() LIMIT 1";
$result = mysql_query($query) or trigger_error("Adverts query failed Error: " mysql_error());
$banner = mysql_fetch_assoc($result) or trigger_error("Unable to fetch Adverts Result");

echo $banner['code'];

 

Should work for random images using MySQL rand() function.

order by rand() can get very expensive in terms of performance if you have a lot of rows.  I prefer to use:

SELECT * FROM table WHERE num_value >= RAND() * (SELECT MAX(num_value) FROM table) LIMIT 1;

Of course, if you're dealing with tens of rows rather than thousands, it isn't going to make much difference.

Archived

This topic is now archived and is closed to further replies.

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