Jump to:

10929 Posts in 2613 Topics by 1809 members

All other Modules

SilverStripe Forums » All other Modules » Get 5 random images from 3 latest galleries

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1549 Views
  • jezikk
    Avatar
    Community Member
    10 Posts

    Get 5 random images from 3 latest galleries Link to this post

    Hi folks,

    I have a problem with getting some random images from the latest galleries.
    I would like to have in my GalleryHolder template shown structure as this:
    <h2>Gallery Title no 1</h2>
    <img no=1>
    ...
    ...
    <img no=5>
    <p>$Content description</p>

    <h2>Gallery Title no 2</h2>
    <img no=1>
    ...
    ...
    <img no=5>
    <p>$Content description</p>

    etc....

    I made some code in my GalleryHolder.php

    function LatestGallery($limit=3, $limitIMG=5) {

    $sqlQuery = new SQLQuery();
    $sqlQuery->select = array("Gallery.ID AS GalleryID, Gallery.FolderID, File.FileName,SiteTree_Live.URLSegment, SiteTree_Live.Title");
    $sqlQuery->from = array("GalleryPage Gallery
    LEFT JOIN SiteTree_Live ON SiteTree_Live.ID = Gallery.ID
    LEFT JOIN File ON File.ID = Gallery.FolderID");
    $sqlQuery->where = array();
    $sqlQuery->orderby = "Gallery.ID DESC";
    $sqlQuery->limit = $limit;
    $rawSQL = $sqlQuery->sql();
    $result = $sqlQuery->execute();

    $doSet = new DataObjectSet();

    foreach($result as $k => $row) {
    $FolderID = $row['FolderID'];
    $Filename = $row['FileName'];
    $URL = $row['URLSegment'];
    $Title = $row['Title'];
    $sqlQuery2 = new SQLQuery();
    $sqlQuery2->select = array("*");
    $sqlQuery2->from = array("File");
    $sqlQuery2->where = array("ParentID=$FolderID");
    $sqlQuery2->orderby = "RAND()";
    $sqlQuery2->limit = $limitIMG;
    $rawSQL2 = $sqlQuery2->sql();
    $result2 = $sqlQuery2->execute();

    $ArraySet['URL'] = $URL;
    $ArraySet['Title'] = $Title;
    foreach($result2 as $a => $v) {
    $ArraySet["IMG_$a"] = $v['Filename'];

    }
    $doSet->push(new ArrayData($ArraySet));
    }

    return $doSet;
    }

    In my template I use <% control LatestGallery %> to get access to this DataObject.
    However, I have one big problem, and I cannot find a nice solution:
    - in my template I have an url to the original image, but how to manage thumbnail creation from the template. I would like to have image 120x80px shown in my template instead of original image 800x600px.

    My environment: SS 2.3.2 + Gallery Module.

    thx in advance

  • UncleCheese
    Avatar
    4085 Posts

    Re: Get 5 random images from 3 latest galleries Link to this post

    Wow. This is way too much code for what you're trying to do. Use the ORM that Silverstripe gives you! This stuff is totally unnecessary:

    $sqlQuery = new SQLQuery();
    $sqlQuery->select = array("Gallery.ID AS GalleryID, Gallery.FolderID, File.FileName,SiteTree_Live.URLSegment, SiteTree_Live.Title");
    $sqlQuery->from = array("GalleryPage Gallery
    LEFT JOIN SiteTree_Live ON SiteTree_Live.ID = Gallery.ID
    LEFT JOIN File ON File.ID = Gallery.FolderID");

    I don't know much about the Gallery module, but this is much easier to do with the ImageGallery module, which offers more robust uploading and management of your images with a more flexible API for what you're trying to do. Read more in the DataObjectManager forum.

  • jezikk
    Avatar
    Community Member
    10 Posts

    Re: Get 5 random images from 3 latest galleries Link to this post

    I decided to write an exact SQL as the DataObject give too much excess, especially when I have more the 100 galleries and ~10000 u/u a day.

    Is the a way to migrate galleries from old module Gallery to new ImageGallery ?
    Is the ImageGallery lighter than the old one ?
    Is the a simple way to do the task as I mentioned in my previous post.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Get 5 random images from 3 latest galleries Link to this post

    Yes, yes, and yes. The big difference between the ImageGallery and Gallery modules is that ImageGallery allows a gallery to have many albums. So you can manage all of your photos in one place, only sectioned into albums.

    Give it a try at http://dataobjectmanager.carlinowebdesign.com

    user: admin
    pass: password

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