The CB Query field plugin (version 3.0 or better) supports 4 additional field types that get their select values from a database query.
These new field types are:
- Query Check Box (Multiple)
- Query Drop Down (Multi-select)
- Query Drop Down (Single Select)
- Query Radio Buttons
You can create as many CB Fields as you want of each field type.
The fields work just like their core counterparts but instead of having to provide a static list of values for each field, you can give an SQL query that generates the list of values.
Our use case is to create a CB Query Single Select drop down country field based on data stored in a database table.
To demonstrate this use case implementation we have prepared an sql statement country_info.sql file from data provided by the the http://www.geonames.org site that you can use to import a country_info table into your Joomla database.
Our country_info table contains the following relevant for this use case fields:
- iso code (2 character unique iso code for country)
- country (name of country)
- continent_full (the full name of the country continent)
With the latest CB Query field plugin (version 3.0 or better) installed and published you will be able to implement the following:
- Create a new CB Field from CB Field Management area
- Select the 'Query Drop Down (Single Select)' type for your new field
- Give the field a name (e.g., countryinfo) and a title (e.g., country)
- Set the size of the field to 10 (so it automatically opens the drop-down field to show 10 of the country select options
- Populate the Query parameter area of the field with:
SELECT `iso`, `country`, `continent_full` FROM `country_info` ORDER BY continent_full, country
(this query sorts the data by continent and then by country name - we are sorting by cointinent because we intend to illustrate the Group Column setting in step 8) - Set the 'Value Column' parameter to iso (this way the 2 character iso code will be stored in the CB database) or you can optionally put country here (if you want the full country name to be stored)
- Set the 'Label Coumn' parameter to country (this way the drop down field will display the country names for selection)
- Set the 'Group Column' parameter to continent_full (this way the countries in the drop down will be grouped by continent)
Our completed CB field configuration looks like the following screenshot:
and our new country field will display on our CB registration page like the following screenshot: