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"; }?> Link to comment https://forums.phpfreaks.com/topic/45672-random/ 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()"; Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-221805 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. Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-221818 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... Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-221824 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? Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-221885 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 Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-221889 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']; .......... ......... ......... ......... } Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-221891 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 Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-221898 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. Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-221900 Share on other sites More sharing options...
MadTechie Posted April 5, 2007 Share Posted April 5, 2007 Someone missed the point ! Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-221902 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 Link to comment https://forums.phpfreaks.com/topic/45672-random/#findComment-222381 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.