Jump to content

random


avatar.alex

Recommended Posts

ok i am creating a radom ad rotator and i need to know how to display the ads radmonly heres the code if you could help me out that would be great:

 

<?
include("connect.php");

$query="SELECT * FROM ads ";
$result=mysql_query($query);
$num = mysql_num_rows ($result);
mysql_close();

if ($num > 0 ) {
$i=0;
while ($i < $num) {
$image = mysql_result($result,$i,"image");
$imageurl = mysql_result($result,$i,"imageurl");
$id = mysql_result($result,$i,"id");

echo "<b><a href='$imageurl'><img src='$image'></a><br>";

++$i; } } else { echo "The database is empty"; }?>

Link to comment
https://forums.phpfreaks.com/topic/45672-random/
Share on other sites

Note that while "ORDER BY RAND()" is simple and effective, it is not efficient for a large dataset.  It's probably fine for an ad rotator (how many ads are you really going to have?), but since it's expensive, you'll get very performance on a large table.

 

ORDER BY RAND() first generates a pseudo-random number of every row in the result set, then sorts the entire result set by the random number.

 

For a large table, you are better off doing the randomization in PHP.

Link to comment
https://forums.phpfreaks.com/topic/45672-random/#findComment-221818
Share on other sites

yea that didn't work lol thanks anyways...that just make the ads show up in different places that could be usfule in the future but with that code how would or what would I altar to get it to show random ads i would like to crate it because It just makes it easyer if I know my way around to code just incase I want to add something... :)

Link to comment
https://forums.phpfreaks.com/topic/45672-random/#findComment-221824
Share on other sites

yea that didn't work lol thanks anyways...that just make the ads show up in different places that could be usfule in the future but with that code how would or what would I altar to get it to show random ads i would like to crate it because It just makes it easyer if I know my way around to code just incase I want to add something... :)

 

I don't understand your question.  If you add "ORDER BY RAND() LIMIT 1" to the end of the query, it will give you one result at random.  Is that not what you want?

Link to comment
https://forums.phpfreaks.com/topic/45672-random/#findComment-221885
Share on other sites

Another way to make random

$query="SELECT * FROM ads ";
$result=mysql_query($query);

while ($row = mysql_fetch_assoc($result))
       $queryResult[] = $row;
}
echo "<pre>";
print_r($queryResult);
$display_data = shuffle($queryResult);
echo "<pre>";
print_r($display_data );

for($i=0;$i<count($display_data);$i++){
        echo $display_data[$i]['name'];
..........
.........
.........
.........
}

 

Link to comment
https://forums.phpfreaks.com/topic/45672-random/#findComment-221891
Share on other sites

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.