在 smarty 中分配变量?

发布于 2024-12-07 12:57:06 字数 1019 浏览 1 评论 0原文

我已将两个数组分配给 smarty : profilesselected_idprofiles 数组包含所有配置文件的数组,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。这意味着如果我想在 selectoption 中添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半边脸i 2024-12-14 12:57:07

我已经测试过这个,它有效。假设您的数组看起来像这样:

$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');

您用来访问变量和数组成员的语法不正确。这是工作解决方案:

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

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>
浅黛梨妆こ 2024-12-14 12:57:07

您可以使用以下代码。

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

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>
夏雨凉 2024-12-14 12:57:07
{assign var="name" value="Bob"}

参考:http://www.smarty.net/docs/en/language .function.assign.tpl

{assign var="name" value="Bob"}

Reference: http://www.smarty.net/docs/en/language.function.assign.tpl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文