Jump to content

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.

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.