almightyegg Posted April 5, 2007 Share Posted April 5, 2007 can you order the out put of a while loop randomly? so one time it will show table values: 1 5 6 3 8 9 and next time it might show 5 7 3 2 8 1 etc... Is it possible?? Quote Link to comment https://forums.phpfreaks.com/topic/45695-solved-while-loop-random-order/ Share on other sites More sharing options...
MadTechie Posted April 5, 2007 Share Posted April 5, 2007 from what source ? File = (not sure) Database = ORDER BY RAND(); Array = shuffle() ? Quote Link to comment https://forums.phpfreaks.com/topic/45695-solved-while-loop-random-order/#findComment-221982 Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 $clut = mysql_query("SELECT * FROM clutch"); while($clutch = mysql_fetch_array($clut)){ So that would be: $clut = mysql_query("SELECT * FROM clutch ORDER BY rand()"); while($clutch = mysql_fetch_array($clut)){ ??? Quote Link to comment https://forums.phpfreaks.com/topic/45695-solved-while-loop-random-order/#findComment-221984 Share on other sites More sharing options...
MadTechie Posted April 5, 2007 Share Posted April 5, 2007 yep that will work how large is the database as using ORDER BY rand() is a quick dirty method Quote Link to comment https://forums.phpfreaks.com/topic/45695-solved-while-loop-random-order/#findComment-221986 Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 It will eventually have 100+ rows BUT I've just realised, using rand() would mean pagination wouldn't work because it randomise every page so chances are some will be listed twice and some won't be listed at all on the pages....is there a way around that?? Quote Link to comment https://forums.phpfreaks.com/topic/45695-solved-while-loop-random-order/#findComment-221989 Share on other sites More sharing options...
$cripts Posted April 5, 2007 Share Posted April 5, 2007 get all values put them into an array then output them using shuffle Quote Link to comment https://forums.phpfreaks.com/topic/45695-solved-while-loop-random-order/#findComment-221994 Share on other sites More sharing options...
MadTechie Posted April 5, 2007 Share Posted April 5, 2007 oow not a 'nice way' that i can think of, unless you return only the ID's and store them in array and then use something like 'where ID in ('.explode($storedID's, ",").') ' but thats tacky would have to be a session, storing a 100+ records in a array to be passed page to page is more tacky Quote Link to comment https://forums.phpfreaks.com/topic/45695-solved-while-loop-random-order/#findComment-221996 Share on other sites More sharing options...
almightyegg Posted April 5, 2007 Author Share Posted April 5, 2007 Oh....Well, for now, as I don't really want to put myself into something difficult I will use rand() and not paginate thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/45695-solved-while-loop-random-order/#findComment-222000 Share on other sites More sharing options...
$cripts Posted April 5, 2007 Share Posted April 5, 2007 u can still use pagination simply do it like this $query = mysql_query('SELECT ....... LIMIT 5,20'); while(.........) { $array[$id] = $idortext; } shuffle($array); foreach($array ...... ) { echo ......; } the only thing about using shuffle is if you need to keep the array keys they will be changed Quote Link to comment https://forums.phpfreaks.com/topic/45695-solved-while-loop-random-order/#findComment-222556 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.