Smarty: check if a variable is in an array

I am using php with smarty. In php, I have two arrays:

$code = Array ( [n_id] => 1 [t_code] => ABC123 [t_description] => Test code [b_enabled] => Yes [n_type] => 3 [dt_start] => [dt_end] => [n_min_req_gbp] => 0 [n_min_req_usd] => 0 [n_amount_gbp] => [n_amount_usd] => [n_max_overall_gbp] => [n_max_overall_usd] => [n_extra] => 6 [b_reuse] => No [n_applications] => Array ( [0] => 2 ) ) 

and

 $all_application = Array ( [1] => New registration [2] => Mid-subscription upgrade [3] => Subscription renewal [4] => Additional purchase ) 

Note that the second array can - and will - grow, this is the reference data from which the n_applications array n_applications embedded in the n_applications array n_applications . That is, the array in n_applications will contain a subset of the keys from the $all_applications .

Now I assign these two arrays to a template:

 $template->assign('code', $code); $template->assign('apps', $all_applications); 

And in the template, I create a form for editing fields in the $code array. Everything works fine, except for the choice of "applications." I want to pre-select those applications that are already in the n_applications field. So, in my template, I have the following:

 <select name="c_apps[]" size="3" class="multiselect" multiple="multiple"> {foreach from=$apps key=k item=a} {assign var=v value=$k|@array_search:$code['n_applications']} <option value="{$k}"{if $v!==FALSE} selected="selected"{/if}>{$a|escape}</option> {/foreach} </select> 

However, this does not work as expected - and all options will be selected. I tried using the in_array function - but with the same result. What is the best way to achieve what I want?

+6
source share
3 answers

After a bit of a fight in all possible directions, I finally managed to pull it like this (smarty code only)

 <select name="c_apps[]" size="3" class="multiselect" multiple="multiple"> {foreach from=$apps key=k item=a} {if @in_array($k, $code.n_applications)} {assign var=v value=true} {else} {assign var=v value=false} {/if} <option value="{$k}"{if $v} selected="selected"{/if}>{$a|escape}</option> {/foreach} </select> 

And that did the trick.

+6
source

You can do it as follows:

 <select name="c_apps[]" size="3" class="multiselect" multiple="multiple"> {foreach from=$apps key=k item=a} <option value="{$k}"{if in_array($k, $code.n_applications)} selected="selected"{/if}>{$a|escape}</option> {/foreach} </select> 
+2
source

I did something similar several years ago and stumbled upon the same logical tasks.

My solution was to change the base array (in your case $all_applications ) by adding another key there (maybe ['opt_selected'] ). I left the default value blank, and for the data I would like to select, I changed the value, guess what ... selected="selected" .

This makes it easy to create a Smarty template:

 <option value="{$k}" {$a.opt_selected|default:''}>{$a|escape}</option> 

This may not be the best solution, but it helps to leave a lot of code from the template, where I usually do not want too much program logic.

Update

To meet the HTML part in your php code, you can simply mark the array:

 $all_applications['opt_selected'] = 1 

... and then configure Smarty as follows:

 <option value="{$k}" {if $a.opt_selected eq '1'}selected="selected"{/if}> {$a|escape} </option> 
0
source

Source: https://habr.com/ru/post/901290/


All Articles