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

Complex array's and smarty.

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
gjmacd
Smarty Rookie


Joined: 25 Oct 2003
Posts: 13
Location: Boston

PostPosted: Sat Oct 25, 2003 4:13 pm    Post subject: Complex array's and smarty. Reply with quote

Here's an output of my array from print_r();

Array ( [0] => Array ( [data] => Array ( [0] => Array ( [element_name] => departments [data] => Array ( [0] => Array ( [element_name] => department [data] => Array ( [0] => Array ( [element_name] => deptid [data] => 1073 ) [1] => Array ( [element_name] => imagename [data] => INTROB.BMP ) [2] => Array ( [element_name] => description [data] => Powder ) ) ) [1] => Array ( [element_name] => department [data] => Array ( [0] => Array ( [element_name] => deptid [data] => 1074 ) [1] => Array ( [element_name] => imagename [data] => PINWHEEL2.JPG ) [2] => Array ( [element_name] => description [data] => Peanut Butter Bars ) ) ) [2] => Array ( [element_name] => department [data] => Array ( [0] => Array ( [element_name] => deptid [data] => 1075 ) [1] => Array ( [element_name] => imagename [data] => PB BARS4.JPG ) [2] => Array ( [element_name] => description [data] => Chocolate Bars ) ) ) [3] => Array ( [element_name] => department [data] => Array ( [0] => Array ( [element_name] => deptid [data] => 1076 ) [1] => Array ( [element_name] => imagename [data] => PINWHEEL3.JPG ) [2] => Array ( [element_name] => description [data] => Mixed Bars ) ) ) ) ) ) ) )

I'm a little confused as how I'd be able to use "assign", then write a template that loops though and deals with the elements of the array.

of course, in my code, I've got :

$smarty->assign ("departments", $theArray);

In my template, I've tried a foreach, but i'm tripped up:

{foreach name=outer item=element_name from=$departments}
{foreach key=key item=item from=$department}
{$key}: {$item}<br>
{/foreach}
{/foreach}

This spits out zippo...

I know this is a fairly complex array, but it's being generated from an XML tree which I don't have too much control over.

Anyone have any suggestions?

I'm new to smarty, and I'm just trying to figure all this out and see if it'll help us with our product.

thanks
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Oct 25, 2003 7:54 pm    Post subject: Reply with quote

the question is, what do you want to do with that structure? do you really want to recurse arbitrarily deep into it?

maybe this thread about advanced recursion is of interest for you.

one mistake in you code is, your looping over $department" which isnt't defined. you may loop over $element_name, but not recursively without {include} or other tricks.

again: it depends very much on what you want to accomplish with your data.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
gjmacd
Smarty Rookie


Joined: 25 Oct 2003
Posts: 13
Location: Boston

PostPosted: Sat Oct 25, 2003 8:41 pm    Post subject: Reply with quote

Thanks for the reply.

Basically, I have a routine that we use called xml2array.
The xml passed to the routine is:

<departments>
<department>
<deptid>1010</deptid>
<imagename>foo.jpg</imagename>
<description>the description</description>
</department>
<department>
<deptid>1010</deptid>
<imagename>foo.jpg</imagename>
<description>the description</description>
</department>
</departments>

a print_r() of the array that's return from xml2array is as follows:

Array ( [0] => Array ( [data] => Array ( [0] => Array ( [departments] => departments [data] => Array ( [0] => Array ( [department] => department [data] => Array ( [0] => Array ( [deptid] => deptid [data] => 1073 ) [1] => Array ( [imagename] => imagename [data] => INTROB.BMP ) [2] => Array ( [description] => description [data] => ProFibe Powder ) ) ) [1] => Array ( [department] => department [data] => Array ( [0] => Array ( [deptid] => deptid [data] => 1074 ) [1] => Array ( [imagename] => imagename [data] => PINWHEEL2.JPG ) [2] => Array ( [description] => description [data] => ProFibe Peanut Butter Bars ) ) ) [2] => Array ( [department] => department [data] => Array ( [0] => Array ( [deptid] => deptid [data] => 1075 ) [1] => Array ( [imagename] => imagename [data] => PB BARS4.JPG ) [2] => Array ( [description] => description [data] => ProFibe Chocolate Bars ) ) ) [3] => Array ( [department] => department [data] => Array ( [0] => Array ( [deptid] => deptid [data] => 1076 ) [1] => Array ( [imagename] => imagename [data] => PINWHEEL3.JPG ) [2] => Array ( [description] => description [data] => ProFibe Mixed Bars ) ) ) ) ) ) ) )

What i'm trying to do is use smarty to traverse through the array that I pass into the "assign" (the array above). Clearly, I would love to address the elements of the array by their associative names and make it easy -- but i'm having trouble even traversing through the darn array to product a printout in smarty.

thanks for the help
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Oct 25, 2003 10:07 pm    Post subject: Reply with quote

okay. so you don't need any recursion. this was not obvious by the unformatted print_r-output Smile

i would preprocess the array in php. make an array $departments (with keys from 0...n) where each element is a hash-array array('deptid'=>..., 'description'=>..., ...). then assign this $department to smarty.

in the template you can loop over the departments:
{foreach from=$departments item=department}
{$department.deptid}: {$department.description} ...
{/foreach}

it's much easier to handle that in the template than your raw-xml-converted-array IMHO.

HTH
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
gjmacd
Smarty Rookie


Joined: 25 Oct 2003
Posts: 13
Location: Boston

PostPosted: Sun Oct 26, 2003 1:48 am    Post subject: re: re-process array. Reply with quote

Good advice to pre-process, thanks for the help.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
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 -> Smarty Development 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