How to fill a combo box using Smarty and Pear
Let's use a simple example that will fill a combo box with a number of cities stored in a MySql database.
With the three-tier architecture allways in mind, we will need four files:
1.- the template file (cities.tpl)
2.- the plugin function associated with the template file (function load_cities.php)
3- the business file (bus_cities.php)
4.- the data file (dat_cities.php)
We assume that the database is populated with a table of cities that just has the name of the cities associated to a key,
and the data file will have the following code to retreive the cities from the database:
dat_cities.php
<?php
//Data Level Class
class DatCities
{
function __construct()
{
//we are using the PEAR DbManager to access the database, and
//we obtain a global instance of DbManager (which was created in a
// file called app_top.php)
$this->dbManager = $GLOBALS['gDbManager'];
}
//now we create a function to get all the cities from the table city in
// our database to populate the city combo box
public function getCities()
{
$query_string = "SELECT City_Code, City_Name FROM city";
$result = $this->dbManager->DbGetAll($query_string);
return $result;
}
?>
The business level file will contain the code that calls the dat_cities functions:
bus_cities.php
<?php
//Business Level connects with the Data Level: dat_cities.php
require_once SITE_ROOT.'/data_objects/dat_cities.php';
//business level class to retrieve cities
class BusCities
{
//variable contains an instance of the data object DatCities:
private $mDatCities;
//constuctor initializes an object of the Data level:
function __construct()
{
$this->mDatCities = new DatCities();
//fetch all cities from the city table in our database:
public function getCities()
$result = $this->mDatCities->getCities();
return $result;
}
?>
Now, let's take care of the display of our combo box in the template file, without forgetting our plugin function associated to it. This plugin function creates the class ListCities which will will create in turn a business object to access it's "getCities()" function just created above.
function load_cities.php
<?php
// plugin functions inside plugin files must be named: smarty_type_name.
function smarty_function_load_cities($params, $smarty)
{
$list_cities = new ListCities();
$list_cities->init();
// assign template variable
$smarty->assign($params['assign'], $list_cities);
Class ListCities
{
//variable will hold the cities for the combo box:
public $mCities;
/* private member - will hold the business object */
private $mBusCities;
// constructor initializes business tier object
// and reads query string parameter
function __construct()
{
// creating the middle tier object
$this->mBusCities = new BusCities();
}
function init()
{
// get list of cities for combo:
$this->mCities = $this->mBusCities->getCities();
for ($i = 0; $i < count($this->mCities ); $i++)
$this->lCity_Names[] = $this->mCities [$i]['city_name'];
}
}
?>
In the template we will have:
cities.tpl
{* cities.tpl *}
{ load_cities assign="cities"}
{* start cities *}
<table border="0" cellpadding="0" cellspacing="0" width="172">
<tr >
<td align="center" height="20" width="160" class="content">
<form name = "citylist" method="post" action="index.php?ListCities">
<!-- Combo Cities -->
<select name ="xity" id="city" style="width:160px" >
<option value="Choose a city" >Choose a City</option>
{html_options options=$list_cities->lCity_Names}
</select><br/><br/>
<input type="submit" name = "button" value="Submit" >
</form>
</td>
</tr>
</table>
The above template will be displayed in your index.php file at the location you want in your web site. Note that with just one line - "{html_options options=$list_cities->lCity_Names} - you can fill your combo box with hundreds of cities.






