开发者

Smarty - Difficult if else to make select options

开发者 https://www.devze.com 2023-02-09 15:07 出处:网络
I am having real trouble getting my head around this smarty else if. For each {foreach $values as $value}

I am having real trouble getting my head around this smarty else if. For each

{foreach $values as $value}

 {if $value = a}
     do action
 {elseif $value = b}
     do action
 {elseif $value = c}
     do action
 {/if}

{/foreach}

I only want to 'do action' once for the entire foreach. For any one iteration of the foreach a differnt condition(a b or c) might be = to $value

The real world example that I am working on is adding 'selected' to a drop down option.

<select>
    {foreach $offices as $office}<!-- The if below is done so that the form is sticky, and when editing a post, it shows the post's current setting.-->

         <option 

         {if $smarty.post.OfficeCode == $office.OfficeCode}
              selected='selected'
         {elseif $bulletin_array[0].OfficeCode == $office.OfficeCode}
              selected='selected'
         {elseif $office.OfficeCode == $UsersOffice}
              selected='selected'
         {/if}                 

        title='{$office.Description}' value='{$office.OfficeCode}'>{$office.Title}  
</option>

    {/foreach}
</select>

the first if will be true if the form has been submited - to make the form sticky the second part will be true the form is being pre-filled when editing the record - ie prefil the form with开发者_运维百科 the current value the third part will be true if the user is creating a new record, in which case set it to a default option that the user usually uses. I need some way of only allowing selected to be added once. I php I would have a variable outside the foreach which was set to true if selected was added. I would then check that variable before doing the if elseif


{if ($smarty.post.OfficeCode eq $office.OfficeCode) 
  or ($bulletin_array[0].OfficeCode eq $office.OfficeCode) 
  or ($office.OfficeCode eq $UsersOffice)}
    selected='selected'
{/if}
0

精彩评论

暂无评论...
验证码 换一张
取 消