czukoman20 Posted December 15, 2007 Share Posted December 15, 2007 can someone give me as many examples as to what you can use the ORDER BY Function to randomize the order.. Like rand(-what goes inside here) what i'm looking for is an order by function that is a different randomization everytime its ordered out of the same datatable. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 15, 2007 Share Posted December 15, 2007 Like this SELECT * FROM table ORDER BY rand() Quote Link to comment Share on other sites More sharing options...
alecks Posted December 15, 2007 Share Posted December 15, 2007 you could do $random = rand(1,2); switch ($random) { case 1: $random = "DESC"; break; case 2: $random = "ASC"; break; } And then the query would be "SELECT * FROM table ORDER BY `id` ".$random."" (assuming there is an id column) You could of course expand the random range and add more options or actually $random = rand(0,1); $options = array("DESC","ASC"); "SELECT * FROM table ORDER BY `id` ".$options[$random]."" Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 16, 2007 Author Share Posted December 16, 2007 Ok well i just thought of something.. how would i find out how many rows are in my datatable and use that to distinguish the rand(0, rows in table) Then i wouldn't have to update it everytime the table changes Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 16, 2007 Share Posted December 16, 2007 Ok well i just thought of something.. how would i find out how many rows are in my datatable and use that to distinguish the rand(0, rows in table) Then i wouldn't have to update it everytime the table changes use mysql_num_rows($query); Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 16, 2007 Author Share Posted December 16, 2007 so then i would have to have two queries one to call the table.. and the other to put the num rows variable into action? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 16, 2007 Share Posted December 16, 2007 so then i would have to have two queries one to call the table.. and the other to put the num rows variable into action? You could use 1 variable, and do both actions. Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 16, 2007 Author Share Posted December 16, 2007 now im confused as to how to do this. could u possibly give an example please thanks Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 16, 2007 Share Posted December 16, 2007 now im confused as to how to do this. could u possibly give an example please thanks Just post your code and I will set it up for you. Then I will explain what i did. Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 16, 2007 Author Share Posted December 16, 2007 ok here is my code //Here we count the number of results //Edit $data to be your query $data = mysql_query("SELECT * FROM users WHERE userid_2 > 0 ORDER BY rand()") or die(mysql_error()); thats probably all you need out of it right? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 16, 2007 Share Posted December 16, 2007 <?php $data = mysql_query("SELECT * FROM users WHERE userid_2 > 0 ORDER BY rand()") or die(mysql_error()); $num_rows = mysql_num_rows($data); echo $num_rows; //will echo how many rows were returned from the query ?> Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 16, 2007 Author Share Posted December 16, 2007 ok well my problem is deeper than this now that i have found out.. and im just lost now.. .. . ... ugh>!? ! and this has to go with my random pagination. so if you guys are looking at this one.. then can you please read the topic that is random pagination. because that is the base of what this problem is thanks. 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.