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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.