A quick trick to display a random image

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

A quick trick to display a random image

Post by calguy1000 »

Given images named header_1.jpg, header_2.jpg, ... header_10.jpg this code will pick a random one
on every page load.

Code: Select all

{capture assign='num'}{1|rand:10}{/capture}{* generate a random number between 1 and 10, assign it to 'num *}
<img src="uploads/header_{$num}.jpg" width='468' height='50'/>
Last edited by calguy1000 on Sat Oct 06, 2007 7:44 pm, edited 1 time in total.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
KO
Power Poster
Power Poster
Posts: 562
Joined: Mon Nov 06, 2006 7:55 pm
Location: Helsinki, Finland

Re: A quick trip to display a random image

Post by KO »

I love these tips! And javascript alternatives like this.

Thanks,

K
Pierre M.

Re: A quick trip to display a random image

Post by Pierre M. »

KO wrote: I love these tips! And javascript alternatives like this.
The power of this tip is its server side non javascript nature. And shortness.
Pierre M.
KO
Power Poster
Power Poster
Posts: 562
Joined: Mon Nov 06, 2006 7:55 pm
Location: Helsinki, Finland

Re: A quick trip to display a random image

Post by KO »

Yeah... that's the cool thing with it. It's just so easy for everyone copy javascript code from somewhere and stick it in wishing people don't have noscript enabled. It's amazing to see what kind of crap high profile companies and their web pages put out without javascript.
streever

Re: A quick trick to display a random image

Post by streever »

Hi all,

I think the poster KO was saying, thanks for an alternative to using javascript :D! You two are in agreement, maybe it is language barrier? :D
Pierre M.

Re: A quick trick to display a random image

Post by Pierre M. »

Yes, my mistake. You are right : rereading KO, I understand now he means "an alternative to using javascript" as you say. Thank you for the language clarification.

Pierre
KO
Power Poster
Power Poster
Posts: 562
Joined: Mon Nov 06, 2006 7:55 pm
Location: Helsinki, Finland

Re: A quick trick to display a random image

Post by KO »

:) No worries. No one's been offended. English is not my mother language and I write total garbage most of the time anyway.

Still nice solution and thanks to Calguy!
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm
Location: Raalte, the Netherlands

Re: A quick trick to display a random image

Post by RonnyK »

Based on this tip from Calguy1000, the variant to create the random background for the header-container....

This should be put in the template.

Replace the:

Code: Select all

<div id="header">
With:

Code: Select all

{capture assign='num'}{1|rand:10}{/capture}
<div id="header" style="background-image: url(uploads/header_{$num}.jpg)">
And dont forget to remove the background image for the header, in the stylesheet, as it already is called here.

Ronny
Last edited by RonnyK on Wed Jan 16, 2008 6:10 pm, edited 1 time in total.
cnymike
Power Poster
Power Poster
Posts: 446
Joined: Sun Jan 22, 2006 3:24 am

Re: A quick trick to display a random image

Post by cnymike »

OK, I'm a little dense, so I think I need a bit more information...

what is this 'capture'... php?
hexdj
Power Poster
Power Poster
Posts: 415
Joined: Sat Mar 24, 2007 8:28 am

Re: A quick trick to display a random image

Post by hexdj »

I haven't tried but I believe you just stick that {capture... } smarty tag in your page template and it should work.
cnymike
Power Poster
Power Poster
Posts: 446
Joined: Sun Jan 22, 2006 3:24 am

Re: A quick trick to display a random image

Post by cnymike »

Yes, it works...but why? What is it?
hexdj
Power Poster
Power Poster
Posts: 415
Joined: Sat Mar 24, 2007 8:28 am

Re: A quick trick to display a random image

Post by hexdj »

it displays the images (which you need to have somewhere in advance)
cnymike
Power Poster
Power Poster
Posts: 446
Joined: Sun Jan 22, 2006 3:24 am

Re: A quick trick to display a random image

Post by cnymike »

Guys, I know what it's supposed to do and I know that I just need to put the code in the template. What I want to know is what is 'capture'... is it a php function, or what?
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: A quick trick to display a random image

Post by calguy1000 »

see: http://smarty.php.net/manual/en

Required reading for CMS users.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
cnymike
Power Poster
Power Poster
Posts: 446
Joined: Sun Jan 22, 2006 3:24 am

Re: A quick trick to display a random image

Post by cnymike »

Thank you.
Post Reply

Return to “Tips and Tricks”