Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » [SOLVED] Random images in my pages

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

Page: 1
Go to End
Author Topic: 4174 Views
  • SImoX
    Avatar
    Community Member
    11 Posts

    [SOLVED] Random images in my pages Link to this post

    Hi all.
    I've built my website using SS and extending it with gallery module, customized on my need, and two other simple modules created by myself.
    In my structure, I choose to put a random-images area in each page of mine whole site. In this area I would show an html ul that contains images randomly taken from my whole image collection (GalleryPage 1, GalleryPage 2, ... ,GalleryPage N).
    The problem is I cannot figure out how to start creating the controller function code in my page class.
    My first idea was randomly querying single-imagefile table (I guess it's 'File' table...), place result in a DataObject and then control it via template. Now, how can I code these first two steps?
    If there's another way, more correct and more simple than this, please share it with me.

    Thanks in advance.

    bye

  • UncleCheese
    Avatar
    4085 Posts

    Re: [SOLVED] Random images in my pages Link to this post

    assuming you want it on every page, just put this in Page_Controller, otherwise, use a more specific one.

    function RandomImage()
    {
    return DataObject::get_one("Image","ParentID = $id_of_folder_containing_images",false, "RAND()");
    }

    and then on your template:

    <img srg="$RandomImage.URL" alt="" / >

  • SImoX
    Avatar
    Community Member
    11 Posts

    Re: [SOLVED] Random images in my pages Link to this post

    Thanks UncleCheese.

    I tried this solution and it works fine, but it's suited for choosing only one pic at a time and only in one gallery. I would like to have a list of images, browsable with "control function" in the template, created by adding, for instance, one image from gallery-one folder and one image from gallery-two folder...

    ty. Cya

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: [SOLVED] Random images in my pages Link to this post

    you can do something like this:

    return DataObject::get("Image","ParentID = $folder1_id OR ParentID = $folder2_id",false, "RAND()", "$NumberToReturn");

    You can do OR or AND filters in mysql which can return images from different folders. Apprently it's pretty resource heavy, but if its only a few images I can't imagine it would be a problem.

  • SImoX
    Avatar
    Community Member
    11 Posts

    Re: [SOLVED] Random images in my pages Link to this post

    Alright then...

    Let's focus on the context.

    This is my template code:

    <ul>
    <% control GalleryList %>
    <li><a rel="shadowbox[galleryTest];options={slideshowDelay: 4,counterType: 'default'}" href="$ViewLink" title="$Title" caption="$Content"><img src="$ThumbnailURL" ></a></li>
    <% end_control %>
    </ul>

    It has to show 5 random images. In order to achieve this, I think GalleryList function must return a value in this way:

    function GalleryList() {
    $photos = DataObject::get_one("GalleryPage");
    return ($photos->GalleryItems(5));
    }

    This script, at the moment, returns first 5 images from my first imagegallery. I want to edit this piece of code mantaining my template as it is now.
    Any ideas?

    Both solutions came from you would force me to make a massive editing of my template source but I'd like to avoid this...
    I would like also to build a solution that works out of the box, without the necessity of searching in my database for setting up right parent ID in my sql query every time I add a new gallery.

  • UncleCheese
    Avatar
    4085 Posts

    Re: [SOLVED] Random images in my pages Link to this post

    I see what you're saying.

    so..

    function RandomGalleryItems()
    {
    $items = $this->GalleryItems()->toArray();
    $random_images = new DataObjectSet();
    while($random_images->Count() <=5 ))
    {
    $random_index = rand(0,sizeof($items));
    $random_images->push($items[$random_index]);
    unset($items[$random_index]);
    }
    return $random_images;
    }

    That should work.

  • SImoX
    Avatar
    Community Member
    11 Posts

    Re: [SOLVED] Random images in my pages Link to this post

    Wow. We got it!

    I had to edit something but now I m quite at the end of this quest.
    At the moment I have N random images showed in my random images area in every page of my site. (N is a template-driven value. Images are taken, at the moment, only from the first gallery. Now I'll work for making this script randomly chooses images among all available galleries)

    I had to replicate and edit Items and GalleryItems functions (->AllItems and AllGalleryItems) in GalleryPage.php in order to make a selection of all images instead of a limited one. (I deleted all $limit entries in php and SQL code).
    Then my function became

    function RandomGalleryItems($list_dim) {
          $photos = DataObject::get_one("GalleryPage");
          $items = $photos->AllGalleryItems()->toArray();
          $random_images = new DataObjectSet();
          
          while($random_images->Count() <= ($list_dim-1) ) {
             $random_index = rand(0,sizeof($items)-1);
             $random_images->push($items[$random_index]);
          }
          
          return $random_images;
       }

    and my template became

    <ul>
    <% control RandomGalleryItems(5) %>
    <li><a rel="shadowbox[galleryTest];options={slideshowDelay: 4,counterType: 'default'}" href="$ViewLink" title="$Title" caption="$Content"><img src="$ThumbnailURL" ></a></li>
    <% end_control %>
    </ul>

    Thanks a lot, UncleCheese.

  • SImoX
    Avatar
    Community Member
    11 Posts

    Re: [SOLVED] Random images in my pages Link to this post

    Now It's finally over.

    I deleted

    AND `ParentID` = '{$this->FolderID}'

    in SQL Query in my new AllItems function in GalleryPage.php and now images are choosen randomly in every gallery available in my CMS.

    Thanks to all.

    Bye

    4174 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.