Forums

How to populate select list from database?

vinny 28 Mar, 2011
Hi,

I'm using CF4 and I should say that it's really good form component. But I have an issue with populating select list from database:

I have created a form with form wizard and have there a select list field Countries with name "countries_name". On Events page I added "DB Record Loader" action On Load and specified Table name "jos_countries" and put "Load Fields" to "Yes". Table column named the same as field - "countries_name".

But this field is not populating! Am I miss something?
GreyHead 28 Mar, 2011
Hi vinny,

As far as I can see from a browse through the code you can't link a DB to a select input. I think it would have to be done with a Custom Code Element.

Bob
vinny 28 Mar, 2011
Hi Bob,

Thanks for the reply. I'll do this with a Custom Code Element.

But then could you explain for what can be used DB Record Loader?
GreyHead 28 Mar, 2011
Hi vinny,

I don't know - Probably to reload data into a form?

I've used the multiple reload to display a list of records.

Bob
Terejima 20 Mar, 2013
Hi.
I try to create a select list from my database but I have two problems.
1) If I use <?php > in a custom element (HTML/PHP), my form don't show when I click on Test Form (I see a blank page).
2) I use this code
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT DISTINCT `name`
  FROM `#__ffs5w_users` ;
";
$db->setQuery($query);
$users = $db->loadObjectList();
?>

<select class="" id="user" size="1" title="" name="user">
  <option value="">Selectionnez un utilisateur</option>

<?php
foreach($users as $p) {
  echo "<option value='".$p->name"'>".$p->name"</option>";
}
?>

</select>

and in the preview I see this :

I don't understand why the "setQuery($query); $users = $db->loadObjectList(); ?>" is showing.
GreyHead 20 Mar, 2013
Hi Terejima,

The main problem is that this line is missing some dots '.':
echo "<option value='".$p->name."'>".$p->name."</option>";
or I prefer
echo "<option value='{$p->name}'>{$p->name}</option>";


It may be easier to do this with a DB Multi Record Loader and the Dynamic Data option in the Selec Drop-Down element. Please see this FAQ

The odd code in the Preview window is probably harmless. There are sometimes problems when you have double quotes "" in element boxes :-(

Bob
Terejima 21 Mar, 2013
Thanks bob.

A friend of mine have found another code that works fine. I use the Custom Element with this code :

<?php
@mysql_pconnect("localhost","root","") or die("Echec de connexion au serveur.");
@mysql_select_db("bdd") or die("Echec de selection de la base.");
?>
<select name="type_offre">
<option value="vide">Selectionnez un type d'offre</option>
<?php
$req=mysql_query(" SELECT name FROM ffs5w_users GROUP BY name ORDER BY name ASC") or die("requete impossible");;

    while ($tb = mysql_fetch_array($req))
    {
?>
	<option value= "<?php echo $tb['name']?>"><?php echo $tb['name']?></option>	
     
	<?php
	}
?>
</select>


I don't understand all but if someone want to use it you need to replace :
-"bdd" is the name of your database
-"type_offre" is the name of your select list
-"ffs5w_users" is the name of the table you want to use
-"name" is the name of the column you want to use

It works fine.
This topic is locked and no more replies can be posted.