Arrays 如何在smarty中检查数组中元素的特定元素是否存在?
我将从PHP向smarty模板分配一个数组,如下所示:Arrays 如何在smarty中检查数组中元素的特定元素是否存在?,arrays,if-statement,smarty,Arrays,If Statement,Smarty,我将从PHP向smarty模板分配一个数组,如下所示: $smarty->assign('data', $contact_list_user_data); <tr height="30" id="user_option"> <td width="300"> <input type="checkbox" id="users" name="cl_user_type[]" val
$smarty->assign('data', $contact_list_user_data);
<tr height="30" id="user_option">
<td width="300">
<input type="checkbox" id="users" name="cl_user_type[]" value="users" {if $data.cl_user_type=='users'}checked="checked"{/if}/>Users
</td>
<td> <input type="checkbox" id="upload_from_file" name="cl_user_type[]" value="upload_from_file" />Upload From File
</td>
<td>
<input type="checkbox" id="copy_paste_from_excel" name="cl_user_type[]" value="copy_paste_from_excel"/>Copy paste from excel
</td>
</tr>
该数组如下所示:
Array
(
[op] => import
[contact_list_id] => 9
[form_submitted] => yes
[cl_user_type] => Array
(
[0] => upload_from_file
[1] => copy_paste_from_excel
)
[registered_users_from_date] =>
[registered_users_to_date] =>
[logged_in_users_from_date] =>
[logged_in_users_to_date] =>
[not_logged_in_users_from_date] =>
[not_logged_in_users_to_date] =>
[test_pack_type_id] =>
[submit_value] => Submit
)
现在,在smarty模板中的一个表单上,如果匹配值为find,我希望选中特定复选框。但我无法以正确的方式解析数组。简言之,如果子数组cl\u user\u type
中的值与表单中存在的复选框的值匹配,我希望选中复选框。在上述情况下,我希望选中最后两个复选框。我应该如何在smarty中写入此条件的if条件?你能帮我实现这个目标吗。我在第一种情况下尝试了if,但并没有成功。
smarty模板中的代码如下所示:
$smarty->assign('data', $contact_list_user_data);
<tr height="30" id="user_option">
<td width="300">
<input type="checkbox" id="users" name="cl_user_type[]" value="users" {if $data.cl_user_type=='users'}checked="checked"{/if}/>Users
</td>
<td> <input type="checkbox" id="upload_from_file" name="cl_user_type[]" value="upload_from_file" />Upload From File
</td>
<td>
<input type="checkbox" id="copy_paste_from_excel" name="cl_user_type[]" value="copy_paste_from_excel"/>Copy paste from excel
</td>
</tr>
使用者
从文件上载
从excel复制粘贴
你试过smarty{}吗?如果出于某种原因您无法使用它,有两种解决方案,最好是在将cl_user_类型的数组发送给smarty之前对其进行修改,如下所示:
[cl_user_type] => Array
(
[upload_from_file] => true,
[copy_paste_from_excel] =>true
)
{$user_type = 'copy_paste_from_excel'}
<input type="checkbox" id="{$user_type}" name="cl_user_type[]" value="{$user_type}" {if $data.cl_user_type.$user_type}checked="checked"{/if}/>
然后在smarty代码中:
<input type="checkbox" id="upload_from_file" name="cl_user_type[]" value="upload_from_file" {if $data.cl_user_type.upload_from_file}checked="checked"{/if}/>
另一个(更糟)选项,使用foreach for every复选框:
<input type="checkbox" id="upload_from_file" name="cl_user_type[]" value="upload_from_file"
{foreach $data.cl_user_type as $type}
{if $type=='upload_from_file'}checked="checked"{/if}
{/foreach}
/>
作为旁注,我建议您使用一个变量,以便可以轻松地为不同的用户类型复制复选框。第一个解决方案如下所示:
[cl_user_type] => Array
(
[upload_from_file] => true,
[copy_paste_from_excel] =>true
)
{$user_type = 'copy_paste_from_excel'}
<input type="checkbox" id="{$user_type}" name="cl_user_type[]" value="{$user_type}" {if $data.cl_user_type.$user_type}checked="checked"{/if}/>
{$user\u type='copy\u paste\u from\u excel'}