/    Sign up×
Community /Pin to ProfileBookmark

Select ONE field from ALL records

Hey there,

I am trying to build a “search dropdown” feature.
basically what I am trying to do is go through my database collecting the ‘type’ field from all records, then putting each type in a dropdown menu.

Also, there are obviously more than one instance where the type is the same as another (ie: a place is a bar, so is another place) how can I get this to only put one “bar” in the dropdown?

Please help!
Thanks,
Josh

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 26.2013 — You can use the "DISTINCT" modifier in your query so that you eliminate duplicates.

If feasible, a more robust solution would be a separate table that defines the possible values for these values, then in the table with the actual data you just reference the primary key of this new table (i.e. use a foreign key). This eliminates the worry that one record has a value of "Bar", another "a bar", and yet another has "pub". The downside is you need to keep this table updated with possible values. I suppose another possibility would be to make the field in the main table an ENUM type and not bother with the 2nd table, though that still requires the need to update the allowed ENUM types as your needs change.
×

Success!

Help @VaMoose spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...