Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

{html_options} Multiple Selected Options

 
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
Aristotle
Smarty Rookie


Joined: 10 Oct 2003
Posts: 8
Location: Phoenix, AZ

PostPosted: Tue Jan 13, 2004 3:31 am    Post subject: {html_options} Multiple Selected Options Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address MSN Messenger
Aristotle
Smarty Rookie


Joined: 10 Oct 2003
Posts: 8
Location: Phoenix, AZ

PostPosted: Tue Jan 13, 2004 4:06 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Jan 13, 2004 8:23 am    Post subject: Reply with quote

did you bother to check http://smarty.php.net/manual/en/language.function.html.options.php ?
the "selected"-attribute can be a string or an array.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Aristotle
Smarty Rookie


Joined: 10 Oct 2003
Posts: 8
Location: Phoenix, AZ

PostPosted: Tue Jan 13, 2004 11:49 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address MSN Messenger
Aristotle
Smarty Rookie


Joined: 10 Oct 2003
Posts: 8
Location: Phoenix, AZ

PostPosted: Tue Jan 13, 2004 11:52 pm    Post subject: Reply with quote

And it does...must have been a template syntax error or something. Sorry.
_________________
Aristotle
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address MSN Messenger
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