|
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 |
gjmacd Smarty Rookie
Joined: 25 Oct 2003 Posts: 13 Location: Boston
|
Posted: Sat Oct 25, 2003 4:13 pm Post subject: Complex array's and smarty. |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Oct 25, 2003 7:54 pm Post subject: |
|
|
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 |
|
gjmacd Smarty Rookie
Joined: 25 Oct 2003 Posts: 13 Location: Boston
|
Posted: Sat Oct 25, 2003 8:41 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Oct 25, 2003 10:07 pm Post subject: |
|
|
okay. so you don't need any recursion. this was not obvious by the unformatted print_r-output
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 |
|
gjmacd Smarty Rookie
Joined: 25 Oct 2003 Posts: 13 Location: Boston
|
Posted: Sun Oct 26, 2003 1:48 am Post subject: re: re-process array. |
|
|
Good advice to pre-process, thanks for the help. |
|
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
|