Home » Php » php – Smarty: check if variable is in array

php – Smarty: check if variable is in array

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m 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 may – and will – grow, this is the reference data, from which n_applications array field in the first array is built. That is, the array in n_applications will contain a subset of keys from the $all_applications arrays.

Now, I’m assigning these two arrays into the template:

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

And in the template, I’m creating a form for editing the fields in the $code array. Everything is working fine except the ‘applications’ selection. I want to pre-select those apps that are already in the n_applications field. So, in my template I have this:

<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 doesn’t work as expected – and ALL options end up being selected. I tried using in_array function – but with the same result. What’s the best way to achieve what I’m after?

How to&Answers:

After a bit of struggling in all possible directions, I finally managed to pull it off 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 this did the trick.

Answer:

You can do it like this:

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

Answer:

I’ve done something similar a few years back, and stumbled over the same logical challenge.

My solution was to modify the base array (in your case, $all_applications) while adding another key there (maybe ['opt_selected']). I left the default value empty, and for the data I wanted to have selected, I’ve changed the value to, guess what, … selected="selected".

This makes it rather easy for your Smarty template:

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

It might not be the best solution, but it helps leaving alot of code out of the template where I usually don’t want too much program logic.

Update

To counter having the HTML part in your php code, you might as well just flag the array:

$all_applications['opt_selected'] = 1

…and then arrange Smarty like this:

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