{html_checkboxes}

    {html_checkboxes}是一个定制函数可以根据给定的数据来创建一个HTML勾选框组。也可以处理缺省选择哪个(或哪几个)项目。

属性名 类型 必需? 缺省 描述
namestringNocheckbox 勾选框组的名称
valuesarray Yes,除非使用options属性 n/a 勾选按钮的值的数组
outputarray Yes,除非使用options属性 n/a 勾选按钮输出的数组
selectedstring/arrayNoempty 选择的勾选框元素
optionsassociative array Yes,除非使用values和output n/a 值和输出的一个关联数组
separatorstringNoempty 分割每个勾选框项目的文本字符串
assignstringNoempty 将勾选框标记赋值给一个数组而不直接输出
labelsbooleanNoTRUE 在输出中增加<label>标记
assignstringNoempty 将输出赋予一个数组,每个元素对应每个勾选框的输出

例子8-9. {html_checkboxes}

<?php

$smarty
->assign('cust_ids', array(1000,1001,1002,1003));
$smarty->assign('cust_names', array(
                                
'Joe Schmoe',
                                
'Jack Smith',
                                
'Jane Johnson',
                                
'Charlie Brown')
                              );
$smarty->assign('customer_id'1001);

?>

    而模板为:

{html_checkboxes name='id' values=$cust_ids output=$cust_names
   selected=$customer_id  separator='<br />'}

    又或者PHP代码为:

<?php

$smarty
->assign('cust_checkboxes', array(
                                     
1000 => 'Joe Schmoe',
                                     
1001 => 'Jack Smith',
                                     
1002 => 'Jane Johnson',
                                     
1003 => 'Charlie Brown')
                                   );
$smarty->assign('customer_id'1001);

?>

    而模板为:

{html_checkboxes name='id' options=$cust_checkboxes
   selected=$customer_id separator='<br />'}

    上两例都将输出:

<label><input type="checkbox" name="id[]" value="1000" />Joe Schmoe</label><br />
<label><input type="checkbox" name="id[]" value="1001" checked="checked" />Jack Smith</label>
<br />
<label><input type="checkbox" name="id[]" value="1002" />Jane Johnson</label><br />
<label><input type="checkbox" name="id[]" value="1003" />Charlie Brown</label><br />

例子8-10. 数据库(PEAR或者ADODB):

<?php

$sql 
'select type_id, types from contact_types order by type';
$smarty->assign('contact_types',$db->getAssoc($sql));

$sql 'select contact_id, contact_type_id, contact '
       
.'from contacts where contact_id=12';
$smarty->assign('contact',$db->getRow($sql));

?>

    上面数据库查询的结果可以这样来输出:

{html_checkboxes name='contact_type_id' options=$contact_types
        selected=$contact.contact_type_id separator='<br />'}

    参见{html_radios}以及{html_options}