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 @count with a multidimensional array in a loop

 
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
crazy8
Smarty n00b


Joined: 17 Dec 2008
Posts: 3

PostPosted: Thu Dec 18, 2008 12:32 am    Post subject: Using @count with a multidimensional array in a loop Reply with quote

I am trying to display the array size of a multi-dimensional array in a Smarty table. However, when using the {array() | @count} it returns Array.

Here's the setup:

First, in PHP two arrays are built:


Code:

<?php
// setup for Smarty and load arrays here
$smarty->assign('parkinglot', $parking);
$smarty->assign('vehicles', $vehicles);

$smarty->display('parkinginfo.tpl');
?>



Where $parking looks like:
Array(1)
1 => Array (3)
floors => "3"
sections => "9"
price => "1.5"

and $vehicles look like:
Array (2)
cars => null
trucks => Array (1)
1 => Array (3)
1 => "A12345"
2 => "B12345"
3 => "C12345"

In the smarty template I want to display a table that shows the number of cars and trucks in a separate table cell:

Code:

{ *Smarty *}
{foreach from=$parkinglot key="space" item="data"}
       
                <th>{$space}</th>                        {* Row number *}
                <td>{if isset($vehicles.cars.$space)}
                        {$vehicles.cars.$space | @count}
                    {else}
                        0
                    {/if}                   
                </td>    {* Cars *}
                <td>{if isset($vehicles.trucks.$space)}
                        {$vehicles.trucks.$space | @count}
                    {else}
                        0
                    {/if}
                </td> {* Trucks *}
        </tr>
{/foreach}


What's peculiar is that the isset($vehicles.trucks.$space) test seems to work just fine. So, my de-referencing of the array seems to work ok in that context, but trying to get count to operate on the same portion of the array results in Array.

Maybe count can't be passed an array where the last "leg" of the array is designated by a variable ($space)? (no errors from the parser, though...)

Any help here is greatly appreciated.

Thanks in advance...
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon Dec 29, 2008 8:54 am    Post subject: Reply with quote

What does
Code:
{$vehicles.trucks.$space|@print_r}

output?
_________________
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
crazy8
Smarty n00b


Joined: 17 Dec 2008
Posts: 3

PostPosted: Tue Dec 30, 2008 7:16 pm    Post subject: Reply with quote

It returns the text 'Array'.
Back to top
View user's profile Send private message
MidnightMotion
Smarty n00b


Joined: 08 Jan 2009
Posts: 1

PostPosted: Thu Jan 08, 2009 1:31 pm    Post subject: Reply with quote

You have to remove the spaces around the pipe sign:

{$vehicles.cars.$space|@count}

Instead of:

{$vehicles.cars.$space | @count}

(I like space too so I just ran into the same problem 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