avatar.alex Posted April 5, 2007 Share Posted April 5, 2007 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"; }?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 5, 2007 Share Posted April 5, 2007 change $query="SELECT * FROM ads"; to $query="SELECT * FROM ads ORDER BY RAND()"; Quote Link to comment Share on other sites More sharing options...
Fergusfer Posted April 5, 2007 Share Posted April 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
avatar.alex Posted April 5, 2007 Author Share Posted April 5, 2007 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... Quote Link to comment Share on other sites More sharing options...
Fergusfer Posted April 5, 2007 Share Posted April 5, 2007 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? Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 5, 2007 Share Posted April 5, 2007 code supplied was correct, unless you wanted something else Fergusfer is correct about it shoudn't be used on large database but its the quick'n'cheap way of doing it Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 5, 2007 Share Posted April 5, 2007 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']; .......... ......... ......... ......... } Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 5, 2007 Share Posted April 5, 2007 true but more memory for large sites, humm what would be the best way to shuffle a table of 1,000,000,000,000+ records Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 5, 2007 Share Posted April 5, 2007 Paging is a better way. If we will display 1000's of records with images we can guess that how much time it will take to display. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 5, 2007 Share Posted April 5, 2007 Someone missed the point ! Quote Link to comment Share on other sites More sharing options...
avatar.alex Posted April 5, 2007 Author Share Posted April 5, 2007 thank you Fergusfer thats what i needed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.