Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

3.1.10 ArrayList push / return sort limit ...


Go to End


5 Posts   2202 Views

Avatar
timo

Community Member, 47 Posts

12 April 2015 at 4:58pm

Edited: 12/04/2015 5:02pm

I'm pulling images from a DataObject.
When returning the ArrayList non of the methods work (sort limit reverse...)

public function getMixedGallery(){
		
		$startPage = StartPage::get()->First();
		$mixedGalleriesField = $startPage->MixedGalleries;
		$fieldArray = explode(",", $mixedGalleriesField);
		
		$images = ArrayList::create();
		foreach($fieldArray as $id){
			foreach(Dataobject::get('Product')->filter(array('ID' => $id, 'ShowProductImages' => '1')) as $gallery) {

				if($gallery->MyImages()->Count() > 0){
				
					$imagesForGallery = new ArrayData(array('myimage' => $gallery->MyImages(), 'mylink' => $gallery->Link()));
					$images->push($imagesForGallery);
				
				}
			}
		}
		
		$images = $images->reverse()->sort('RAND()')->limit(intval(trim($startPage->NumGallery)));
		return $images;
	}

thanks.timo

Avatar
wmk

Community Member, 87 Posts

12 April 2015 at 6:40pm

Hi timo,

`->sort('RAND()')` doesn't make sense on an ArrayList, as it doesn't query the database.

See API Docs, it sorts on a given and existing field. You can pass that random number to each new generated Image ArrayData.

`Dataobject::get('Product')->filter(array('ID' => $id, 'ShowProductImages' => '1'))` looks like it's returning exactly one DataObject, as you filter by procuct's ID, which is unique. I'd use the new syntax, e.g. something like `Product::get()->filter(array('ShowProductImages' => 1))->byID($id)` instead.

Feel free to join us on IRC for discussing what you want to do.

Happy Sunday and greetings,

wmk

Avatar
timo

Community Member, 47 Posts

12 April 2015 at 9:57pm

This is my solution.
Isnt there another way ?
Image gallery pulls Images from different Dataobject.
Dont limit each Image-set for itself but limits and shuffles all images in the end.

public function getMixedGallery(){
		
		$startPage = StartPage::get()->First();
		$mixedGalleriesField = $startPage->MixedGalleries;
		$fieldArray = explode(",", $mixedGalleriesField);
		$images = array();
		
		foreach($fieldArray as $id){
			foreach(Product::get()->filter(array('ShowProductImages' => 1))->byID($id) as $gallery) {
				$productLink = $gallery->Link();
				foreach($gallery->MyImages() as $myimage){
					array_push($images, array('mylink' => $productLink, 'myimage' => $myimage));
				}
			}
		}
		
		$keys = array_keys($images); 
		shuffle($keys); 
		$random = array(); 
		
		foreach ($keys as $key) { 
			$random[$key] = $images[$key]; 
		}
		
		$images = ArrayList::create();
		
		$i = 0;
		foreach ($random as $item){ 
			$images->push($item);
			$i++;
			if($i >= intval(trim($startPage->NumGallery))){
				break;
			}
		}
		
		return $images;
	}

timo.

Avatar
wmk

Community Member, 87 Posts

13 April 2015 at 4:14am

Edited: 13/04/2015 4:15am

Hey Timo, great you found a solution.

Of course you can make an arraylist out of dataobjects, add a random "Sort" value to it and sort by that.

If you want to work with an array you could do something like this right after creating the images array:

shuffle($images); //this shuffles the array.
$imageList = ArrayList::create(array_slice($images, 0, intval(trim($startPage->NumGallery))));
return $imageList;

note: don't use the same variable name for different things (images array, images ArrayList) in one method.

see: shuffle and array_slice man pages.

Cheers, wmk

Avatar
timo

Community Member, 47 Posts

13 April 2015 at 8:10am

Hi wmk!

Thats much cleaner!
thanks.timo.