ChonoForms' DB Connection provides a
useful tool for saving to the database but is more limited when it comes
to getting data back again and using it to control forms or to display
for editing. The Profile plug-in lets you recover a single record, and
ChronoForms' sister product, ChronoConnectivity, is
intended to help you display longer lists of results.
In this recipe, we'll look at getting information from one or
more database tables for use in our Form HTML. Very similar code can be
used to get information for use in other parts of a ChronoForms form,
for example into an e-mail template.

This article is from Chapter 12 “Adding advanced features” of The ChronoForms Book and is included here with permission from Packt Publishing.
Getting ready
There isn't any obvious preparation for this recipe. What we'll do is look at each of the common types of form input in turn and see how to display the extracted information in the Form HTML.
How to do it...
- Getting the information from the database
We've seen an example of this in the drop-down recipe at the start of this article. The code we used there was as follows:
$db =& JFactory::getDBO();
$query = "
SELECT `id`, `title`
FROM `#__content`
WHERE `sectionid` = 1
AND `state` = 1 ;
";
$db->setQuery($query);
$options = $db->loadAssocList();
The first line of this calls the Joomla! database object to give us a connection to the database that Joomla! is using.
The next set of lines define a $query variable that is the MySQL instruction that will be used to extract the data from the database. This is a simple example of getting two columns from the Joomla! jos_content table. To get all the columns, we can change one line to read SELECT *.
The last two lines set the query and then execute it, getting our results back into a $options array in this case. There are several different methods that you can use to execute the query depending on the form in which you want the results. There's a helpful page on the Joomla! docs that tells you what choices you have (see the See also section below).
You can find some more examples of Joomla! database queries here and many more in the ChronoForms forums.
The example above extracted several records from the table to create a set of options. In the rest of this recipe, we'll assume that we've extracted a single record into a $cf_data object so that we can address column values as $cf_data->column_name.
- Adding a value to text or hidden inputs
This is probably the most common and the easiest value to restore.
<input type='text' . . .
value='<?php echo $cf_data->column_name; ?>' />
<input type='hidden' . . .
value='<?php echo $cf_data->column_name; ?>' />
- Adding a value to a textarea
Very similar to the text input but the value goes between the tags:
<textarea . . . ><?php echo $cf_data->column_name; ?></textarea>
- Adding a value to radio buttons
The remaining form elements are more complicated because they don't take simple text values but instead need to be marked as checked or unchecked.
<?php
$c1 = $c2 = '';
if ( $cf_data->column_name == 'xxx' ) {
$c1 = "checked='checked'";
} elseif ( $cf_data->column_name == 'yyy' ) {
$c2 = "checked='checked'";
}
?>
<input type='radio' value='xxx' . . . <?php echo $c1; ?> />
<input type='radio' value='yyy' . . . <?php echo $c2; ?> />
Here we check to see if the result matches the value of the radio input and if it does then we set $c1 or $c2 to show the corresponding input as selected.
- Adding a value to checkbox groups
Checkbox groups can have many buttons and all or none of them can be checked so it's easier to use a loop to check them than a series of if statements. The result has also probably been stored in the database table as a comma separated string like aaa,ccc,eee that we will need to unpack.
<?php
// unpack the results into an array
$result_array = explode(',', $cf_data->column_name);
// create an array matching the check-boxes
$checkbox_array = array('1' => 'aaa', . . . '6' => 'fff');
foreach ( $checkbox_array as $k => $v ) {
if ( in_array($v, $result_array) {
$c[$k] = "checked='checked'";
} else {
$c[$k] = "";
}
}
?>
<input type='checkbox' value='aaa' . . .
<?php echo $c[1]; ?> />
<input type='checkbox' value='bbb' . . .
<?php echo $c[2]; ?> />
. . .
<input type='checkbox' value='fff' . . .
<?php echo $c[6]; ?> />
Although this is more complicated, it is following essentially the same method as we used for the radio buttons above. The big difference is that we are using an array to tell us what checkboxes there are that might be checked. We need to do this to let us create all of the checkboxes correctly.
- Adding a value to a select drop-down
The code for a select drop-down is very similar. Rather than repeating it with just the minor changes, we'll go back and modify the code from the first recipe in this article to create the options using PHP and set one value as selected.
To use the code with a multi-select drop-down requires some small changes to be more like the checkbox code above.
Let's start with the code for creating the drop-down options from a country array:
foreach ( $countries as $k => $v ) {
echo "<option value='$k'>$v</option>";
}
Here's how we adjust this to set a particular value. Let's assume that we have the saved value as $country:
foreach ( $countries as $k => $v ) {
$s = '';
if ( $country == $k ) {
$s = "selected='selected'";
}
echo "<option value='$k' $s >$v</option>";
}
That's it!
How it works...
Each of these methods uses a combination of PHP, MySQL, and HTML to add customised values to your forms. This combination of server-side code, database, and browser code is very powerful and forms the core of any CMS. These simple examples can be extended to make very sophisticated applications based around ChronoForms.
The critical steps that are at work here are: using PHP, as the web page is being built to pull specific information from the database, and then using that information to customise parts of the HTML displayed to the user in the browser.
See also
There are different styles of laying out both JavaScript and PHP and sometimes fierce debates about where line breaks and spaces should go. We've adopted a style here that is hopefully fairly clear, reasonably compact, and more or less the same for both JavaScript and PHP. If it's not the style you are accustomed to, then we're sorry.