ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Pre-populate a list with unique entires

June 15, 2011 6:18pm

Subscribe [3]
  • #1 / Jun 15, 2011 6:18pm

    bzhsteven

    3 posts

    Hello,

    I apologize in advance if this question have been already asked but I could not find it.

    My goal is to pre-populate a drop-down list to be used in a search form with values from a custom field. In the following example I try to pre-populate the drop down list with existing cities which has been already entered into a channel and specific custom field.

    <select id=“ville” name=“search:ville”>
    <option value=”“>Toute</option>
    {exp:channel:entries channel="biens"}
    <option value=”{ville}”>{ville}</option>
    {/exp:channel:entries}
    </select>

    (ville = city) btw

    This actually works, except for one very annoying thing. The list is populated with All entries of the custom field, even when they is more than one time a city. I would like to get only the uniques (distinct) values in order for a visitor to select possible cities in the DB but only see once each city in the list.

    I really hope somebody could help me on this issue.

    Thank you in advance,

    Steven

  • #2 / Jun 15, 2011 7:36pm

    Matt:P's avatar

    Matt:P

    277 posts

    Hi Steven

    You could create a separate Channel containing an Entry for each City. That way you have a set of unique Entries which you can use to populate your drop-down menu.

    Then, using relationship field type, relate your main Entries to one or more Entries from your City Channel.

    EE allows for one relationship per Relationship Custom Field. This is native to EE. If one City has been related-to in several different Entries from another Channel, then you can use EE’s native Reverse Related Entries to render everything related to that City.

    Does that help?

    Also, check out Playa for one-to-many relationships within a single Custom Field.

  • #3 / Jun 16, 2011 2:46am

    bzhsteven

    3 posts

    Helo Matt,

    Thank you for your quick answer. This seems to be an interesting solution, one question come to my mind. Will then this list of cities being created into a separated channel fixed? What I had in mind is when my customer would write a new real estate object, he would indicate the city, as well as all other informations. Then the list of cities would be automatically used in the drop-down. I would like to avoid having the customer creating first the city into a second place before he can enter his new real estate object.

    Of course if there is no technical other way your proposition will definitely help me.

    Best regards,

    Steven

  • #4 / Jun 16, 2011 8:41am

    Matt:P's avatar

    Matt:P

    277 posts

    What I had in mind is when my customer would write a new real estate object, he would indicate the city, as well as all other informations.

    You can use a ‘Select Dropdown’ fieldtype for this: http://ellislab.com/expressionengine/user-guide/cp/admin/content_admin/custom_fields_edit.html

    Then the list of cities would be automatically used in the drop-down.

    Does the above statement refer to the public website front-end or the CMS?

    I would like to avoid having the customer creating first the city into a second place before he can enter his new real estate object.

    Could yo clarify this for me please? Do you mean that you are trying to avoid the client creating the same city twice?

    Is this correct?

  • #5 / Jun 16, 2011 7:24pm

    bzhsteven

    3 posts

    Hello Matt,

    Thank you for following up on my problem. I obviously explained wrong my issue. What I intend to do is to allow my client (admin of his future site) to enter real estate objects (in kind of articles) and write down the city into an input field, because there could be so many cities and small town I wanted to avoid having him first populating a channel with cities.

    Later when a customer would go on his site he could search for objects with various criterias but also cities, with a list of those that have been associated to an object.

    In fact I discovered (I’m obviously a rookie here) that I could write down direct request, so I was going back to something more familiar to me, SQL. Here is the code I used to pre-populate my drop-down list for cities:

    <label for=“ville”>Ville</label>
    <select id=“ville” name=“search:ville”>
    <option value=”“>Toute</option>
    {exp:query sql="SELECT distinct field_id_18 FROM exp_channel_data "}
    <option value=”{field_id_18}”>{field_id_18}</option>
    {/exp:query}
    </select>

    field_id_18 being the custom field holding the info about the city

    I post it in case of someone new like me looks for a solution. But thanks again Matt for helping me and getting me discovering another aspect of this very great Expressionengine, the relationship and playa. For sure I’m gonna use it some day.

    Best regards,

    Steven

  • #6 / Aug 28, 2012 4:51am

    suvasishm

    4 posts

    Hi Matt,

    Thanks. It perfectly helps me!!!

  • #7 / Dec 12, 2014 4:23am

    siblify's avatar

    siblify

    3 posts

    Thanks - this saved me much time!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases