All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

How do you auto-populate a select drop down with custom field data?

Tim Alford 10 May 2012 20:32 question, complete

Is there a way to auto-populate a select drop-down menu from custom field data in a low search form? Similar to how you would do this:

 
<select name="search:genre">
<option value="" {if genre ==""}selected="selected"{/if}>All Genres</option>
{option:genre}
<option value="{option_value}" {selected}>{option_name}</option>
{/option:genre}
</select>

Replies

  1. Low 11 May 2012 11:00

    The options for a select drop-down are stored in the DB but in an encrypted manner. This means you'll have to hard-code the options in your template, I'm afraid.

  2. Low 11 May 2012 14:31

    I've created a quick plugin to achieve what you want: Low Options.

  3. Tim Alford 11 May 2012 14:55

    Thanks for the fast response. Tried it but getting this error:

    Error
    The following tag cannot be processed:
    {exp:low_options:genre}
    Please check that the ‘low_options’ module is installed and that ‘genre’ is an available method of the module

    Checked and the plugin is installed. "Genre" is also the custom field short name.

    Here is the code:

     
    <select name="search:genre">
    <option value="" {if genre ==""}selected="selected"{/if}>All Genres</option>
    {exp:low_options:genre}
    <option value="{option:value}" {selected}>{option:label}</option>
    {/exp:low_options:genre}
    </select>
    </pre>

  4. Low 11 May 2012 14:57

    Ah, the plugin currently only works with EE2.5... I'll see if I can update it to work with older EE versions, too.

  5. Low 11 May 2012 15:10

    Could you try again? You'll need to change the syntax to:

    {exp:low_options:get field="genre"} 
    ...
    {/exp:low_options:get}


    And if you're using it inside the Low Search form, you'll need these conditionals:

    {exp:low_options:get field="genre"} 
    {if option:value == "{low_search_search:genre}"} selected="selected"{/if}
    {/exp:low_options:get}

  6. Tim Alford 11 May 2012 15:36

    Works great for the select EE's default "Select Dropdown" custom field type. Hopefully this is not too much to ask but is there any way it can also work for P&T Multiselect?

    Getting these errors on that one:

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined offset: 1
    Filename: libraries/Template.php
    Line Number: 3653
    A PHP Error was encountered
    Severity: Notice
    Message: Array to string conversion
    Filename: libraries/Template.php
    Line Number: 3726

    Probably because of the use of optgroups:

    Genre
     Sub-Genre
     Sub-Genre

    Looks like the optigroup comes in as "Array". All others genres list though.

    Maybe we should just hard code this.

  7. Low 11 May 2012 15:43

    Ah right, optgroups aren't supported now. I'll take a look.

  8. Low 14 May 2012 12:48

    Can you try the version that's up in GitHub now? Take note of the slightly different syntax to accommodate for optgroups.

  9. Tim Alford 14 May 2012 14:46

    Works like magic. Thank you.