Forum Moderators: coopster

Message Too Old, No Replies

Random Sponsor link

is this right?

         

mooger35

6:29 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



On my website I have a sponsor link that pulls info from a database. What I want is for it to be pulled randomly.

This is what I have and it appears to work but I'm not 100% certain of it. Did I do this right?

$sql_test = "SELECT * FROM sponsors WHERE valid = 'yes'";
$result_test = mysql_query($sql_test);
$num = mysql_num_rows($result_test);
srand((float) microtime() * 10000000);
while($row = mysql_fetch_row($result_test)) {
if ($row[3] == NULL)
$link = "";
else
$link = "href";
$myArray[$row[0]] = "<a ".$link."='../postlink.php?link=http://".$row[3]."' target='_blank'><img src='images/".$row[2]."' border='0' alt='".$row[1]."'></a>";
}
$rand_keys = array_rand($myArray, $num);
$sponsor = $myArray[$rand_keys[0]];

dreamcatcher

12:15 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not just add it to the sql query:

SELECT * FROM sponsors WHERE valid = 'yes' ORDER BY rand();

dc

mooger35

12:39 am on Jan 28, 2006 (gmt 0)

10+ Year Member



man... there is always a better way. Next time I'll just come here first. Thanks. :-)