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. Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/ 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() Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415861 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]."" Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415864 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 Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415906 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); Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415910 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? Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415943 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. Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415947 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 Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415959 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. Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415960 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? Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415964 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 ?> Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415968 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. Link to comment https://forums.phpfreaks.com/topic/81856-order-by-functions/#findComment-415971 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.