Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Include all images from folder

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

Page: 1 2
Go to End
Author Topic: 2924 Views
  • freewolny
    Avatar
    Community Member
    12 Posts

    Include all images from folder Link to this post

    Hi!

    How can I include all images uploaded to a folder?

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Include all images from folder Link to this post

    Hi Freewolny

    You need to make a function in your page controller that grabs all the images from the folder and returns them. Something like this:

    function GetImages(){

    return DataObject::get("Image", "ParentID = $FolderID");

    }


    Then in your template:

    <% control GetImages %>
    <img src="$URL" alt="$Name" >
    <% end_control %>

  • freewolny
    Avatar
    Community Member
    12 Posts

    Re: Include all images from folder Link to this post

    Thank you for your reply. I find it very useful. Unfortunately, I need some more information:

    Where and how to define (to name) the folder that contains the pictures to be grabbed?
    And
    Is it possible to publish all the photos from 3 different folders?

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Include all images from folder Link to this post

    The folder is identified by it's ID, so in the line DataObject::get("Image", "FolderID = $FolderID"); we need to replace $FolderID with the actual numeric ID of the folder you want to grab the images from.

    The esiest way to do this is to just hard code the number in there. To get the ID of the folder you can either use phpMyAdmin and look into the 'files' table, or you can go to the admin area of the CMS and open the 'Files & Images' tab. Then Hover over the folder and look at your status bar (bottom left of the browser), you will see the URL of the folder and on the end of it will be a number, this is the folder ID.

    The second way to do this is dynamically with another database call. So you could do something like this in your function:

    $FolderID = DataObject::get_one("Folder", "Name = SomeFolder")->ID;

    Then use $FolderID in the next DB call to the images as before.

    To get more than one folders images you can use the mysql OR operator to do something like this:

    return DataObject::get("Image", "ParentID = $Folder1 OR ParentID = $Folder2 OR ParentID=$Folder3");

  • freewolny
    Avatar
    Community Member
    12 Posts

    Re: Include all images from folder Link to this post

    Once again many thanx.

    Your suggested easiest way (with hard coding the number) is enough for me.

    Have a nice day aram!

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Include all images from folder Link to this post

    Thanks freewolny, your welcome

  • freewolny
    Avatar
    Community Member
    12 Posts

    Re: Include all images from folder Link to this post

    Hello again,
    It looks like I need some more help.
    I try to do it like this:

    function GetImages(){

    return DataObject::get("Image", "FolderID = 1");

    }


    and I have an error:

    [User Error] Couldn't run query: SELECT `File`.ID, `File`.ClassName, `File`.Created, `File`.LastEdited, `File`.Name, `File`.Title, `File`.Filename, `File`.Sort, `File`.ParentID, `File`.OwnerID, `File`.PopupWidth, `File`.PopupHeight, `File`.Embed, `File`.LimitDimensions, `File`.Caption, `File`.AlbumID, `File`.ID, if(`File`.ClassName,`File`.ClassName,'File') AS RecordClassName FROM `File` WHERE (FolderID = 1) AND (`File`.ClassName IN ('Image','Image_Cached')) ORDER BY Name Unknown column 'FolderID' in 'where clause'

    In the next step I would like to create 3 separate functions that get images from 3 different folders and include them to diffferent div's. The problem is this error above...

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Include all images from folder Link to this post

    oops my bad! Its supposed to be 'ParentID = 1' rather than 'FolderID = 1'

    I have corrected my posts above.

    Sorry about that!

    2924 Views
Page: 1 2
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.