|
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 |
Aristotle Smarty Rookie
Joined: 10 Oct 2003 Posts: 8 Location: Phoenix, AZ
|
Posted: Tue Jan 13, 2004 3:31 am Post subject: {html_options} Multiple Selected Options |
|
|
I REALLY need a way to have html_options take an array for the selected parameter.
index.php:
[php:1:a91b4338a0]<?php
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('cust_options', array(
1001 => 'Joe Schmoe',
1002 => 'Jack Smith',
1003 => 'Jane Johnson',
1004 => 'Charlie Brown'));
$smarty->assign('customer_ids', array(1001,1003));
$smarty->display('index.tpl');
?>[/php:1:a91b4338a0]
index.tpl:
Code: | <select name=customer_id>
{html_options options=$cust_options selected=$customer_ids}
</select> |
Result:
Code: | <select name="customer_id[]" multiple="multiple" size="3">
<option value="1000">Joe Schmoe</option>
<option value="1001" selected="selected">Jack Smith</option>
<option value="1002">Jane Johnson</option>
<option value="1003" selected="selected">Charlie Brown</option>
</select> |
Is there another plugin or method I can use to do this? _________________ Aristotle |
|
Back to top |
|
Aristotle Smarty Rookie
Joined: 10 Oct 2003 Posts: 8 Location: Phoenix, AZ
|
Posted: Tue Jan 13, 2004 4:06 am Post subject: |
|
|
Figured it out:
(plugin found in OurCMS Smarty package)
[php:1:dfe3a1df2c]<?php /* $Id$ */
/*
*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: html_options
* Purpose: Prints the list of <option> tags generated from
* the passed parameters
* -------------------------------------------------------------
*/
function smarty_function_html_options($params, &$smarty)
{
$print_result = true;
extract($params);
$html_result = '';
settype($selected, 'array');
if (isset($options)) {
settype($options, 'array');
// sort will work only with $options to keep key=>value association.
if (!empty($sort))
{
switch ($sort)
{
// sort by key
case "keys":
ksort($options);
break;
// sort by key reverse order
case "rkeys":
krsort($options);
break;
// sort by values keeping index association
case "vals":
asort($options);
break;
// sort by values keeping index association, by reverse order
case "rvals":
arsort($options);
break;
}
}
foreach ($options as $key => $value) {
$html_result .= "<option value=\"$key\"";
if (in_array($key, $selected))
$html_result .= " selected=\"selected\"";
$html_result .= ">$value</option>\n";
}
} else {
settype($output, 'array');
settype($values, 'array');
for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
/* By default, check value against $selected */
$sel_check = $values[$i];
$html_result .= "<option";
if ($i < count($values))
$html_result .= " value=\"".$values[$i]."\"";
else
$sel_check = $output[$i]; /* if more outputs than values, then
check output against $selected */
if (in_array($sel_check, $selected))
$html_result .= " selected=\"selected\"";
$html_result .= ">".$output[$i]."</option>\n";
}
}
if ($print_result)
print $html_result;
else
return $html_result;
}
/* vim: set expandtab: */
?>[/php:1:dfe3a1df2c]
This really should become a standard Smarty feature. _________________ Aristotle |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
|
Back to top |
|
Aristotle Smarty Rookie
Joined: 10 Oct 2003 Posts: 8 Location: Phoenix, AZ
|
Posted: Tue Jan 13, 2004 11:49 pm Post subject: |
|
|
Actually, I did see that. It just wasn't working for some reason, perhaps because the html_options was inside a {section} loop. That seems to goof things up at times. More than likely, though, it was human error, and something I did (typo?) was the reason for the problem. I must have fixed it sometime between trying the OurCMS plugin and fiddling around with other things, though...I'll try the same template with the standard Smarty plugin and see if it works. _________________ Aristotle |
|
Back to top |
|
Aristotle Smarty Rookie
Joined: 10 Oct 2003 Posts: 8 Location: Phoenix, AZ
|
Posted: Tue Jan 13, 2004 11:52 pm Post subject: |
|
|
And it does...must have been a template syntax error or something. Sorry. _________________ Aristotle |
|
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
|