Skip to Content Skip to Menu

🌲 Merry Christmas! Great savings on Professional and Developer Memberships! Get 25% off now with code XMAS-2024!

[SOLVED] Auto populated Drop Down Field

  • clickmo
  • clickmo
  • OFFLINE
  • Posts: 55
  • Thanks: 1
  • Karma: 1
13 years 8 months ago - 13 years 8 months ago #160740 by clickmo
I'm not exactly sure where the best place to ask this question or what category this question would fall under.

Basically, under a user profile I have access to Community Builder Field Drop down Single Select and Community Builder Field Query. Basically I want to combine the two.

I want my drop down to be populated by results from a query.

The scenario is as follows:

- 2 fields in the user profile will be drop down lists.
- First field has pre-defined values coming from either a database (query) or manual input. The first field is a category, such as: Infantry, Aerospace, Naval
- Second field has pre-defined values coming strictly from database, values determined by the first field. This is like a sub-category, such as: Soldier, Sniper, Pilot, Shipmate.

As you can guess, if the first field drop box is selected to Infantry, the second drop box should only have Soldier and Sniper. Likewise if the first field is set to Aerospace, I should only see Pilot.

What would be the best approach to take in this situation and scenario?
Last edit: 13 years 8 months ago by clickmo.

Please Log in or Create an account to join the conversation.

13 years 8 months ago - 13 years 8 months ago #160746 by byrannewell
Replied by byrannewell on topic Re: Auto populated Drop Down Field
I just did this. In a nutshell, I downloaded a module (mod_php) that let me run javascript, HTML, and PHP in a module position, and I placed my code in there. The resulting drop down was then in that module position.

Then I modified the code I found here , to render the AJAX.

To find the correct SQL statements, I went it to PHP myAdmin, and looked. The main SQL table that contains the CB fields, is "jos_comprofiler_fields". I wanted the first dropdown box to match fields in that table that began with "cb_sub". The result is below:
Code:
SELECT fieldid, title FROM `jos_comprofiler_fields` where `name` LIKE 'cb_sub%' LIMIT 0, 30

Once you dl the PHP mod, create a new module type of mod_php. You can test it by entering in what's below. If done correctly, then it should give you a dropdown list of whatever matches the SQL statement contained in the $sql variable.
Code:
<? $sql = "SELECT fieldid, title FROM `jos_comprofiler_fields` where `name` LIKE 'cb_%' "; $result = mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["fieldid"]; $thing=$row["title"]; $options.="<OPTION VALUE=\"$id\">".$thing.'</option>'; } ?> <select name="mainCategory"> <option>Please Select</option> <?=$options?> </select>

It was much more involved, but those two things should get you on the right path. Although I'm no expert by any stretch of the imagination, I did successfully get it working, and this is how I did it. Let me know what more help you need.
Last edit: 13 years 8 months ago by byrannewell.

Please Log in or Create an account to join the conversation.

  • clickmo
  • clickmo
  • OFFLINE
  • Posts: 55
  • Thanks: 1
  • Karma: 1
13 years 8 months ago #160753 by clickmo
Replied by clickmo on topic Re: Auto populated Drop Down Field
You're right, it does put me on the right path. I did not know mod_php existed. Karma for you my good person.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48622
  • Thanks: 8303
  • Karma: 1446
13 years 8 months ago #160799 by krileon
Replied by krileon on topic Re: Auto populated Drop Down Field
What you're wanting is conditional fields display. This can be done with CB Conditional allowing you to conditionally display a field based off another fields value. You can't alter an inputs options with this plugin, but you can show/hide secondary drop-down fields based off your first drop-down fields value.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

13 years 8 months ago #160820 by byrannewell
Replied by byrannewell on topic Re: Auto populated Drop Down Field
I haven't been able to get the CB Conditional plugin to work as I think it should—namely in an AJAX type manner. Is it supposed to? When I tried it, a) it didn't seem to work in conjunction with CB Profile Pro b) even just using CB, it only showed the second drop down, once the first one was saved and the page was refreshed.

Please Log in or Create an account to join the conversation.

  • clickmo
  • clickmo
  • OFFLINE
  • Posts: 55
  • Thanks: 1
  • Karma: 1
13 years 8 months ago #160832 by clickmo
Replied by clickmo on topic Re: Auto populated Drop Down Field
It never crossed my mind to use the CB Conditional plugin in this manner. It is a bit more leg work, but ... it does the trick without me needing to script it in PHP.

Basically, one drop down is manually created with the main categories. Then every sub-category is individually created as a list and only shown when the category it corresponds to is created via the CB Conditional plugin.

Example:

Field A -> Drop Down (Single Select) with 3 manually populated categories at the bottom. (Infantry, Aerospace, Naval)

Field B -> Drop Down (Single Select) with manually populated entries as sub-categories for Infantry. With CB Conditional options set to: Field conditional self -> IF Field A -> IS Equal To -> TO Infantry -> THEN Show

Repeat Field B for C (Aerospace) and D (Naval)

When accessing the field through the profile, to edit it you will only see Field A. B, C and D will only appear if a specific option of A is selected. B appears on Infantry, otherwise C appears on Aerospace and finally if none of the above are true, D is the one that shows.

Pretty much what I wanted to accomplish, the difference being I have to manually enter every choice and just hide the ones I don't want displayed. End result to the user is still the exact same. Back end though is just slightly more work, perfectly acceptable though.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum