Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Random Data in foreach loop

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
iMr
Smarty n00b


Joined: 07 Aug 2014
Posts: 2

PostPosted: Thu Aug 07, 2014 10:38 pm    Post subject: Random Data in foreach loop Reply with quote

Code:

function randata(){
   $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   $charslength = strlen($chars);
   $randomstring = '';
   for ($i = 0; $i < 15; $i++) $randomstring .= substr($chars, rand(0, $charslength - 1), 1);
   return $randomstring; 
}

{foreach $a->links as $l}
  <a href="link/{randata()}" onclick="trackClick('{randata()}');"></a>
{/foreach}


how to get the randata() to be the same value for each href and the onclick event but different for each loop so in output it would be like this:

Code:

<a href="link/erged4" onclick="trackClick('erged4');"></a>
<a href="link/evsdvd4" onclick="trackClick('evsdvd4');"></a>
<a href="link/sdcsaca" onclick="trackClick('sdcsaca');"></a>
<a href="link/asxasx" onclick="trackClick('asxasx');"></a>


Any help appriciated Very Happy
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Aug 08, 2014 2:38 am    Post subject: Reply with quote

As about as random as it gets from a one-liner:

Code:
{foreach $a->links as $l}
  {$rand = substr(str_shuffle(md5(microtime())), 0, 15)}
  <a href="/link/{$rand}">trackClick('{$rand}');"></a>
{/foreach}


output:

Code:

<a href="/link/68af70ed1bb3df4">trackClick('68af70ed1bb3df4');"></a>
<a href="/link/5fa171d45ef0d93">trackClick('5fa171d45ef0d93');"></a>
<a href="/link/7f6b1fd33bfb1da">trackClick('7f6b1fd33bfb1da');"></a>
Back to top
View user's profile Send private message Visit poster's website
iMr
Smarty n00b


Joined: 07 Aug 2014
Posts: 2

PostPosted: Fri Aug 08, 2014 1:32 pm    Post subject: Reply with quote

mohrt wrote:
As about as random as it gets from a one-liner:




That is more than perfect, exactly what i was looking for!
Thanks for your help!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Aug 11, 2014 9:43 pm    Post subject: Reply with quote

But generally speaking, I would not include application logic into template, if I were you.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP