简体   繁体   中英

Smarty PHP Multi-Select for custom fields

I purchased a script built by someone who used smarty, it's a social network platform.

I have an issue with making the dropdown select field mulitiselect , and I didn't find a solution.

In the admin area, there's an option to add custom fields in the registration form, there are only three options : Textbox, text area, selectbox (screenshot: https://imgur.com/d7DuQZc

Once an option of that custom field is selected during the registration process, it will be seen in the profile page.

I can't seem to figure out how to make that field a multiselect one.

here's the code of the custom-field .tpl file

{elseif $custom_field['type'] == "selectbox"} 
               <select name="fld_{$custom_field['field_id']}" multiple="multiple" size="5"> 
                   <option {if $custom_field['value'] == ""}selected{/if} value="none">{__("Select")} {__($custom_field['label'])} </option> 
                   {foreach $custom_field['options'] as $id => $value} 
                       <option {if $custom_field['value'] == $value}selected{/if} value="{$id}">{$value}</option> 
                   {/foreach}

I added : multiple="multiple", the field change and i can select multiple options by holding ctrl , but i don't know if it's working or not knowing that i only see one selected option in the profile page.

here's the code in profile.php :

/* get custom fields */ $smarty->assign('custom_fields', $user->get_custom_fields( array("for" => "user", "get" => "profile", "node_id" => $profile['user_id']) ));

here's the code to show the selected options in profile.tpl :

{foreach $custom_fields['other'] as $custom_field}{if $custom_field['value']} {$custom_field['label']}{$custom_field['value']} </li> {/if} {/foreach}

Do you please have an idea how to be able to select more than one option and display the selected ones correctly?

Thank you for your help.

I think the problem is that after modifying your select to multiple you still consider a result of its work as a single value but not as an array. First, you need to change the name of select to name="fld_{$custom_field['field_id']}[]" which is mean that select can have a few values and it will send it to backend as an array of values. Second, you need to manage this array in your backend and save all its values. Third, you need to treat with it as with array in your front and {if $custom_field['value'] == $value} will not be a single value so you need to check if the $value is in the array $custom_field['value'] like {if $value|in_array($custom_field['value'])} . And I think everything will work as you expect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM