Welcome Guest, Not a member yet? Register   Sign In
Database-driven chained select using xajax
#1

[eluser]Mortred[/eluser]
Just thought I'd share my code.

Before we begin, I must assume you successfully installed xajax in CodeIgniter. To know more about how to install xajax in CodeIgniter, just search this forums.

The actual model files are a bit large so I reduced it only the necessary functions.

Controller (system/application/controllers/mypage.php):
Code:
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):
Code:
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):
Code:
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):
Code:
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):
Code:
&lt;html&gt;
&lt;head&gt;
  &lt;?=$xajax_js;?&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form action="" method="post"&gt;
      Select Country: <select name="country_id" id="country_id" 0nselect="getCities(this.value)">&lt;?=$country_list?&gt;</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 />
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;


Messages In This Thread
Database-driven chained select using xajax - by El Forum - 09-18-2008, 10:33 AM
Database-driven chained select using xajax - by El Forum - 07-09-2009, 01:00 AM
Database-driven chained select using xajax - by El Forum - 07-12-2009, 10:46 PM
Database-driven chained select using xajax - by El Forum - 09-28-2009, 04:17 AM
Database-driven chained select using xajax - by El Forum - 10-07-2009, 03:19 AM
Database-driven chained select using xajax - by El Forum - 10-07-2009, 05:21 AM
Database-driven chained select using xajax - by El Forum - 09-14-2010, 02:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB