Database-driven chained select using xajax

by gowtham 2010-09-17 11:25:30

Controller (system/application/controllers/mypage.php):
class Mypage extends Controller {
function __construct()
{
parent::Controller();
}

function showFormPage()
{
$this->load->library('xajax');
$this->xajax->registerFunction(array('getCities',&$this,'getCities'));
$this->xajax->registerFunction(array('getZipCodes',&$this,'getZipCodes'));
$this->xajax->processRequest();

$this->load->model('countries');
$data['country_list'] = $this->countries->getListAll(null, 0, 'html_option');
$data['xajax_js'] = $this->xajax->getJavascript(base_url());
$this->load->view('htmlform', $data);
}

function getCities($country_id)
{
$objResponse = new xajaxResponse();
$this->load->model('cities');
$res = $this->cities->getListByCountry(null, 0, $country_id, 'html_option');
$objResponse->Assign("city_id", "innerHTML", $res);
return $objResponse;
}

function getZipCodes($city_id)
{
$objResponse = new xajaxResponse();
$this->load->model('zipcodes');
$res = $this->cities->getListByCity(null, 0, $city_id, 'html_option');
$objResponse->Assign("zipcode_id", "innerHTML", $res);
return $objResponse;
}
}

Model (system/application/models/countries.php):
class Countries extends Model {
var $table;
function __construct()
{
$this->table = "countries";
parent::Model();
}

function getListAll($limit=NULL, $offset=0, $option)
{
if ($limit != NULL)
{
$query = $this->db->query("SELECT * FROM ".$this->table." LIMIT ".$limit." OFFSET ".$offset." ");
}
else
{
$query = $this->db->query("SELECT * FROM ".$this->table." ");
}
$res = $query->result_array();
if ($option=='html_option')
{
$list = '';
for($x=0; $x<count($res); $x++)
{
$list .= "<option value='".$res[$x]['id']."'>".$res[$x]['country_name']."</option>";
}
return $list;
}
else
{
return $res;
}
}
}

Model (system/application/models/cities.php):
class Cities extends Model {
var $table;
function __construct()
{
$this->table = "cities";
parent::Model();
}

function getListByCountry($limit=NULL, $offset=0, $country_id, $option)
{
if ($limit != NULL)
{
$query = $this->db->query("SELECT * FROM ".$this->table." WHERE country_id = '".$country_id."' LIMIT ".$limit." OFFSET ".$offset." ");
}
else
{
$query = $this->db->query("SELECT * FROM ".$this->table." WHERE country_id = '".$country_id."' ");
}
$res = $query->result_array();
if ($option=='html_option')
{
$list = '';
for($x=0; $x<count($res); $x++)
{
$list .= "<option value='".$res[$x]['id']."'>".$res[$x]['city_name']."</option>";
}
return $list;
}
else
{
return $res;
}
}
}

Model (system/application/models/zipcodes.php):
class ZipCodes extends Model {
var $table;
function __construct()
{
$this->table = "zipcodes";
parent::Model();
}

function getListByCity($limit=NULL, $offset=0, $city_id, $option)
{
if ($limit != NULL)
{
$query = $this->db->query("SELECT * FROM ".$this->table." WHERE city_id = '".$city_id."' LIMIT ".$limit." OFFSET ".$offset." ");
}
else
{
$query = $this->db->query("SELECT * FROM ".$this->table." WHERE city_id = '".$city_id."' ");
}
$res = $query->result_array();
if ($option=='html_option')
{
$list = '';
for($x=0; $x<count($res); $x++)
{
$list .= "<option value='".$res[$x]['id']."'>".$res[$x]['zipcode']."</option>";
}
return $list;
}
else
{
return $res;
}
}
}

View (system/application/views/htmlform.php):
<html>
<head>
<?=$xajax_js;?>
</head>
<body>
<form action="" method="post">
Select Country: <select name="country_id" id="country_id" 0nselect="getCities(this.value)"><?=$country_list?></select><br />
Select City: <select name="city_id" id="city_id" 0nselect="getZipcodes(this.value)"></select><br />
Select ZipCode: <select name="zipcode_id" id="zipcode_id"></select><br />
</form>
</body>
</html>

Tagged in:

1142
like
0
dislike
0
mail
flag

You must LOGIN to add comments