- Creating a connection object using mysql_connect()
- Selecting the database using mysql_select_db();
- Executing the query - mysql_query()
- Processing the results - mysql_fetch_array() or mysql_fetch_assoc()
- Preparing the HTML output (
Well, my PHP basics are still pretty strong. Okay...enough is enough. You need to be smart because everyone does that.
So in my Home.page file (this could be any of your .page file), I first insert the drop down component - TDropDownList
<com:TForm>Now in your
<com:TDropDownList ID="cmbTest" />
</com:TForm>
Home.php file, write a method which fetches all the values from a table.protected function getTestList()Now, in the above method you will write the logic to fetch the data which needs to be fetched from the database. Because, we are doing this with Active Records, I assume that you have already configured your application for the use of Active Records. If not, then go to Configuring the MySQL database for use in PRADO Application with Active Records article and configure it right away.
{
// Returns an array containing all the records from the table
}
Okay, so you have configured Active Record for application. Now, we need the ActiveRecord class for the table from which we will pull the data. I assume you already know who to create the Active Record classes using the PRADO shell. If not refer to the article - Creating a Active Record class using PRADO Application Shell
Also, make sure that your directory which stores the Active Record class is included in the
Here is the table structure for the test table in the database.
mysql> desc test;Once you have your Active Record class for the above table, write the follwing inside it in our
+-----------+--------------+------+-----+---------+----------------+
Field Type Null Key Default Extra
+-----------+--------------+------+-----+---------+----------------+
id int(10) NO PRI NULL auto_increment
test_text varchar(100) YES NULL
+-----------+--------------+------+-----+---------+----------------+
2 rows in set (0.24 sec)mysql>
Home.php class. TestRecord is the active record class for the test table above.<?phpOk. We need to populate the drop down when the page loads. So, we need to write the
class Home extends TPage
{
protected function getTestList()
{
// Returns an array containing all the records from the table
return TestRecord::finder()->findAll();
}
}
?>
onLoad() method in Home.php class. This method is invoked every time the page loads and during callbacks.<?phpWe are writing populating the contents of the drop down only initially when the page loadas and not when it is posted back for server processing.
class Home extends TPage
{
protected function getTestList()
{
// Returns an array containing all the records from the table
return TestRecord::finder()->findAll();
}
public function onLoad($param)
{
if (!$this->IsPostBack)
{
// Populate the Test Drop Down from database values
$this->cmbTest->DataTextField = 'test_text';
$this->cmbTest->DataValueField = 'id';
$this->cmbTest->DataSource = $this->TestList;
$this->cmbTest->dataBind();
}
}
}
?>
So, that's it. If everything works fine, you will have your drop down populated with the values from the database.
Download the working demo of the this tutorial here.

