";}?> "> HOW TO: Dropdown from Database - Forums

Forums

HOW TO: Dropdown from Database

ecopure 15 Aug, 2011
Hi All,
My form worked fine until I tried to get a dropdown to populate from a database, can you tell me what i am doing wrong as when i save this the form only loads the chrono link in footer?

<label class="cf_label" style="width: 150px; float: left; margin: 0 10px 0 0; width: 45%;">Lead Industry:</label> <select class="cf_inputbox" id="leadindustry" size="1" title=""  name="leadindustry">
    <option value="1">RET - Window Cleaner</option>
<?php
$database =& JFactory::getDBO();
$sql = "SELECT leadindustry FROM jos_gb_salesleadindustry"
$database->setQuery( $sql );
$rows = $database->loadObjectList();
?>
<?php
foreach($rows as $row) {
  echo "<option value='".$row->leadindustry."'></option>";
}
?>
  
   </select>
GreyHead 16 Aug, 2011
Hi ecopure,

The code looks OK. Does the form show up without this part? It's just possible that using $row & $rows is causing a problem - sometimes Joomla! or other components can use these variable names.

Which version of ChronoForms are you using? You can find the version from Site Admin | Extensions | Install/Uninstall | Components in Joomla! 1.5 or Site Admin | Extensions | Extension Manager | Manage in Joomla! 1.6.

If you have CFv4 the most common reason not to see Form HTML is that you need to drag a Show HTML action into the On Load event.

Bob
ecopure 16 Aug, 2011
Hi greyhead,

the form shows fine up without this code, what should i adjust

i am using v3.2 as v4 wouldnt work with chronoconnectivity.
GreyHead 16 Aug, 2011
Hi ecopure,

By all means email or PM me the site URL and a SuperAdmin login and I'll take a quick look.

Bob
GreyHead 16 Aug, 2011
Hi ecopure,

Fixed, there was a sneaky ; missing from the end of the $sql = . . . line

Bob
ecopure 18 Aug, 2011
Hi Greys,

As usual, your a hero.

Thanks

NOTE TO VIEWERS - Heres how to make a form that has a dropdown box that selects data from your database:

1/. Code to add to the MAIN form:

 <option value="">==?==</option>
<?php
$db =& JFactory::getDBO();
$sql = "
  SELECT `id`, `YOUR_COLUMN_NAME_INSERT_HERE` 
   FROM `#__YOUR_TABLE_NAME_INSERT_HERE` ;
";
$database->setQuery($sql);
$options = $db->loadObjectList();
foreach ( $options as $o ) {
  echo "<option value='{$o->YOUR_COLUMN_NAME_INSERT_HERE}'>{$o->YOUR_COLUMN_NAME_INSERT_HERE}</option>";
}
?>


2/. You can then make another simple form and chronoconnectivity to edit the various options of this specific dropdown box.

3/. This means you can easily add and delete options that appear in your form.

Note: you could also change the option value to the row ID and then option label shows the name: i,e:
<option value='{$o->YOUR_COLUMN_ID_INSERT_HERE}'>{$o->YOUR_COLUMN_NAME_INSERT_HERE}</option>


Hope that helps!
This topic is locked and no more replies can be posted.