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

[eluser]Leonel Folmer[/eluser]
Anyone have any working example with MySQL so I can take a look, I'm not able to find anything to see how works, or some who can make a tutorial for Codeigniter. I would like to filter countries, states and cities and after this shows the results in a view.

I found this link example but could not make it work.

Would be very grateful for the help.
#2

[eluser]Jaketoolson[/eluser]
Do you mean CI's Active Record?
#3

[eluser]Leonel Folmer[/eluser]
CI, Codeigniter.
#4

[eluser]Jaketoolson[/eluser]
huh? Did you click on the link?
#5

[eluser]joakley77[/eluser]
Codeigniter's Active Record is about the easiest way to approach MySQL you're going to find. That link is exactly how to approach MySQL within Codeigniter Wink
#6

[eluser]bubbafoley[/eluser]
he's not talking about ActiveRecord. It's a jQuery plugin

http://www.codeassembly.com/Simple-chain...or-jQuery/
#7

[eluser]Jaketoolson[/eluser]
Quote:he’s not talking about ActiveRecord. It’s a jQuery plugin

I took it as ActiveRecord as he posted in a CI forum, not a jQuery forum... in most languages, the term chaining means the same thing, and NOT what the jQuery plugin does. Just like saying you are looking for help with Java and someone says they can help because they know Javascript. Similar name, different languages/applications.

If for some odd reason you need help with the jQuery plugin for 'chaining' select boxes, good luck.
#8

[eluser]Leonel Folmer[/eluser]
I'm sorry for having caused such confusion, in fact only bubbafoley understand my question, this is exactly the example he mentioned that I seek. need a working example with CodeIgniter.
#9

[eluser]bubbafoley[/eluser]
[quote author="Leonel Folmer" date="1299742277"]I'm sorry for having caused such confusion, in fact only bubbafoley understand my question, this is exactly the example he mentioned that I seek. need a working example with CodeIgniter.[/quote]

I got your back. Here's my version.

application/controllers/welcome.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');    
    }
    
    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) );
    }
}

application/models/combo_m.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Combo_m extends CI_Model {
    
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    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;
    }

}

application/views/welcome_message.php
Code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>
    <script language="JavaScript" type="text/javascript" src="/assets/js/jquery.js"></script>
    <script language="JavaScript" type="text/javascript" src="/assets/js/jquery.chainedSelects.js"></script>
    <script language="JavaScript" type="text/javascript">
$(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");
        }
    });
});
    </script>

<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>
#10

[eluser]bubbafoley[/eluser]
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);




Theme © iAndrew 2016 - Forum software by © MyBB