Introduction to Html_options usage in smarty templates

Source: Internet
Author: User

First look at the html_option function parameter table:

Whether the property type must be a default value description
Values array is, unless you use the Option property N/A array containing the values of each element of the Drop-down list
Output array is, unless you use the option attribute N/A an array containing the values for each element of the Drop-down list
Selected String/array no empty an array of elements or elements that have been selected
The options associative array is, unless you use value and output n/A to contain values and an associative array that is displayed
Name string No empty drop-down menu names

If the given value is an array, it is processed and displayed as Optgroup, which supports recursion.

Example 1:

The code is as follows Copy Code
index.php:
Require (' Smarty.class.php ');
$smarty = new Smarty;
$smarty->assign (' Cust_ids ', Array (1000,1001,1002,1003));
$smarty->assign (' Cust_names ', Array (' Joe schmoe ', ' Jack Smith ', ' Jane Johnson ', ' Carlie Brown '));
$smarty->assign (' customer_id ', 1001);
$smarty->display (' Index.tpl ');
INDEX.TPL:
<select name=customer_id>
{html_options values= $cust _ids selected= $customer _id output= $cust _names}
</select>

Output:

The code is as follows Copy Code
<select name=customer_id>
<option value= "1000" >joe schmoe</option>
<option value= "1001" selected= "selected" >jack smith</option>
<option value= "1002" >jane johnson</option>
<option value= "1003" >charlie brown</option>
</select>

Example 2:
post.php

The code is as follows Copy Code
$DBA = DBA ();
$rs _categories = get_post_categories ($DBA); Here you use the function you wrote to take out all the
Categoryforeach ($rs _categories as $key => $value) {
$categorie _ids[$key]= $value [' id ']; $categorie _names[$key]= $value [' title '];
}
$smarty = new Smarty ();
$smarty->assign (' Categorie_ids ', $categorie _ids);
$smarty->assign (' Categorie_names ', $categorie _names);p OST.TPL
<select id= "category" >
<{html_options values= $categorie _ids output= $categorie _names}>
</select>

Output

The code is as follows Copy Code
<select id= "category" >
<option value= "1" > Discount information </option>
<option value= "2" > Coupon </option>
</select>

Template section

The code is as follows Copy Code
<select name= "Select2" size= "4 id=" AllUser "style=" width:300px; height:200px "multiple=" multiple ">
<{html_options options= $sysUser}>
</select>

Program part

The code is as follows Copy Code
$sql _sysuser = "SELECT * from Tuser where tuser_status= ' 1 ' ORDER by Tuser_truename";
$rs _sysuser = $db->sql_query ($sql _sysuser);
while ($row _sysuser = $db->sql_fetchrow ($rs _sysuser)) {
$usersys [$row _sysuser[' tuser_name ']]= $row _sysuser[' tuser_truename '];
}


Visible Smarty's html_options accept is actually an array, and this array is still a bit of a request to drop ~
The key value of the array will be the value of option, and the value of the array will be the display of option, and if an initial state is required for a value that is out of the selection state, then

Program part

The code is as follows Copy Code
$smarty->assign (' customer_id ', 1001);

Template section

The code is as follows Copy Code
<select name=customer_id>
{html_options options= $cust _options selected= $customer _id}
</select>


Html_options There is also a use, option value and display is separate, so as to facilitate the handling of other situations
The specific usage is as follows
index.php:

The code is as follows Copy Code

Require (' Smarty.class.php ');
$smarty = new Smarty;
$smarty->assign (' Cust_ids ', Array (1000,1001,1002,1003));
$smarty->assign (' Cust_names ', Array (' Joe schmoe ', ' Jack Smith ', ' Jane
Johnson ', ' Carlie Brown '));
$smarty->assign (' customer_id ', 1001);
$smarty->display (' Index.tpl ');

INDEX.TPL:

  code is as follows copy code

<select Name=customer_id>
 {html_options values= $cust _ids selected= $customer _id output= $cust _names}
</ Select>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.