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

From: Date: Mon, 29 Jul 2002 03:25:51 +0000
Subject: Re: [SMARTY] Quick question about checking form post variables / arrays
References: 1 2  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5489@lists.php.net to get a copy of this message
I'm not so sure it does. I've never used html_options, but from the docs, it appears to handle only single selections. 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> All done. Hope this helps. YMMV, of course. cheers, Travis Monte Ohrt wrote:
check out the html_options function, it does all this for you. Joao Prado Maia wrote:
I've been trying to figure out how to do what I'll explain here, but with no success. If anyone can shed some light into this it would be very appreciated. Ok, so in HTML you can just name your form elements field[] and that field is turned into an array automatically on your PHP script. What I have been trying to do is the following: <select name="category[]" multiple size="3"> {section name="y" loop=$cats} <option value="{$cats[y].cat_id}">{$cats[y].cat_title}</option> {/section} </select> But if there is any information missing on the form, I make the same form appear again to the user, with the information already filled out. While I can easily just do the following for text fields: <input type="text" name="name" value="{$smarty.post.name}"> I don't know how to make the select box above have the appropriate options selected by default. How do I check if that {$cats[i].cat_id} is inside the $smaty.post.category variable in Smarty ? Joao -- João Prado Maia <jpm@phpbrasil.com> http://phpbrasil.com - php com um jeitinho brasileiro -- Precisando de consultoria em desenvolvimento para a Internet ? Impleo - http://impleo.net/?lang=br -- Leia mais sobre os nossos serviços e produtos http://impleo.net/products.php?lang=br
-- Travis Low <mailto:travis@dawnstar.org> <http://dawnstar.org/travis>

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