Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Array within an array

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
insightcreative
Smarty n00b


Joined: 23 Apr 2012
Posts: 2

PostPosted: Mon Apr 23, 2012 8:22 am    Post subject: Array within an array Reply with quote

Hi

I have to change a bit of functionality on a site. At the moment, there is an array with about 8 items in it. I need to display the items in a certain order and also break into the array to display some other stuff first.

So, it's go something like:

2
3
1
Some other bits
8
5
6
7
4

It's someone else's code and was obviously built for flexibility but I don't need that so my plan is to display each element in the array manually rather than looping through it.

I've managed to do part of it:

Code:
{$setall.5.attr_value}


But some of the positions in the array are also arrays which I do need to loop through.

So, if position 5 is a value then I'm fine. But what if position 5 is another array? How can I loop through that to get all the values out no matter how many there are?

This is my first foray into smarty templates so any help would be gratefully received!

Best regards

Andy Jacobs
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4177
Location: Hamburg / Germany

PostPosted: Wed Apr 25, 2012 9:09 am    Post subject: Reply with quote

The following should do the job

Code:
{foreach $setall.5 as $value}
     {$value}
{/foreach}
Back to top
View user's profile Send private message
insightcreative
Smarty n00b


Joined: 23 Apr 2012
Posts: 2

PostPosted: Wed Apr 25, 2012 12:27 pm    Post subject: Still not right... Reply with quote

Hi

Thanks for that but it still seems to be one level out. If you have a look at:

http://j412.workinprogress.co.uk/component/jomdirectory/23-Friendship-Travel

You'll see that I've put your code in (just changed the 5 to a 0). Within that there is another array.

At position 3, it just says 'attr_value => "array"'. In there, is British Isles Europe as you can see at the top of the page.

I guess I want to get the array at position 3 of position 0 of the first array.

In my head, this should look like:

Code:
{foreach $setall.0.3 as $value}


But that doesn't work :/

The bit that displays the output at the top is like this:

Code:
{foreach from=$setall item=item}
           
            {if $item.req_type != 6}
            {if $item.attr_value}
            <div class="detailed_line" >
              <p>
                <label>{$item.name}</label>
                {$item.attr_value|JView:$item.attr_id:$data->id:$item.type:$item.req_type} </p>
            </div>
            {/if}
            {/if}
            {/foreach}


This displays everything apart from the long description. What I need to do is display the fields in the setall array in a certain order. I can do that.

BUT... I need to insert the long description part way through so it might go:

{$setall.2.attr_value}<br />
{$setall.4.attr_value}<br />
{$setall.3.attr_value}<br />
LONG Description
{$setall.1.attr_value}<br />
{$setall.5.attr_value}<br />

This works fine as long as {$setall.2.attr_value} isn't another array.

Best regards

Andy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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