[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3140
  • Last Modified:

Smarty Multiselect

Is there a way to do a multi select box in Smarty? as the manual only seems to have standard drop down lists.


  • 3
  • 3
1 Solution
Short answer:The multi-select has to be done in the HTML/ template file, nothing special is needed from Smarty

More details:
Here is a fragment of one of my tpl files:
                          <select name="f_place[]" size="4" multiple class="liste" >
                        {html_options values=$LesLieux selected=$IDET_local_n output=$LesLieux}
The 2 important things here are the "[]" and the "multiple"
WARNING: you need to change the form-processing page as well, because now f_place is an ARRAY (maybe empty)

In the corresponding php file, I've put:
      $f_place=array(); $f_place=@$_REQUEST['f_place'];// or $_GET or $_POST if you mant more security
                $debug=true; // this in fact at the top of the file
                if ($debug) {echo "Places array: <pre>";print_r($f_place);echo "</pre>";}; // check the results
gabbs1Author Commented:

I found this post on the smarty forums but still can't get it to work so it is possible:


Just pass an array to "selected" and it should select all the vars but it doesn't work for me :(
gabbs1Author Commented:

Just got it working! phew! Yup you just pass an array ($productsAssigned) but I had [] at the end which didn't work for some reason and my array I was passing wasnt quite passing correctly but it works:

{html_options name="linkedProducts[]" class="inputbox" options=$products selected=$productsAssigned size="10" multiple="1"}
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

My code had a typo:
                         <select name="f_place[]" size="4" multiple class="liste" >
                        {html_options values=$LesLieux selected=$pr_f_place output=$LesLieux}

and somewhere in CALLING php
$my_smarty= new Smarty;
$pr_place[]='xxx'; // prepare values

[of course, you may decide to give the same name to f_place, pr_f_place and pr_place, which I have here put different on purpose]
gabbs1Author Commented:

Why do you use the <select></select> tags when you can do it all in smarty?
B-)) Not sure why I did it that way.
I don't remember if I HAD to, beacuse of some problem, or if I just did it that way so that the TPL file is more easily readable (at least for me)
Well, I remember now: smarty html_options generate additional values for each row, and I was looking for the "correct way" to remove this extra value, 'cause I had lots of them and the age was heavy (>500K).
No way. So I directly modified in php the corresponding smarty generator, so that this extra text was not generated - which saved me >200K
And when it worked, I did not change it!
This didn't help me until I realized that if I didn't explicitly assign the key, that key would default to the standard numeric integer...not only was it unable to match up the selected countries in the select form (in the example below, "$SelectedCountries" is exactly the same as "$CountryList" in order to prove the theory when you run the script), but those integers would be what would pass in the form rather than the name of the highlighted countries (since there's no ID logic assigned to each country in my application, this is not a desirable thing).

Just figured I'd post this just in case anyone else is banging their head against their desk :P

$GetCountryList = mssql_query("SELECT DISTINCT CountryName 
								FROM MyTable
								ORDER BY CountryName", $connect);
$GetCountryListNum = mssql_num_rows($GetCountryList);
	$CountryName = addslashes(mssql_result($GetCountryList,$i,'countryname'));
	$CountryList[$CountryName] = $CountryName;
	$SelectedCountries[$CountryName] = $CountryName;

Open in new window


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now