Jump to:

23483 Posts in 18965 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Random Images from Gallery Albums

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

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

Page: 1
Go to End
Author Topic: 568 Views
  • ss-ss
    Avatar
    Community Member
    15 Posts

    Random Images from Gallery Albums Link to this post

    Hi,

    I am using the following code to fetch 6 random images from all the albums located in one gallery, but sometimes I am getting 3 random images, sometimes 4 and soon. It is not always 6 random images.

    What I am doing wrong?

    function GalleryImages() {
          $gallery = DataObject::get_one("ImageGalleryPage");
          $items = DataObject::get("ImageGalleryItem","ImageGalleryPageID = {$gallery->ID}","RAND()",null,6);
          return $gallery->GalleryItems(null,$items);
       }    

  • ss-ss
    Avatar
    Community Member
    15 Posts

    Re: Random Images from Gallery Albums Link to this post

    Anybody please???

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Random Images from Gallery Albums Link to this post

    Why are you doing return $gallery->GalleryItems(null,$items);
    and not just: return $items;

    Sorry if it's not a help, i just got curious :o)

  • novaweb
    Avatar
    Community Member
    115 Posts

    Re: Random Images from Gallery Albums Link to this post

    ss-ss,

    This is probably because RAND() is returning numbers outside of the range in which gallery items exist within your gallery.

    I would run:

    $items = DataObject::get("ImageGalleryItem","ImageGalleryPageID = {$gallery->ID}");

    To return all of your images, first.

    Then put the id's of the $items in to an array.

    Then use shuffle on the array ( http://nz.php.net/shuffle )

    Then inside a for loop (limited to 6), run DataObject::get_by_id inside the loop using the shuffled id's, append each result to a new DataObjectSet thenm return that at the end of your function.

    Good luck!

    568 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.