Jump to content

Modify code to randomize?


dwest

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/97447-modify-code-to-randomize/
Share on other sites

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 :-)

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.