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

Using foreach with explode

 
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
Psyche
Smarty Regular


Joined: 16 Apr 2006
Posts: 35

PostPosted: Tue Jul 17, 2007 6:09 pm    Post subject: Using foreach with explode Reply with quote

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
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Wed Jul 18, 2007 7:54 am    Post subject: Reply with quote

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 Smile
_________________
Darn computers always do what I tell them to instead of what I want them to do.
Back to top
View user's profile Send private message
Psyche
Smarty Regular


Joined: 16 Apr 2006
Posts: 35

PostPosted: Wed Jul 18, 2007 9:14 am    Post subject: Reply with quote

I see... But I have to do it with Smarty only.
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Wed Jul 18, 2007 10:28 am    Post subject: Reply with quote

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
View user's profile Send private message
Psyche
Smarty Regular


Joined: 16 Apr 2006
Posts: 35

PostPosted: Wed Jul 18, 2007 10:32 am    Post subject: Reply with quote

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:
Quote:
http://www.mysite.com/page.php?id=3&date=2007-07-17
http://www.mysite.com/page.php?id=5&date=2007-07-17
http://www.mysite.com/page.php?id=2&date=2007-07-17
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Wed Jul 18, 2007 10:42 am    Post subject: Reply with quote

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
View user's profile Send private message
Psyche
Smarty Regular


Joined: 16 Apr 2006
Posts: 35

PostPosted: Wed Jul 18, 2007 11:00 am    Post subject: Reply with quote

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 Sad
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jul 19, 2007 9:29 am    Post subject: Reply with quote

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 Wink.
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