[eluser]Leonel Folmer[/eluser]
Thanks for your time, I'll see how it works.
[eluser]Leonel Folmer[/eluser]
Well, I had forgotten to mention that I'm using version 1.7.3, however I tested on version 2.0 and it worked perfectly. with version 1.7.3 not, could you take a look?
[eluser]Leonel Folmer[/eluser]
Version 1.7.3
The Controller
Code: <?php
class Welcome extends Controller {
public function Welcome()
{
parent::Controller();
}
public function index()
{
$this->load->view('welcome_message');
}
public function combobox()
{
$name = $this->input->get('_name');
$value = $this->input->get('_value');
$this->load->model('combo_m');
echo json_encode( $this->combo_m->get_dropdown($name, $value) );
}
}
/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */
The Model
Code: <?php
class Combo_m extends Model {
function __construct()
{
parent::Model();
}
function get_dropdown($name, $value)
{
$arr = array();
switch($name)
{
case 'country' :
$query = $this->db->where('country_id', $value)
->get('states');
if($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$arr[] = array($row->id => $row->name);
}
}
else
{
$arr[] = array('0' => 'No state');
}
break;
case 'state' :
$query = $this->db->where('state_id', $value)
->get('cities');
if($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$arr[] = array($row->id => $row->name);
}
}
else
{
$arr[] = array('0' => 'No city');
}
break;
default :
$arr[] = array(
array('1' => 'Data 1'),
array('2' => 'Data 2'),
array('3' => 'Data 3')
);
break;
}
return $arr;
}
}
The View
Code: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
[removed][removed]
[removed][removed]
[removed]
$(function()
{
$('#country').chainSelect('#state','<?php echo site_url('welcome/combobox') ?>',
{
before:function (target) //before request hide the target combobox and display the loading message
{
$("#loading").css("display","block");
$(target).css("display","none");
},
after:function (target) //after request show the target combobox and hide the loading message
{
$("#loading").css("display","none");
$(target).css("display","inline");
}
});
$('#state').chainSelect('#city','<?php echo site_url('welcome/combobox') ?>',
{
before:function (target)
{
$("#loading").css("display","block");
$(target).css("display","none");
},
after:function (target)
{
$("#loading").css("display","none");
$(target).css("display","inline");
}
});
});
[removed]
<style type="text/css">
body {
background-color: #fff;
margin: 40px;
font-family: Lucida Grande, Verdana, Sans-serif;
font-size: 14px;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 16px;
font-weight: bold;
margin: 24px 0 2px 0;
padding: 5px 0 6px 0;
}
code {
font-family: Monaco, Verdana, Sans-serif;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#loading
{
position:absolute;
top:0px;
right:0px;
background:#ff0000;
color:#fff;
font-size:14px;
font-familly:Arial;
padding:2px;
display:none;
}
</style>
</head>
<body>
<div id="loading">Loading ...</div>
<h1>Chained select demo</h1>
<form name="formname" method="post" action="">
<!-- country combobox -->
<select id="country" name="country">
<option value="1" selected>France</option>
<option value="2">Romania</option>
<option value="3">Usa</option>
<option value="4">Brazil</option>
</select>
<!-- state combobox is chained by country combobox-->
<select name="state" id="state" style="display:none"></select>
<!-- city combobox is chained by state combobox-->
<select name="city" id="city" style="display:none"></select>
</form>
<p><br />Page rendered in {elapsed_time} seconds</p>
</body>
</html>
[eluser]Leonel Folmer[/eluser]
[quote author="bubbafoley" date="1299746834"]oh and here's my database
Code: CREATE TABLE IF NOT EXISTS `cities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
`state_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `cities` (`id`, `name`, `state_id`) VALUES
(1, 'New York', 2),
(2, 'Another City', 2);
CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `countries` (`id`, `name`) VALUES
(1, 'France`'),
(2, 'Romania'),
(3, 'Usa'),
(4, 'Brazil');
CREATE TABLE IF NOT EXISTS `states` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
`country_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `states` (`id`, `name`, `country_id`) VALUES
(1, 'Montana', 3),
(2, 'New York', 3),
(3, 'Texas', 3);
Well, I had forgotten to mention that I’m using version 1.7.3, however I tested on version 2.0 and it worked perfectly. see here with version 1.7.3, could you take a look? [/quote]
[eluser]Leonel Folmer[/eluser]
Got it!
[eluser]InsiteFX[/eluser]
DEFAULT CHARSET=latin1 ?
InsiteFX
|