我正在为smarty模板分配一个名为$ users_criteria的数组。数组如下:
$users_criteria = array(
1 => array(
'label' => 'Registered users',
'condition' => 'date_range'
),
2 => array(
'label' => 'Logged-in users',
'condition' => 'date_range'
),
3 => array(
'label' => 'Not logged-in Users',
'condition' => 'date_range'
),
4 => array(
'label' => 'Never logged-in Users'
),
5 => array(
'label' => 'Package purchase',
'condition' => 'package_type'
),
6 => array(
'label' => 'Try demo packages',
'condition' => 'demo_packages'
)
);
$smarty->assign('users_criteria', $users_criteria);
$file_to_show = 'contact-list-import.tpl';
现在模板中有六个复选框,我想将上面数组中的标签值分配给每个复选框的value属性。也就是说,每个复选框都应该包含上述数组中的单个值。简而言之,我的要求是:上面数组中值为1的键应该映射到名为registered_users的复选框,上面数组中值为2的键应该映射到名为logged_in_users的复选框,......这样的方式类似到所有六个复选框。我试图实现这一点,但我得到的复选框的重复组合。我的代码如下:
{foreach from=$users_criteria key=myId item=criteria}
<tr height="30">
<td align="left" width="20%">
<input type="checkbox" class="user_checkbox" name="user_checkbox" id="registered_users" value="{$criteria.label}"/>Registered users
</td>
<td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
<td align="left" > <input type="text" style="width:100px;" class="inputfield" name="registered_users_from_date" id="registered_users_from_date" maxlength="10" width="20%"></td>
<td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
<td> <input type="text" style="width:100px;" class="inputfield" name="registered_users_to_date" id="registered_users_to_date" maxlength="10" width="20%"></td>
</tr>
<tr><td colspan="5"> </td></tr>
<tr height="30">
<td align="left" width="20%">
<input type="checkbox" class="user_checkbox" name="user_checkbox" id="logged_in_users" value="{$criteria.label}"/>Logged-in users
</td>
<td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
<td align="left" > <input type="text" style="width:100px;" class="inputfield" name="registered_users_from_date" id="logged_in_users_from_date" maxlength="10"></td>
<td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
<td> <input type="text" style="width:100px;" class="inputfield" name="registered_users_to_date" id="logged_in_users_to_date" maxlength="10"></td>
</tr>
<tr><td colspan="5"> </td></tr>
<tr height="30">
<td align="left" width="20%">
<input type="checkbox" class="user_checkbox" name="user_checkbox" id="not_logged_in_users" value="{$criteria.label}"/>Not logged-in Users
</td>
<td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
<td align="left" > <input type="text" style="width:100px;" class="inputfield" name="not_logged_in_users_from_date" id="not_logged_in_users_from_date" maxlength="10"></td>
<td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
<td> <input type="text" style="width:100px;" class="inputfield" name="not_logged_in_users_to_date" id="not_logged_in_users_to_date" maxlength="10"></td>
</tr>
<tr><td colspan="5"> </td></tr>
<tr height="30">
<td width="300" colspan="5">
<input type="checkbox" class="user_checkbox" name="user_checkbox" id="never_logged_in_users" value="{$criteria.label}"/>Never logged-in Users
</td>
</tr>
<tr><td colspan="5"> </td></tr>
<tr height="30">
<td width="300" colspan="1">
<input type="checkbox" class="user_checkbox" name="user_checkbox" id="package_purchase" value="{$criteria.label}"/>Package purchase
</td>
<td align="left" colspan="4">
<select name="test_pack_type_id" id="test_pack_type_id">
<option value="">All</option>
{if $all_type}
{foreach from=$all_type item="type"}
<option value="{$type.package_type_id}">{$type.package_type_name}</option>
{/foreach}
{/if}
</select>
</td>
</tr>
<tr><td colspan="5"> </td></tr>
<tr height="30">
<td width="300" colspan="5">
<input type="checkbox" class="user_checkbox" name="user_checkbox" id="try_demo_packages" value="try_demo_packages"/>Try demo packages
</td>
</tr>
<tr><td colspan="5"> </td></tr>
{/foreach}
你可以更正代码,以便在smarty中使用foreach为复选框指定正确的值吗?提前谢谢。
答案 0 :(得分:2)
您可以使用{section}
代码执行此操作。我给你举个例子。
{section name=i loop=$users_criteria}
<tr height="30">
<td align="left" width="20%">
<input type="checkbox" class="user_checkbox" name="user_checkbox" id="registered_users" value="{$users_criteria[i].label}"/>Registered users
</td>
<td valign="middle" align="left" width="10%"><b>From Date </b> : </td>
<td align="left" > <input type="text" style="width:100px;" class="inputfield" name="registered_users_from_date" id="registered_users_from_date" maxlength="10" width="20%"></td>
<td valign="middle" align="left" width="10%"><b>To Date </b> : </td>
<td> <input type="text" style="width:100px;" class="inputfield" name="registered_users_to_date" id="registered_users_to_date" maxlength="10" width="20%"></td>
</tr>
<tr><td colspan="5"> </td></tr>
{/section}