FORMfields, the premiere web framework
Login
Support Home | Knowledge Base | Documentation | Forums | Contact Us
Info
FORMfields & FORMgen :: Field Customizations

Populating DropDownFields With Data in a Database Table

(2212 views. Last Updated: 2007-08-20 2:39 PM)

Here is an example of how to load a drop down box from rows in a database table:

Let's say you have a table named friends with the following structure and data:
fields: id name  age
row1:   1  John  30
row2:   2  Peter 34
row3:   3  Bob   29

Then the code to display a basic drop down box of friends would be like:
// ---------- FORM STRUCTURE - START ----------
...
$ffForm = new FfForm();
...
$ffDb = &FfDb::s();
$friends = $ffDb->getList("SELECT name FROM friends");
$ffForm->addField(new DropDownField("friend", "Friend", FORM_FIELD_REQUIRED, $friends, null, "[Select One]")); 
...

Alternatively, you could choose to associate the ids with the friend names, so that when the form is submitted, the value of the field is the friend id and not the friend name.
...
$ffForm = new FfForm();
...
$ffDb = &FfDb::s();
$friends = $ffDb->splitResultSet($ffDb->query("SELECT name,id FROM friends");
$ffForm->addField(new DropDownField("friend", "Friend", FORM_FIELD_REQUIRED, $friends[0], $friends[1], "[Select One]")); 
...

Note: This example also works with fields like the ListField, MultipleListField, SetSelectorField, etc...
Info
Support | Earn Money
Copyright © 2005-2019 Brain Book Software LLC.
Built with FORMfields, the premiere web framework.