Using (or not) associative arrays
| From: | Per Nystrom | Date: | Thu, 06 Mar 2003 01:48:23 +0000 |
| Subject: | Using (or not) associative arrays | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-8422@lists.php.net to get a copy of this message | ||
Hi,
I'm a relative newbie to PHP and Smarty, so please forgive me if this has
all been hashed out already. I've read through some of the posts about this,
and I've come to understand that Smarty templates can't retrieve the key
values in associative arrays, so I have to flatten them. If my
understanding so far is correct, then what I'm trying to do is probably a
nightmare in Smarty.
I want to create a template that generates tabular forms, based
on field and attribute information stored in array(s). Since not all
cells will have the same attributes, or even the same number of
attributes, the first solution I thought of was something like this:
<?php
// index.php
$form = array('userid' => array(
'fieldname' => 'Username',
'type' => 'text',
'size' => '10',
'maxlength' => '80'),
'password' => array(
'fieldname' => 'Password',
'type' => 'password',
'size' => '10',
'maxlength' => '80'));
$smarty = new Smarty;
$smarty->assign('form', $form);
$smarty->display('tabular_form.tpl');
?>
{* tabular_form.tpl *}
<table border="1" cellspacing="2" cellpadding="5">
<form action="action.php" method="POST">
<p>
<tr>
{section name=curr_field loop=$form}
{* step through fields *}
{section}
{* step through attributes *}
{/section}
{/section}
{* I haven't even gotten to considering table rows yet!! *}
</tr>
</table>
Of course, I now know that it's impossible to get the key values out of
associative arrays in Smarty, so I'm looking at something much more
hideous:
<?php
// index.php
// I know there is a way to take the original $form
// array and flatten it using each() or list() or
// something, but I haven't yet learned enough to adapt
// the code I found in the newsgroup to my purposes.
$form_fields[] = 'userid';
$form_attr_name[0][] = 'name';
$form_attr_value[0][] = 'Username';
$form_attr_name[0][] = 'type';
$form_attr_value[0][] = 'text';
$form_attr_name[0][] = 'size';
$form_attr_value[0][] = '10';
$form_attr_name[0][] = 'maxlength';
$form_attr_value[0][] = '80';
$form_attr_name[0][] = 'value';
$form_attr_value[0][] = '';
$form_fields[] = 'password';
$form_attr_name[1][] = 'name';
$form_attr_value[1][] = 'Password';
$form_attr_name[1][] = 'type';
$form_attr_value[1][] = 'password';
$form_attr_name[1][] = 'size';
$form_attr_value[1][] = '10';
$form_attr_name[1][] = 'maxlength';
$form_attr_value[1][] = '80';
$form_attr_name[1][] = 'value';
$form_attr_value[1][] = '';
$smarty = new Smarty;
$smarty->assign('form_fields', $form_fields);
$smarty->assign('form_attr_name', $form_attr_name);
$smarty->assign('form_attr_value', $form_attr_value);
$smarty->display('tabular_form.tpl');
?>
I'm sure that others have faced this before, and there has to
be a better way. I'm wondering now if it's even worth it to try to make a
generic form template. Has anybody found an elegant solution to this?
Thanks,
Per