在 smarty 中分配变量?
我已将两个数组分配给 smarty : profiles
和 selected_id
。 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>
现在我想默认选择 admin 已选择的 id。这意味着如果我想在 select
的 option
中添加 selected = "selected"
。为此,我写道:
{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}
那么我可以将 select = "selected"
分配给一个变量,以便我可以在 option
中使用它吗?
I have assigned two array to smarty : profiles
and 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 :
<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 . That means if I want to add selected = "selected"
in the option
of select
. 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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经测试过这个,它有效。假设您的数组看起来像这样:
您用来访问变量和数组成员的语法不正确。这是工作解决方案:
I have tested this, and it works. Assuming your arrays look something like this:
The syntax you're using to access variables and array members isn't correct. This is the working solution:
您可以使用以下代码。
you can use following code.
参考:http://www.smarty.net/docs/en/language .function.assign.tpl
Reference: http://www.smarty.net/docs/en/language.function.assign.tpl