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
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
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
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
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
Share on other sites

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.