我正在为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';
现在,一个模板中有六个复选框,我想将上面数组中的标签值分配给每个复选框的值属性。也就是说,每个复选框都应该有一个来自上面数组的值。简而言之,我的要求是:上面数组中值为1的键应该映射到名称为registered_users的复选框,上面数组中的值为2的键应该映像到名称为logged_In_user的复选框,。。。以这种方式类似于所有六个复选框。我试图做到这一点,但我得到了复选框的重复组合。我的代码如下:
{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来更正代码以便为复选框分配正确的值吗?提前谢谢。
您可以使用{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}