Re: [SMARTY] Quick question about checking form post variables / arrays

From: Date: Mon, 29 Jul 2002 13:25:23 +0000
Subject: Re: [SMARTY] Quick question about checking form post variables / arrays
References: 1 2 3  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5494@lists.php.net to get a copy of this message
Travis Low wrote:
I'm not so sure it does. I've never used html_options, but from the docs, it appears to handle only single selections.
It will handle multiple select boxes, and can determine which ones are selected with an array of values. <select name="category[]" multiple size="3"> {html_options values=$cat_id output=$cat_title selected=$cat_selected} </select> $cat_id, $cat_title and $cat_selected are all arrays of values. Monte
Here's how I do it. Typically, my select drop-downs are populated from a database. So there would be a query against some table that would ultimately result in the $cats array mentioned below. To reproduce the multiple selections in the template, I would loop through the list of selections from the $_POSTed data and modify the $cats array as described below. In the PHP file, do something like this: $cats = mysql_fetch_array( mysql_query( "select * from cats" ) ); $selected_cats = $_POST["category"]; # NOTE: you can't use foreach here, it makes a COPY of each # array element, and you need to modify the ACTUAL element. for( $i = 0; $i < count( $cats ); $i++ ) {
    if( $selected_cats[$cats[$i]["cat_id"]] )
    {
        $cats[$i]["selected"] = "selected";
    }
} Then, in the template, do something like this: <select name="category[]" multiple size="3"> {section name="y" loop=$cats} <option value="{$cats[y].cat_id}" {$cats[y].selected}>{$cats[y].cat_title}</option> {/section} </select>


« previous php.smarty.general (#5494) next »