[英]Assign variable in smarty?
I have assigned two array to smarty : profiles
and selected_id
. 我给smarty分配了两个数组:
profiles
和selected_id
。 profiles
array contains the array of all profiles and the selected_id
array contains ids of the profiles to be displayed .So I am displaying the all profiles like this : profiles
数组包含所有配置文件的数组, selected_id
数组包含要显示的配置文件的id。因此,我正在显示所有配置文件,如下所示:
<select id="countries" class="multiselect" multiple="multiple" name="profiles[]">
{foreach name = feach item = k from = $profiles}
<option value="{$k->bz_pro_id}">{$k->bz_pro_first_name} {$k->bz_pro_last_name}</option>
{/foreach}
</select>
Now I want to default select the ids that are already selected by admin . 现在,我要默认选择admin已选择的ID。 That means if I want to add
selected = "selected"
in the option
of select
. 这意味着如果我想在
select
option
中添加selected = "selected"
。 For that I write : 为此,我写道:
{foreach name = feach item = k from = $profiles}
{foreach name = feach2 item = k2 from = $selected_id}
{if $k->bz_pro_id == $k2->bz_pro_id}
selected = "selected"
{/if}
{/foreach}
{/foreach}
So can I assign the select = "selected"
to a variable so that I can use it in the option
? 那么我可以将
select = "selected"
分配给变量,以便在option
使用它吗?
I have tested this, and it works. 我已经对此进行了测试,并且可以正常工作。 Assuming your arrays look something like this:
假设您的数组看起来像这样:
$profiles[] = array ( 'bz_pro_id' => '1', 'bz_pro_first_name' => 'test1', 'bz_pro_last_name' => 'test2');
$profiles[] = array ( 'bz_pro_id' => '2', 'bz_pro_first_name' => 'test3', 'bz_pro_last_name' => 'test4');
$selected_id = array('1');
The syntax you're using to access variables and array members isn't correct. 您用于访问变量和数组成员的语法不正确。 This is the working solution:
这是可行的解决方案:
<select id="countries" class="multiselect" multiple="multiple" name="profiles[]">
{foreach name=feach item=k from=$profiles}
<option value="{$k.bz_pro_id}"
{if in_array($k.bz_pro_id, $selected_id)}selected{/if}>
{$k.bz_pro_first_name} {$k.bz_pro_last_name}
</option>
{/foreach}
</select>
you can use following code. 您可以使用以下代码。
<select id="countries" class="multiselect" multiple="multiple" name="profiles[]">
{foreach name = feach item = k from = $profiles}
<option value="{$k->bz_pro_id}" {if $k->bz_pro_id|in_array($selected_id)}selected = "selected"{/if} >{$k->bz_pro_first_name} {$k->bz_pro_last_name}</option>
{/foreach}
</select>
{assign var="name" value="Bob"}
参考: http : //www.smarty.net/docs/en/language.function.assign.tpl
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.