View previous topic :: View next topic |
Author |
Message |
Psyche Smarty Regular
Joined: 16 Apr 2006 Posts: 35
|
Posted: Tue Jul 17, 2007 6:09 pm Post subject: Using foreach with explode |
|
|
Hello,
I have the following entry in my database:
Quote: | 3_2007-07-17;5_2007-07-17;2_2007-07-17; |
Using foreach and explode how can I get result like:
Quote: |
$someVariabile = 3, $anotherVariable = 2007-07-17
$someVariabile2 = 5, $anotherVariable2 = 2007-07-17
$someVariabile = 2, $anotherVariable3 = 2007-07-17
|
I tried something but I can only get the first number, don't know how to get the date.
Thanks. |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Wed Jul 18, 2007 7:54 am Post subject: |
|
|
That's not really a Smarty related issue, is it?
Anyway, something along these lines should work for you:
Code: | $a = explode(';',$string);
foreach($a as $substring) {
$b = explode('_',$substring);
$someVariable[$b[0]] = $b[1];
} |
This creates such an array:
Code: | Array
(
[3] => 2007-07-17
[5] => 2007-07-17
[2] => 2007-07-17
) |
Of course you can do whatever you want with $b[0] and $b[1] in the loop _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
Psyche Smarty Regular
Joined: 16 Apr 2006 Posts: 35
|
Posted: Wed Jul 18, 2007 9:14 am Post subject: |
|
|
I see... But I have to do it with Smarty only. |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Wed Jul 18, 2007 10:28 am Post subject: |
|
|
Why? That's a job for PHP IMHO. _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
Psyche Smarty Regular
Joined: 16 Apr 2006 Posts: 35
|
Posted: Wed Jul 18, 2007 10:32 am Post subject: |
|
|
Because that row comes with another rows as a result of a query.
Using Smarty's foreach and explode I would like to build links like:
|
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Wed Jul 18, 2007 10:42 am Post subject: |
|
|
So, what speaks agains processing the data in PHP so Smarty can display it easily?
Btw, it is possible in Smarty, since you can use PHP function (such as explode) as modifiers, but I still think PHP is the better suited tool for that task. If you really wanna do it in Smarty, I'd suggest you write your own modifier, that gets a string and does exactly what the function in my first post does to it and returns the array.
http://smarty.php.net/manual/en/plugins.modifiers.php _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
Psyche Smarty Regular
Joined: 16 Apr 2006 Posts: 35
|
Posted: Wed Jul 18, 2007 11:00 am Post subject: |
|
|
Code: |
{assign var=analizeFirstTemp value=";"|explode:$p[p].tip_analiza}
{foreach item=nrAnaliza from=$analizeFirstTemp name=nrAnaliza}
{assign var=analizeSecondTemp value="_"|explode:$nrAnaliza}
{foreach item=analiza from=$analizeSecondTemp name=analiza}
{$analiza}
{/foreach}
{/foreach}
|
But it prints only the numbers |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Jul 19, 2007 9:29 am Post subject: |
|
|
I agree -- you should not have to parse passed in data in the template except, perhaps, using a modifier -- but even then, meh.
Okay, you *have* to. Assuming you just want to process the data and not actually build an array or assign variables, then (untested and off the top of my unsteady head):
Code: | {foreach from=$rs_dates item=raw_dates}
{* consider each raw_date as separated by ; *}
{foreach from=$raw_dates|explode:';' item=dateitem}
{* the following is an untested and poor (but expedient) hack -- consider using assign even if it unexpectedly works *}
{* print number then date *}
{$dateitem|explode:'_'|@array_shift} - {$dateitem|explode:'_'|@pop}
{/foreach}
{/foreach} |
The moral: do it in PHP . |
|
Back to top |
|
|