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.

Template Questions /

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

[SOLVED] Random images in my pages


Go to End


8 Posts   6043 Views

Avatar
SImoX

Community Member, 11 Posts

11 January 2009 at 5:34am

Edited: 13/01/2009 4:48am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

11 January 2009 at 9:18am

Edited: 11/01/2009 9:18am

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="" / >

Avatar
SImoX

Community Member, 11 Posts

12 January 2009 at 1:48am

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

Avatar
Carbon Crayon

Community Member, 598 Posts

13 January 2009 at 2:51am

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.

Avatar
SImoX

Community Member, 11 Posts

13 January 2009 at 3:18am

Edited: 13/01/2009 3:24am

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.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

13 January 2009 at 3:50am

Edited: 13/01/2009 3:51am

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.

Avatar
SImoX

Community Member, 11 Posts

13 January 2009 at 4:34am

Edited: 13/01/2009 4:37am

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.

Avatar
SImoX

Community Member, 11 Posts

13 January 2009 at 4:48am

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