0

我将一个数组从 PHP 分配给 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
)

现在在 smarty 模板中的表单上,如果找到匹配值,我想检查特定的复选框。但我无法以正确的方式解析数组。简而言之,如果子cl_user_type数组中的值与表单中存在的复选框的值进行数学运算,我希望选择复选框。在上述情况下,我希望选择最后两个复选框。我应该如何在 smarty 中写 if 条件?你能帮我实现这个目标吗?我在第一个条件下尝试了 if 但无法成功。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>
4

1 回答 1

1

你试过 smarty { html_checkboxes } 吗?如果由于某种原因你不能使用它,有两种解决方案,更好的一种是在将 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}
    />

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

{$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}/>
于 2013-08-01T15:15:34.837 回答