2013-08-01 72 views
0

我指定一个阵列从PHP给Smarty模板如下:如何在smarty中检查数组中元素的特定元素存在?

$smarty->assign('data', $contact_list_user_data); 

数组看起来如下:

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 
) 

现在智者模板的表单上我想使特定的复选框检查是否找到匹配值。但我无法以正确的方式解析数组。简而言之,如果子数组cl_user_type的值与表单中存在的复选框的值相匹配,我希望选中该复选框。在上面的情况下,我想要选择最后两个复选框。如果在smarty的条件下如何写?你能帮我实现这个目标吗?我尝试了如果在第一种情况下,但不能成功。 从Smarty的模板的代码如下:

<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>&nbsp;<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> 

回答

1

你试过智者{html_checkboxes}?如果由于某种原因,你不能使用它,有两种解决方案,是更好的一个modifiying发送之前的cl_user_type阵列给Smarty这样的:

[cl_user_type] => Array 
    (
     [upload_from_file] => true, 
     [copy_paste_from_excel] =>true 
    ) 

,然后在你的智者代码:

<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每一个复选框:

<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} 
    /> 

的旁注,我建议你使用一个变量,因此您可以轻松地复制为不同的U复选框ser类型。第一种解决方案如下所示:

{$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}/>