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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Random image from album


Go to End
Reply


14 Posts   2210 Views

Avatar
Samba Sam

Community Member, 85 Posts

16 February 2010 at 8:02am

Edited: 16/02/2010 8:15am

Thanks Banal! Works like a charm

So this is what I settled on:

In my TestimonialPage.php:

public function RandomTestimonial(){
   return DataObject::get_one('Testimonial','',true, 'RAND()');
   }

In my TestimonialPage.ss

<div id="Mainbody">$RandomTestimonial.TestimonialImage</div>

Thanks again everyone,
Sam

PS: What is the difference between using false and true in the above function?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

16 February 2010 at 8:25am

The third argument is for "cache".. I have no idea, really. I assume it means that if you run the same query again, if true, it will just look in memory for the result of the query rather than running it again. I always use false.

Avatar
Pigeon

Community Member, 243 Posts

16 February 2010 at 9:57am

Hmm, sorting by Rand() wont make use of query cache, where as my previous method will.

Alternatively you could just make the user put the images in a specific folder and then grab one at random with PHP. Very quick.

Avatar
UncleCheese

Forum Moderator, 4096 Posts

16 February 2010 at 10:23am

Yup! I agree, Pigeon. Just glob() it!!

Avatar
banal

Community Member, 901 Posts

16 February 2010 at 11:26am

I don't think you'll be looking at significant speed increases when DataObject count is below several hundred or even thousands. You can still tweak the method for performance when you identify this as bottleneck, otherwise it's just premature optimization.

Avatar
UncleCheese

Forum Moderator, 4096 Posts

16 February 2010 at 11:52am

Sigh.. Here comes banal again with the voice of reason. Come on, man! Don't ruin our fun! :)

Go to Top