[smarty模板的作用]smarty模板中html_options用法介绍

更新时间:2017-07-11    来源:smarty模板    手机版     字体:

【www.bbyears.com--smarty模板】

先看看html_option函数参数表:

属性 类型 是否必须 缺省值 描述
values array 是,除非使用option属性 n/a 包含下拉列表各元素值的数组
output array 是,除非使用option属性 n/a 包含下拉列表各元素显示值的数组
selected string/array 否 empty 已选定的元素或元素数组
options associative array 是,除非使用value和output n/a 包含值和显示的关联数组
name string 否 empty 下拉菜单的名称

如果给定值是数组,那么会作为OPTGROUP 处理并显示,支持递归。

示例1:

 代码如下 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:

输出:

 代码如下

示例2:
post.php

 代码如下 $dba = dba();
$rs_categories = get_post_categories($dba); //这里用到了自己写的函数,取出所有
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);post.tpl

输出

 代码如下

模板部分

 代码如下

程序部分

 代码如下 $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"];
}


可见smarty的html_options接受的其实是一个数组,并且这个数组还是有点要求滴~
数组的键值将是option的value值,而数组的值则将是option的显示值,如果需要初始状态某个值是出于选择状态,那么

程序部分

 代码如下 $smarty->assign("customer_id", 1001);

模板部分

 代码如下


html_options还有一种用法,option的value和显示是分开的,这样方便处理其他的一些情况
具体用法如下
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:

 代码如下

本文来源:http://www.bbyears.com/jiaocheng/34022.html