Novocaine88 Posted April 6, 2011 Share Posted April 6, 2011 Hi, I'm not necessarily looking for code examples for my problem, (they are welcome but its not my explicit goal). I'm looking more for advice on how to go about tackling the problem i have. what i already have Currently i have a search getting results from a MySQL database. The results are ordered by one field that signifies their importance. Many of these results have the same value in this one column, and i have a switch statement to distinguish different content/styles for the different values. (there are just over 10 different bands) So the most important results are shown first and with a different style to the ones below it. (some bands have the same styling) what i need I would like to be able to randomize the results but only randomize the results within their specified bands. e.g. lets say theres 100 results in band A and 100 in band B. I still want everything in band A to be listed above band B, but i would like to randomly change the order of the results in band A and band B, (in this case once a week). I need to save the order of this randomization (client/server side?) for a week, then run the script again to set a new order. Now I would still class myself as an amateur php'er so at this stage i dont even know how i would go about separating the bands and randomizing their results independantly, let alone causing this functionality to run only once a week. So can anyone suggest a way i might go about this? I'm pretty stuck right now. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/ Share on other sites More sharing options...
btherl Posted April 6, 2011 Share Posted April 6, 2011 Since you're only doing this once a week, you can do it a simple way: SELECT * FROM table ORDER BY band, RAND() The primary order is on band, and secondary order is random. Then you can loop through the results assigning values to a new column "rand_order" in your table, starting at 1 and going upwards. Then to fetch them in your random order for that week, you just need to order by that column: "ORDER BY rand_order" Does that sound like what you need? Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1197972 Share on other sites More sharing options...
btherl Posted April 7, 2011 Share Posted April 7, 2011 Or even simpler: UPDATE table SET rand_order = RAND() once a week. Then you can order by "band, rand_order" when fetching the data. Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1197982 Share on other sites More sharing options...
Novocaine88 Posted April 7, 2011 Author Share Posted April 7, 2011 So would SELECT * FROM table ORDER BY band, RAND() randomize the order of the results within their separate bands? If thats the case, then that sounds good. But how would i go about doing this automatically only once a week? It looks like your suggesting a manual once a week "re-order" Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1198106 Share on other sites More sharing options...
spiderwell Posted April 7, 2011 Share Posted April 7, 2011 create an event on mysql directly to trigger once a week? Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1198129 Share on other sites More sharing options...
Novocaine88 Posted April 7, 2011 Author Share Posted April 7, 2011 Right, I've got what i wanted. Thanks for all your suggestions. What I did was use the UPDATE table SET rand_order = RAND() and the SELECT * FROM table ORDER BY band, RAND() (thanks btherl) which did exactly what i wanted. Randomized the results completely, then when printing them out ordered them initially by their band and then their random number. And I've been told I can set up a cron job to run the randomization script once a week to changg the order again. (not sure if this is sort of what you meant spiderwell) Once again, Cheers for the help Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1198198 Share on other sites More sharing options...
btherl Posted April 7, 2011 Share Posted April 7, 2011 That will give you a different random order every single time. The select should be SELECT * FROM table ORDER BY band, rand_order in order to use the stored order that is updated each week with cron. Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1198431 Share on other sites More sharing options...
requinix Posted April 8, 2011 Share Posted April 8, 2011 RAND() allows you to seed the number generator. You don't need cron or a rand_order field: "SELECT * FROM table ORDER BY band, RAND(" . date("YW") . ")" date(YW) will spit out a different number each week (which starts on Monday) and within that week it will always be the same number. Same RNG seed means you get the same numbers in the same sequence each time. Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1198505 Share on other sites More sharing options...
spiderwell Posted April 8, 2011 Share Posted April 8, 2011 And I've been told I can set up a cron job to run the randomization script once a week to changg the order again. (not sure if this is sort of what you meant spiderwell) Once again, Cheers for the help maybe, I not sure i know what a cron job is, but event is created on the mysql database and is a bit of SQL that is trigger at times you specify it for example: CREATE EVENT `myevent` ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY ON COMPLETION PRESERVE DO UPDATE `mytable` SET `myfield` = NULL; this sets a column to null once a day. events usually drop after execution the on preserve bit means it doesnt drop the event once executed, so will run daily find out more here http://dev.mysql.com/doc/refman/5.1/en/create-event.html Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1198508 Share on other sites More sharing options...
Novocaine88 Posted April 8, 2011 Author Share Posted April 8, 2011 That will give you a different random order every single time. The select should be SELECT * FROM table ORDER BY band, rand_order in order to use the stored order that is updated each week with cron. Yeh sorry, that is what i have used. Didnt check what i wrote RAND() allows you to seed the number generator. You don't need cron or a rand_order field: "SELECT * FROM table ORDER BY band, RAND(" . date("YW") . ")" date(YW) will spit out a different number each week (which starts on Monday) and within that week it will always be the same number. Same RNG seed means you get the same numbers in the same sequence each time. Thanks for this, ill give it a go. It sounds like a better solution Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1198606 Share on other sites More sharing options...
Novocaine88 Posted April 8, 2011 Author Share Posted April 8, 2011 RAND() allows you to seed the number generator. You don't need cron or a rand_order field: "SELECT * FROM table ORDER BY band, RAND(" . date("YW") . ")" date(YW) will spit out a different number each week (which starts on Monday) and within that week it will always be the same number. Same RNG seed means you get the same numbers in the same sequence each time. I've given this a whirl and it seems to do the job very nicely. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/232876-randomize-the-order-of-an-array-after-x-amount-of-time/#findComment-1198631 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.