{html_checkboxes}
{html_checkboxes} 是一个自定义函数,用于创建带有提供的数据的 HTML 复选框组。它还会处理默认情况下选择哪个项目。
属性
| 属性名 | 必需 | 描述 |
|---|---|---|
| name | 否 | 复选框列表的名称(默认为 'checkbox') |
| values | 是 | 复选框按钮的值数组 |
| output | 是 | 复选框按钮的输出数组 |
| selected | 否 | 选中的复选框元素,可以是字符串或数组 |
| options | 是 | 值和输出的关联数组,用于替代 values 和 output |
| separator | 否 | 用于分隔每个复选框项的文本字符串 |
| assign | 否 | 将复选框标签分配给一个数组,而不是输出到模板中 |
| labels | 否 | 在输出中添加 <label> 标签(默认为 true) |
| label_ids | 否 | 在输出中为 <label> 和 <input> 添加 id 属性(默认为 false) |
| escape | 否 | 转义输出/内容(值始终会被转义)(默认为 true) |
| strict | 否 | 仅在提供了布尔值 TRUE 或字符串 "disabled" 和 "readonly" 时设置 "extra" 属性的 "disabled" 和 "readonly"(默认为 false) |
-
必需的属性是
values和output,除非您使用options。 -
所有输出都符合 XHTML 标准。
-
所有不在上述列表中的参数都会以名称/值对的形式打印在每个创建的
<input>标签中。
示例
<?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',
[
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 />
<?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}。