For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > July 2004 > Re: [SMARTY] Newbie: probably dumb q about assoc arrays and {section}









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [SMARTY] Newbie: probably dumb q about assoc arrays and {section}
Jeroen De Jong

2004-07-04, 10:05 am

or with the same array, and a foreach loop:

{foreach key=key item=item from=$items}
<input name="{$key}" value="{$item.ItemName}">, {$item.ItemDesc}<br />
{/foreach}

Jeroen

----- Original Message -----
From: "Christopher J. Mackie" <cjmackie@princeton.edu>
To: <smarty-general@lists.php.net>
Sent: Sunday, July 04, 2004 1:28 AM
Subject: [SMARTY] Newbie: probably dumb q about assoc arrays and {section}


I'm missing something in the Help docs for using {section} with associative
arrays. I'm sure it's obvious, but I'm not seeing it--any help appreciated.

I've got an associative array:
$my_items = array(
ItemCode1 => array (
ItemName => Name1
ItemDesc => Desc1 )
ItemCode2 => array (
ItemName => Name2
ItemDesc => Desc2 )
...etc.
);

I want to create the following:

<input name="ItemCode1" value="Name1">, Desc1<br />
<input name="ItemCode2" value="Name2">, Desc2<br />
etc.

If I read the Help docs correctly, it should look like this:
--- items.php ---
$smarty = new Smarty;
....
$smarty->assign( 'items', $my_items );
$smarty->display( 'items.tpl' );
--- .items.tpl ---
{section name=item loop=$items}
<input name='$items[item]' value='$items[item].ItemName'>,
{$items[item].ItemDesc}<br />
{/section}

But clearly I *don't* read the Help docs correctly, b/c Smarty prints
nothing where any of the array values should be. The array is properly
filled--I've triple-checked. I've tried it with and without quotes around
the Smarty brackets--no difference.

Can anyone see what I am doing wrong?

Tx, --CJ

--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Mark Rogers

2004-07-04, 10:05 am

Jeroen,

A few things to look at here.

First, {section} does not work with assoc arrays; it requires your array to
have indexes 0, 1, 2, etc which yours does not. So I would not expect your
first example to work.

However, the solution is to use {foreach}, and your code here looks fine.

I note that you have PHP warnings turned off, which may mean that PHP is
trying to tell you something but you're missing it. I know they're turned
off, because

> $my_items = array(
> ItemCode1 => array (
> ItemName => Name1
> ItemDesc => Desc1 )
> ItemCode2 => array (
> ItemName => Name2
> ItemDesc => Desc2 )
> ...etc.
> );


... is not valid PHP - all the key and value strings should be quoted:
$my_items = array(
'ItemCode1' => array (
'ItemName' => 'Name1',
'ItemDesc' => 'Desc1' ),
'ItemCode2' => array (
'ItemName' => 'Name2',
'ItemDesc' => 'Desc2' )
...etc.
);

As it stands you're telling PHP to use the constant ItemName (as defined
with the define() function). Since it isn't defined PHP issues a warning and
assumes you mean 'ItemName' instead. (In this example single or double
quotes would work equally well.) Note also the missing commas, which suggest
that the quoted code is not in fact the same code you're trying to use - PHP
throws up more than a warning for that. (Unless of-course you have all
warnings and errors going to a log file which you're not looking at...)

So, in the absence of a visual scan telling me where the problem is, I'd
suggest looking at the error_reporting() function and seeing what that
throws up.

It would help to know: Are simpler Smarty constructs working fine? Do you
get any output at all from the template? Is the template even being found?

Mark Rogers
More Solutions Ltd
Mark Rogers

2004-07-04, 10:05 am

> Jeroen,
>
> A few things to look at here.


Oops, missed some attributions in there, damn email coming in out of
sequence doesn't help.

However, many of the comments still apply to Christopher.

Mark Rogers
More Solutions Ltd
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com