dwest Posted March 23, 2008 Share Posted March 23, 2008 Hi, I have a WordPress photo gallery plugin which stores the order of the photo albums based on a sort order number assigned by me in the admin settings. I want to bypass this setting and display them in random order, different each time the page loads. The code in the plugin to get the sort order is $sortorder = $wpdb->get_var("SELECT sortorder FROM $wpdb->nggalbum WHERE id = '$albumID' "); if (!empty($sortorder)) { $gallery_array = unserialize($sortorder); } Is there an adjustment I can make to this that will give me a different order each time? I realize this is a hack to the plugin but I need to do it. I'm waiting on the plugin developer to formally add it as an option in the admin area of the plugin. Don't know when he'll get to it :-) Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/97447-modify-code-to-randomize/ Share on other sites More sharing options...
cmgmyr Posted March 23, 2008 Share Posted March 23, 2008 try $sortorder = $wpdb->get_var("SELECT sortorder FROM $wpdb->nggalbum WHERE id = '$albumID' ORDER BY RAND() LIMIT 1"); ...LIMIT optional, or you can just change the value...the RAND() is the part you want to stick with. Quote Link to comment https://forums.phpfreaks.com/topic/97447-modify-code-to-randomize/#findComment-498602 Share on other sites More sharing options...
dwest Posted March 23, 2008 Author Share Posted March 23, 2008 Thanks CMGMYR! Trying now. What is the significance of LIMIT? What is being limited? Quote Link to comment https://forums.phpfreaks.com/topic/97447-modify-code-to-randomize/#findComment-498610 Share on other sites More sharing options...
dwest Posted March 23, 2008 Author Share Posted March 23, 2008 Not working. Still seems to be putting them in the order specified in the admin settings. Quote Link to comment https://forums.phpfreaks.com/topic/97447-modify-code-to-randomize/#findComment-498612 Share on other sites More sharing options...
dwest Posted March 23, 2008 Author Share Posted March 23, 2008 Sorry! I got into the database and studied what is stored for sortorder and it is a string like this: a:8:{i:0;s:1:"7";i:1;s:1:"6";i:2;s:1:"1";i:3;s:1:"... I suppose that is an array? yes? Quote Link to comment https://forums.phpfreaks.com/topic/97447-modify-code-to-randomize/#findComment-498617 Share on other sites More sharing options...
dwest Posted March 23, 2008 Author Share Posted March 23, 2008 Solved this by using PHP function Shuffle() Further down in the plugin code is the following which takes each item in the array and does stuff to it: if (is_array($gallery_array)) { foreach ($gallery_array as $galleryID) { $out .= nggCreateAlbum($galleryID,$mode,$albumID); } } I changed it to: if (is_array($gallery_array)) { shuffle($gallery_array); foreach ($gallery_array as $galleryID) { $out .= nggCreateAlbum($galleryID,$mode,$albumID); } } Now the albums appear in a different order each time thsy are loaded. Thanks for the help anyway cmgmyr :-) Quote Link to comment https://forums.phpfreaks.com/topic/97447-modify-code-to-randomize/#findComment-498630 Share on other sites More sharing options...
cmgmyr Posted March 23, 2008 Share Posted March 23, 2008 dwest, I think it might not have worked because of your query, try something like this: $sql = "SELECT something FROM your_table WHERE id = '$albumID' ORDER BY RAND() LIMIT 5"; Just run this as a full query and not just getting the variable and see what you come up with. Putting the data into arrays (when it's already in one) will just slow you down. ...the limit will limit the amount of records returned, so in this one a max of 5 records can be returned. Quote Link to comment https://forums.phpfreaks.com/topic/97447-modify-code-to-randomize/#findComment-498754 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.