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

x
  • 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.

http://smarty.php.net/manual/en/language.function.html.options.php

Thanks
0
gabbs1
Asked:
gabbs1
  • 3
  • 3
1 Solution
 
fiboCommented:
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}
                          </select>
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
0
 
gabbs1Author Commented:

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

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=5260&highlight=multiple+select

Just pass an array to "selected" and it should select all the vars but it doesn't work for me :(
0
 
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"}
0
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.

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

and somewhere in CALLING php
$my_smarty= new Smarty;
.....
$pr_place=array();
$pr_place[]='xxx'; // prepare values
...
$my_smarty->assign('pr_f_place',$pr_place);

[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]
0
 
gabbs1Author Commented:

Why do you use the <select></select> tags when you can do it all in smarty?
0
 
fiboCommented:
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!
B-))
0
 
redcoreCommented:
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

<?php
$GetCountryList = mssql_query("SELECT DISTINCT CountryName 
								FROM MyTable
								ORDER BY CountryName", $connect);
$GetCountryListNum = mssql_num_rows($GetCountryList);
$i=0;
while($GetCountryListNum>$i)
	{
	$CountryName = addslashes(mssql_result($GetCountryList,$i,'countryname'));
	$CountryList[$CountryName] = $CountryName;
	$SelectedCountries[$CountryName] = $CountryName;
	++$i;
	}
 
?>

Open in new window

0

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