|
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 |
jaxyeh Smarty Rookie
Joined: 07 Aug 2003 Posts: 6
|
Posted: Thu Aug 07, 2003 3:34 pm Post subject: {html_options} functions along with array passed from DB |
|
|
Hello Guys,
I am starting to develop some huge php project along with smarty templates... However, I came up with one problem using {html_options} along with array that depends on DATABASE... Which means, array are not defined through by regular assigned method, but array retrieved through DATABASE. so I use SECTION LOOP to define array variables, but how can I define them without loop or using {html_options}??
I decided to do those follow method, using {section loop} above the {html_options}. It works fine. But I wasn't sure if its logical correction, so I am asking you guys who're expert with Smarty templates to observe and give me feedback on this method...
here 's PHP code:
[php:1:01a2a09dbf]
$query = "SELECT * FROM groups ORDER BY groupid ASC";
if ($groups = $db->array_query($query)) {
$smarty->assign('groups', $groups);
$smarty->assign('group_id', '2');
}
[/php:1:01a2a09dbf]
here's TPL code using {section}:
Code: |
{section name=i loop=$groups}
{html_options values=$groups[i].groupid selected=$group_id output=$groups[i].name}
{/section}
|
thanks!!
-Jax |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Aug 07, 2003 3:43 pm Post subject: |
|
|
hmm, htmloptions already takes *arrays* as parameters for values and output. i would suggest tweaking your db-result, so that smarty can handle them easily:
[php:1:6b2e921f4f]
if ($groups = $db->array_query($query)) {
$groups_opts = array();
foreach ($groups as $group) {
$groups_opts[$group['groupid']]=$group['name'];
}
$smarty->assign('groups_opts', $groups_opts);
$smarty->assign('group_id', '2');
}
[/php:1:6b2e921f4f]
after that you have a hash-array in your template mapping id->name.
that fits perfectly for {html_options}'s options-attribute:
Code: |
{html_options options=$groups_opts selected=$group_id}
|
no need for growing your own loops here
Last edited by messju on Thu Aug 07, 2003 3:59 pm; edited 1 time in total |
|
Back to top |
|
jaxyeh Smarty Rookie
Joined: 07 Aug 2003 Posts: 6
|
Posted: Thu Aug 07, 2003 3:55 pm Post subject: wow! |
|
|
That's the perfect answer I am looking for!!! THANKS!!
your an angel!
-jax |
|
Back to top |
|
mikeken Smarty Regular
Joined: 13 Aug 2003 Posts: 57 Location: MN
|
Posted: Wed Aug 20, 2003 3:02 am Post subject: |
|
|
are you guys using ezsql or whatever its called? |
|
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
|