Query for radiobuttons / dropdopwn-fields single-select

4 years 8 months ago #313318 by ericmuc
I need a query for a form, to get the stored value of a radiobox / dropdown-field (single-select) of CB in the mysql data base.

That can be used in RS!Forms Pro to edit only a special set of profile data beside the CB profile edit.

I know:
1) the chosen data are stored per User in xxxx_comprofiler
2) the fields are stored in xxxx_comprofiler_fields
3) the value/label pairs are stored in xxxx_comprofiler_field_values

My base for an query is:
$db->setQuery("SELECT  'Field1` FROM `XXXX_comprofiler` WHERE `user_id`='".$userId."'");

By this I only get the stored value.

But the form needs also the value|label pairs, which are stored at 3)

The sample code for the form (RS!Forms Pro) is:
//<code>
// Prepare the empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();
// Keep this if you'd like a "Please select" option, otherwise comment or remove it
$items[] = "|Please Select[c]";
 
// Run the SQL query and store it in $results
$db->setQuery("SELECT your_value, your_label FROM #__your_table");
$results = $db->loadObjectList();
 
// Now, we need to convert the results into a readable RSForm! Pro format.
// The Items field will accept values in this format:
// value-to-be-stored|value-to-be-shown
// Eg. m|M-sized T-shirt
foreach ($results as $result) {
  $value = $result->your_value;
  $label = $result->your_label;
  $items[] = $value.'|'.$label;
}
 
// Multiple values are separated by new lines, so we need to do this now
$items = implode("\n", $items);
 
// Now we need to return the value to the field
return $items;
//</code>

Link to RS!Joomla

So the query must contain a subquery, to fill in the possible value|label pairs and show the right value|label pair by the stored value.

Do you have a hint for usage the above code with the CB fields?

Thanks a lot!

Please Log in to join the conversation.

4 years 8 months ago - 4 years 8 months ago #313321 by krileon
The query for building select options is as follows.

SELECT `fieldtitle` AS `value`, if ( `fieldlabel` != '', `fieldlabel`, `fieldtitle` ) AS `text`, `fieldgroup` AS `group` 
 FROM `#__comprofiler_field_values`
 WHERE `fieldid` = FIELD_ID_HERE
 ORDER BY `ordering`

Be sure to replace FIELD_ID_HERE with your actual fields id.

In regards for how to do this in CB you can use CB Query Field which provides Query, Query Check Box (Multiple), Query Drop Down (Multi-select), Query Drop Down (Singe-Select) and Query Radio Buttons fieldtypes. For you usage you'd use the following for example.

Type: Query Radio Buttons
Query:
SELECT `your_value`, `your_label` FROM `#__your_table`
Value Column: your_value
Label Column: your_label

That's all you need to do. It'll handle building the radio field, storage, searching, etc..


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 to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.201 seconds

Facebook Twitter LinkedIn