Smarty Multiselect

Posted on 2006-05-18
Last Modified: 2010-05-18

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

Question by:gabbs1
    LVL 29

    Expert Comment

    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
    LVL 1

    Author Comment


    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 :(
    LVL 1

    Author Comment


    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"}
    LVL 29

    Accepted Solution

    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]
    LVL 1

    Author Comment


    Why do you use the <select></select> tags when you can do it all in smarty?
    LVL 29

    Expert Comment

    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!
    LVL 1

    Expert Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now