|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
iMr Smarty n00b
Joined: 07 Aug 2014 Posts: 2
|
Posted: Thu Aug 07, 2014 10:38 pm Post subject: Random Data in foreach loop |
|
|
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 |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Aug 08, 2014 2:38 am Post subject: |
|
|
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 |
|
iMr Smarty n00b
Joined: 07 Aug 2014 Posts: 2
|
Posted: Fri Aug 08, 2014 1:32 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Aug 11, 2014 9:43 pm Post subject: |
|
|
But generally speaking, I would not include application logic into template, if I were you. |
|
Back to top |
|
|
|
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
|