views:

394

answers:

4

Hey Stackoverflow-Folks, is there any smart way to post random numbers (e.g. 1-4) in a list by using the smarty tpl-engine?

standart list sorted 1-5:

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

Here's my solution (PHP):

<ul>
{foreach from=randomNumbers}
<li>{smarty.randomNumbers}</li>
{/foreach}
</ul>

modified list sorted 1-5 (random):

<ul>
  <li>3</li>
  <li>2</li>
  <li>5</li>
  <li>1</li>
  <li>4</li>
</ul>

I've really tested nearly everything, but I do only need a smart & small solution for this :-)

Kind Regards, Heinrich

+1  A: 

You can use the rand() function from php in smarty. Pass the parameter as the number of times you run the loop and you should be done.

pinaki
+1  A: 
Tom Haigh
works well, but my output is still numeric and not random
Heinrich
@Heinrich: what output are you getting?
Tom Haigh
by using your code snippet, I get a clean numeric output: 1,2,3,4,5, not a randome one like 5,4,2,1,3 or 4,1,2,3,5 etc. :-/
Heinrich
@Heinrich: weird. it works for me. i guess you've tried everything like clearing templates_c/
Tom Haigh
yes, but I still get 1,2,3,4,5 :-(, do I need a specific smarty.plugin to run your code-snippet correctly ?
Heinrich
A: 

thank you tom !

works well, but my output is still numeric and not random like this:

<ul>
  <li>5</li>
  <li>2</li>
  <li>4</li>
  <li>1</li>
  <li>3</li>
</ul>

or

<ul>
  <li>4</li>
  <li>1</li>
  <li>5</li>
  <li>3</li>
  <li>2</li>
</ul>

etc...

hope you can help me out :)

Heinrich

Heinrich
A: 

ok, it seems to be a PHP-Problem. Since the last PHP versions, array_rand isn't random any more. I've tried to add something like this

    |shuffle

or

    shuffle();

to tom's smarty-code,

but without success >_<

...

Heinrich
unfortunately that won't work because shuffle modifies the passed array and returns a bool
Tom Haigh