Welcome Guest, Not a member yet? Register   Sign In
Chained selects
#11

[eluser]Leonel Folmer[/eluser]
Thanks for your time, I'll see how it works.
#12

[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?
#13

[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>
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;Welcome to CodeIgniter&lt;/title&gt;
    [removed][removed]
    [removed][removed]
    [removed]
$(function()
{
    $('#country').chainSelect('#state','&lt;?php echo site_url('welcome/combobox') ?&gt;',
    {
        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','&lt;?php echo site_url('welcome/combobox') ?&gt;',
    {
        before:function (target)
        {
            $("#loading").css("display","block");
            $(target).css("display","none");
        },
        after:function (target)
        {
            $("#loading").css("display","none");
            $(target).css("display","inline");
        }
    });
});
    [removed]

&lt;style type="text/css"&gt;

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;
}

&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

<div id="loading">Loading ...</div>

<h1>Chained select demo</h1>

&lt;form name="formname" method="post" action=""&gt;
    &lt;!-- country combobox --&gt;
    <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>
    &lt;!-- state combobox is chained by country combobox--&gt;
    <select name="state" id="state" style="display:none"></select>
    &lt;!-- city combobox is chained by state combobox--&gt;
    <select name="city" id="city" style="display:none"></select>
&lt;/form&gt;

<p><br />Page rendered in {elapsed_time} seconds</p>

&lt;/body&gt;
&lt;/html&gt;
#14

[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]
#15

[eluser]Leonel Folmer[/eluser]
Got it!
#16

[eluser]InsiteFX[/eluser]
DEFAULT CHARSET=latin1 ?

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB