⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

[updated 24/11/2012] Dependent dropdown (library)



Koia

Koia
  • profile picture
  • Member

Posted 19 July 2016 - 07:36 AM

Can someone help me with getting the Dependent dropdown library working with the "Goods" test code as in
/topic/1087-updated-24112012-dependent-dropdown-library/.

I use Codeigniter 3 with HMVC from https://github.com/jlamim/ci3-hmvc, grocery-crud-1.5.4, Gc_dependent_select 1.5 and I have $config['index_page'] = 'index.php';

The drop down selection works only for Country but nothing happens when i click on state and city.

When I click on edit in a grocery crud http://.....hudev1/index.php/hem/goods/goods I get following error.

 

Error Number: 1054

Unknown column 'post_code' in 'where clause'

SELECT * FROM `dd_city` WHERE `state_ids` = '1' AND post_code>'167' ORDER BY `state_title` DESC

Filename: modules/grocery_crud/libraries/Gc_dependent_select.php

Line Number: 147


Similar problem was reported in
/topic/1087-updated-24112012-dependent-dropdown-library/page-6
response #120. I implemented proposed changes as in #121 but the problem remains.

Thanks in advance, Ingmar

My code and database is as follows:

    public function goods() {

        $crud = new grocery_CRUD();
        $crud->set_table('dd_goods');
        $crud->set_relation('goods_country', 'dd_country', 'country_title');
        $crud->set_relation('goods_state', 'dd_state', 'state_title');
        $crud->set_relation('goods_city', 'dd_city', 'city_title');

        $this->load->library('gc_dependent_select');
// settings

        $fields = array(
// first field:
            'goods_country' => array(// first dropdown name
                'table_name' => 'dd_country', // table of country
                'title' => 'country_title', // country title
                'relate' => null // the first dropdown hasn't a relation
            ),
// second field
            'goods_state' => array(// second dropdown name
                'table_name' => 'dd_state', // table of state
                'title' => 'state_title', // state title
                'id_field' => 'state_id', // table of state: primary key
                'relate' => 'country_ids', // table of state:
                'data-placeholder' => 'select state' //dropdown's data-placeholder:
            ),
// third field. same settings
            'goods_city' => array(
                'table_name' => 'dd_city',
                'where' => "post_code>'167'", // string. It's an optional parameter.
                'order_by' => "state_title DESC", // string. It's an optional parameter.
                'title' => 'id: {city_id} / city : {city_title}', // now you can use this format )))
                'id_field' => 'city_id',
                'relate' => 'state_ids',
                'data-placeholder' => 'select city'
            )
        );

        $config = array(
        'main_table' => 'dd_goods',
        'main_table_primary' => 'goods_id',
        'url' => base_url() . 'index.php/' . strtolower(__CLASS__) . '/' . strtolower(__FUNCTION__) . '/',
        'ajax_loader' => base_url() . 'style/images/'. 'ajax-loader.gif',
        'segment_name' => 'Your_segment_name' // It's an optional parameter. by default "get_items"
        );
        $categories = new gc_dependent_select($crud, $fields, $config);

// first method:
        //$output = $categories->render();
// the second method:
        $js = $categories->get_js();
        $output = $crud->render();
        $output->output.= $js;
        $this->_example_output($output);
    }
   

}
/*

CREATE TABLE IF NOT EXISTS `dd_country` (
  `country_id` int(15) NOT NULL,
  `country_title` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `dd_state` (
  `state_id` int(15) NOT NULL,
  `state_title` varchar(255) DEFAULT NULL,
  `country_ids` int(15) NOT NULL,
  PRIMARY KEY (`state_id`),
  KEY `country_ids` (`country_ids`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `dd_city` (
  `city_id` int(15) NOT NULL,
  `city_title` varchar(255) DEFAULT NULL,
  `state_ids` int(15) NOT NULL,
  PRIMARY KEY (`city_id`),
  KEY `state_ids` (`state_ids`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `dd_goods` (
  `goods_id` int(11) NOT NULL,
  `goods_name` varchar(255) DEFAULT NULL,
  `goods_country` int(11) NOT NULL,
  `goods_state` int(11) NOT NULL,
  `goods_city` int(11) NOT NULL,
  PRIMARY KEY (`goods_id`),
  KEY `goods_country` (`goods_country`),
  KEY `goods_state` (`goods_state`),
  KEY `goods_city` (`goods_city`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 */

 


Koia

Koia
  • profile picture
  • Member

Posted 20 July 2016 - 23:53 PM

I have tested the "Goods" dependent dropdown example with Codeigniter 3.06 without HMVC option and it works as expected. So latest version Gc_dependent_select 1.5 unfortunately seems not to support HMVC option. Are there plans for a HMVC support in a possible future version?


Novelz

Novelz
  • profile picture
  • Member

Posted 18 August 2016 - 09:03 AM

Hi there!
Thank you Victor for this awesome library, I have only one problem, maybe I'm missing something but I can't update dynamically the second select.
For example, if I change the first select from A to B I still have in the second select the elements related to the A value.

 

Is there any way to make them update on every change?


xmyownprisonx

xmyownprisonx
  • profile picture
  • Member

Posted 27 August 2016 - 02:17 AM

Hi all! This is my second library.
If you have a few dependent dropdowns on any page this library for you.
When you made selection in a "Parent" dropdown the library refreshes a "child" dropdown.

Advantage of this library:

  • integration is easily
  • it is not required to change a template
  • it is not required to create a callback function

The library works perfectly with GC v 1.5.x and CI 3.x
_______________________________________

Download here:

v 1.5.1 attachicon.gifGc_dependent_select.php

__________________________________________

v 1.2.0 attachicon.gifgc_dependent_select.php
_______________________________________
v 1.1.1 attachicon.gifgc_dependent_select.php
_______________________________________
v 1.1.0 attachicon.gifgc_dependent_select.php
The library v.1.1.0 has a bug!
http://www.grocerycr...t__20#entry4344
_______________________________________

How to use (v. 1.2.0.):
attachicon.gifcc.jpg
attachicon.gifc.jpg
 


$crud = new grocery_CRUD();
$crud->set_table('dd_goods');
$crud->set_relation('goods_country', 'dd_country', 'country_title');
$crud->set_relation('goods_state', 'dd_state', 'state_title');
$crud->set_relation('goods_city', 'dd_city', 'city_title');

$this->load->library('gc_dependent_select');
// settings

$fields = array(

// first field:
'goods_country' => array( // first dropdown name
'table_name' => 'dd_country', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
// second field
'goods_state' => array( // second dropdown name
'table_name' => 'dd_state', // table of state
'title' => 'state_title', // state title
'id_field' => 'state_id', // table of state: primary key
'relate' => 'country_ids', // table of state:
'data-placeholder' => 'select state' //dropdown's data-placeholder:

),
// third field. same settings
'goods_city' => array(
'table_name' => 'dd_city',
'where' =>"post_code>'167'",  // string. It's an optional parameter.
'order_by'=>"state_title DESC",  // string. It's an optional parameter.
'title' => 'id: {city_id} / city : {city_title}',  // now you can use this format )))
'id_field' => 'city_id',
'relate' => 'state_ids',
'data-placeholder' => 'select city'
)
);

$config = array(
'main_table' => 'dd_goods',
'main_table_primary' => 'goods_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/', path to method
'ajax_loader' => base_url() . 'ajax-loader.gif' // path to ajax-loader image. It's an optional parameter
'segment_name' =>'Your_segment_name' // It's an optional parameter. by default "get_items"
);
$categories = new gc_dependent_select($crud, $fields, $config);

// first method:
//$output = $categories->render();

// the second method:
$js = $categories->get_js();
$output = $crud->render();
$output->output.= $js;
$this->_example_output($output);

Note:

You can't use the callback_edit_field and callback_add_field for this field. You can use callback_edit_field and callback_add_field only for the first field

example 2:
attachicon.gifschema.jpg


function test()
{
$crud = new grocery_CRUD();
$crud->set_table('zone_allocate_subzone');
$crud->set_relation('category_id', 'category', 'category_name');
$crud->set_relation('subcategory_id', 'sub_category', 'subcategory_name');
$crud->set_relation('zone_id', 'zone', 'zone_name');
$crud->set_relation('subzone_id', 'sub_zone', 'subzone_name');
$this->load->library('gc_dependent_select');

$fields = array(
'category_id' => array(// first dropdown name
'table_name' => 'category', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
'subcategory_id' => array(// second dropdown name
'table_name' => 'sub_category', // table of state
'title' => 'subcategory_name', // state title
'id_field' => 'id', // table of state: primary key
'relate' => 'category_id', // table of state:
'data-placeholder' => 'Select Subcategory' //dropdown's data-placeholder:
)
);
$config = array(
'main_table' => 'dd_goods',
'main_table_primary' => 'goods_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
);
$categories = new gc_dependent_select($crud, $fields, $config);
$js = $categories->get_js();

$fields2 = array(
'zone_id' => array(// first dropdown name
'table_name' => 'zone', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
'subzone_id' => array(// second dropdown name
'table_name' => 'sub_zone', // table of state
'title' => 'subzone_name', // state title
'id_field' => 'id', // table of state: primary key
'relate' => 'zone_id', // table of state:
'data-placeholder' => 'Select Subzone' //dropdown's data-placeholder:
)
);
// change the "segment_name"!!!
$config2 = array(
'main_table' => 'dd_goods',
'main_table_primary' => 'goods_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
'segment_name' => "other_name"
);
$categories2 = new gc_dependent_select($crud, $fields2, $config2);
$js2 = $categories2->get_js();

$output = $crud->render();
$output->output.= $js . $js2;

$this->_example_output($output);
}

---------------------------------------------------
P.S. Thanks to web-johnny, thanks to KaBaDaBrA and to me /public/style_emoticons/#EMO_DIR#/smile.png .
P.S.S. Code can contain some bugs . If you find a bug - report, please.
Sorry, my English is poor.

Sir Viktor,

 

Can I used this as filter? then display list of students that I can encode grades on corresponding student? Thanks a lot.. and More Power!!!


Zaraq K Bangash

Zaraq K Bangash
  • profile picture
  • Member

Posted 28 August 2016 - 09:50 AM

Hello Victor.. i am using latest version GC and CI 3X, also your latest library for dependent dropdown. my case is exactly the same like your example2 code. the problem is my 1st dependent dropdown works ie main_category - subcategory dropdowns but my second set of dependent dropdown doesn't unless i remove first dropdown code ie main_category - subcategory

 

. otherwise then second dependent dropdown keeps loading and loading and loads nothing in his dependent dropdown.

public function product()
    {
        if(($this->session->userdata('logged_in')==TRUE) && ($this->session->userdata('role')== 'admin'))
        {
        
            $crud = new grocery_CRUD();

            //$crud->set_theme('datatables');
            $crud->set_table('product');
            $crud->set_subject('Product');
            $crud->required_fields('products_name','products_desc','products_price','products_img1','products_img2','option_id','cat_id');
            $crud->set_relation('main_category_id','main_category','main_category_name');
            $crud->set_relation('opt_grp_id','opt_grp','opt_grp_name');
            $crud->set_relation('option_id','options','option_value');
            $crud->set_relation('cat_id','category','cat_name');
            
            $crud->set_field_upload('products_img1','assets/images/products'); // used to set input type file upload
            $crud->set_field_upload('products_img2','assets/images/products'); // used to set input type file upload
            $crud->set_field_upload('products_img3','assets/images/products'); // used to set input type file upload
            $crud->set_field_upload('products_img4','assets/images/products'); // used to set input type file upload
            
            $crud->display_as('products_name','Product Name');
            $crud->display_as('cat_id','Sub-Category Name');
            $crud->display_as('main_category_id','Parent Category');
            $crud->display_as('opt_grp_id','Product Option Group');
            $crud->display_as('option_id','Product Option Value');
            $crud->display_as('products_desc','Product Brief Description');
            $crud->display_as('products_desc2','Product Description');
            $crud->display_as('products_price','Price');
            $crud->display_as('products_disc_price','New Discounted Price');
            $crud->display_as('products_img1','Add Image 1');
            $crud->display_as('products_img2','Add Image 2');
            $crud->display_as('products_img3','Add Image 3');
            $crud->display_as('products_img4','Add Image 4');
            
            $this->load->library('gc_dependent_select');
            
            // ****** AJax method for loading sub categories in  dropdown2 when main category in dropdown1 is changed*****

            $fields = array(
            
            // first field:
            'main_category_id' => array( // first dropdown name
            'table_name' => 'main_category', // table of country
            'title' => 'main_category_name', // main category title/name
            'relate' => null // the first dropdown hasn't a relation
            ),
            // second field
            'cat_id' => array( // second dropdown name
            'table_name' => 'category', // table of state
            'title' => 'cat_name', // state title
            'id_field' => 'cat_id', // table of category: primary key
            'relate' => 'main_category_id', // table of state:
            'data-placeholder' => 'Select Sub-Category' //dropdown's data-placeholder:
            
            )// end of Fields array
            
            );            
            $config = array(
            'main_table' => 'product',
            'main_table_primary' => 'products_id',
            "url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
            'ajax_loader' => base_url() . 'assets/images/ajax-loader.gif' // path to ajax-loader image. It's an optional parameter
        //    ,'segment_name' =>'Your_segment_name' // It's an optional parameter. by default "get_items"
            );
            $categories = new gc_dependent_select($crud, $fields, $config);
            $js = $categories->get_js();
            
            ////************************SECOND DROP DOWN for selecting option types******************
            
            $fields2 = array(
            
            // first field:
            'opt_grp_id' => array( // first dropdown name
            'table_name' => 'opt_grp', // table of country
            'title' => 'opt_grp_name', // main category title/name
            'relate' => null // the first dropdown hasn't a relation
            ),
            // second field
            'option_id' => array( // second dropdown name
            'table_name' => 'options', // table of state
            'title' => 'option_value', // state title
            'id_field' => 'option_id', // table of category: primary key
            'relate' => 'opt_grp_id', // table of state:
            'data-placeholder' => 'Select Product Options' //dropdown's data-placeholder:
            
            )// end of Fields array
            );
            
            $config2 = array(
            'main_table' => 'product',
            'main_table_primary' => 'products_id',
            "url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
            'ajax_loader' => base_url() . 'assets/images/ajax-loader.gif' // path to ajax-loader image. It's an optional parameter
            );
            
            $categories2 = new gc_dependent_select($crud, $fields2, $config2);
            
            $js2 = $categories2->get_js();
            
            $output = $crud->render();
            $output->output.= $js . $js2;
            $this->_example_output($output);            

        }else
        {
            $this->session->set_flashdata('msg', 'Please login first!!!');
            redirect('Admin/index');
            }
    }

 

Victor my 2nd dependent dropdown works only when i remove first dependent code. problem might be here : $output->output.= $js . $js2; when i used them the first one works other keeps loading
I will really appreciate your quick help on this.

Thanks

 


Zaraq K Bangash

Zaraq K Bangash
  • profile picture
  • Member

Posted 29 August 2016 - 11:27 AM

Hello my issues are fixed now.. I figured out very late that i was 

$fields = array(

'main_cat_id' => array(
'table_name' => 'main_category', 
'title' => 'main_cat_name', // country title
'relate' => null 
),
'cat_id' => array(
'table_name' => 'category',
'title' => 'cat_name',
'id_field' => 'cat_id', // table of state: primary key
'relate' => 'main_cat_id',                                                //MY PROBLEM LIED HERE I WAS NOT RELATING TWO TABLES CORRECTLY 
'data-placeholder' => 'Select Subcategory' //dropdown's data-placeholder:
)
);

 

and i m not sure but i was excluding this part for my 2nd dependent drop-down set when i included this line 'segment_name' => "other_name"

 

$config2 = array(

'main_table' => 'products',
'main_table_primary' => 'products_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
'ajax_loader' => base_url() .'assets/images/ajax-loader.gif',
'segment_name' => "other_name"
);

 

now my both of dependent dropdowns are working fine.


mckaygerhard

mckaygerhard
  • profile picture
  • Member

Posted 07 October 2016 - 20:25 PM

hi all, i made the use of this great enhanced library for GC but i have a problem, i set some callback and when i try to add new registry got an error, i note that callback cannot be use in related fields, but i only used in the main primary key!

 

the add does not work and trow for me this:

2016-10-07 16:14:38: (mod_fastcgi.c.2695) FastCGI-stderr: PHP Fatal error:  Uncaught exception 'Exception' with message 'On the state "edit" the Primary key cannot be null' in /home/general/Devel/mainstream/simplegastoswebphp/appweb/libraries/Grocery_CRUD.php:3229
2016-10-07 16:14:38: (mod_fastcgi.c.2695) FastCGI-stderr: Stack trace:
2016-10-07 16:14:38: (mod_fastcgi.c.2695) FastCGI-stderr: #0 /home/general/Devel/mainstream/simplegastoswebphp/appweb/libraries/Grocery_CRUD.php(4666): grocery_CRUD_States->getStateInfo()
2016-10-07 16:14:38: (mod_fastcgi.c.2695) FastCGI-stderr: #1 /home/general/Devel/mainstream/simplegastoswebphp/appweb/controllers/cargargastover.php(192): Grocery_CRUD->render()
2016-10-07 16:14:38: (mod_fastcgi.c.2695) FastCGI-stderr: #2 [internal function]: Cargargastover->gastoregistros('tienda', 'read')
2016-10-07 16:14:38: (mod_fastcgi.c.2695) FastCGI-stderr: #3 /home/general/Devel/mainstream/simplegastoswebphp/appsys/core/CodeIgniter.php(359): call_user_func_array(Array, Array)
2016-10-07 16:14:38: (mod_fastcgi.c.2695) FastCGI-stderr: #4 /home/general/Devel/mainstream/simplegastoswebphp/index.php(219): require_once('/home/general/D...')
2016-10-07 16:14:38: (mod_fastcgi.c.2695) FastCGI-stderr: #5 {main}
2016-10-07 16:14:38: (mod_fastcgi.c.2695) FastCGI-stderr:   thrown in /home/general/Devel/mainstream/simplegastoswebphp/appweb/libraries/Grocery_CRUD.php on line 3229


mckaygerhard

mckaygerhard
  • profile picture
  • Member

Posted 07 October 2016 - 20:30 PM

 

for difficult cases use KaBaDaBrA's code.
Maybe in the next version I'll try make this functionality.

also the abakadaba code its no more, link are down

 

appart of my callbak problem


mckaygerhard

mckaygerhard
  • profile picture
  • Member

Posted 09 October 2016 - 23:41 PM

solved my error, the problem was that in grosery crud cannot set a fiel as readonly (set field type) and later also set a callback.. when goes to the inserts, the grocery crud does not write the value for that field..

 

casuality that fiel was the kp filed, but happened with all! its a bug or a non-documnted limit of grocery crud


Ayer Siddharth

Ayer Siddharth
  • profile picture
  • Member

Posted 17 November 2016 - 11:09 AM

Hi i have two depended drop downs which needs to be filled with same data when we select any value from parent drop down.

 

I was able to get it working in first drop down that i declare in fields. While the second one remains disabled.

 

Here is my code.

 

$fields = array(
'property_id' => array(
'table_name' => 'property',
'relate' => null
)
,'to_pano' => array( //this works
'table_name' => 'panorama',
'title' => 'name', 
'id_field' => 'id',
'relate' => 'property_id',
'data-placeholder' => 'Select Panorama',
'ajax_loader' => base_url() . 'loading.gif'
),
'from_pano' => array( //this does not, it just remains disabled.
'table_name' => 'panorama',
'title' => 'name', 
'id_field' => 'id',
'relate' => 'property_id',
'data-placeholder' => 'Select Panorama',
'ajax_loader' => base_url() . 'loading.gif'
),
 
);
 
$config = array(
'main_table' => 'property',
'main_table_primary' => 'id',
"url" => base_url() . '/' . __FUNCTION__ . '/',
);
 
$categories = new gc_dependent_select($this->grocery_crud, $fields, $config);
$js = $categories->get_js();
$output = $this->grocery_crud->render();
$output->output.= $js;
$this->_example_output($output);
 
Does this plugin supports this kind of requirements. If not what could be work around to this kind of situations.
 
Any help would be appreciated.
 
Thanks
Siddharth

xcaldana

xcaldana
  • profile picture
  • Member

Posted 02 December 2016 - 17:13 PM

Hi all! This is my second library.
If you have a few dependent dropdowns on any page this library for you.
When you made selection in a "Parent" dropdown the library refreshes a "child" dropdown.

Advantage of this library:

  • integration is easily
  • it is not required to change a template
  • it is not required to create a callback function

The library works perfectly with GC v 1.5.x and CI 3.x
_______________________________________

Download here:

v 1.5.1 attachicon.gifGc_dependent_select.php

__________________________________________

v 1.2.0 attachicon.gifgc_dependent_select.php
_______________________________________
v 1.1.1 attachicon.gifgc_dependent_select.php
_______________________________________
v 1.1.0 attachicon.gifgc_dependent_select.php
The library v.1.1.0 has a bug!
http://www.grocerycr...t__20#entry4344
_______________________________________

How to use (v. 1.2.0.):
attachicon.gifcc.jpg
attachicon.gifc.jpg
 


$crud = new grocery_CRUD();
$crud->set_table('dd_goods');
$crud->set_relation('goods_country', 'dd_country', 'country_title');
$crud->set_relation('goods_state', 'dd_state', 'state_title');
$crud->set_relation('goods_city', 'dd_city', 'city_title');

$this->load->library('gc_dependent_select');
// settings

$fields = array(

// first field:
'goods_country' => array( // first dropdown name
'table_name' => 'dd_country', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
// second field
'goods_state' => array( // second dropdown name
'table_name' => 'dd_state', // table of state
'title' => 'state_title', // state title
'id_field' => 'state_id', // table of state: primary key
'relate' => 'country_ids', // table of state:
'data-placeholder' => 'select state' //dropdown's data-placeholder:

),
// third field. same settings
'goods_city' => array(
'table_name' => 'dd_city',
'where' =>"post_code>'167'",  // string. It's an optional parameter.
'order_by'=>"state_title DESC",  // string. It's an optional parameter.
'title' => 'id: {city_id} / city : {city_title}',  // now you can use this format )))
'id_field' => 'city_id',
'relate' => 'state_ids',
'data-placeholder' => 'select city'
)
);

$config = array(
'main_table' => 'dd_goods',
'main_table_primary' => 'goods_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/', path to method
'ajax_loader' => base_url() . 'ajax-loader.gif' // path to ajax-loader image. It's an optional parameter
'segment_name' =>'Your_segment_name' // It's an optional parameter. by default "get_items"
);
$categories = new gc_dependent_select($crud, $fields, $config);

// first method:
//$output = $categories->render();

// the second method:
$js = $categories->get_js();
$output = $crud->render();
$output->output.= $js;
$this->_example_output($output);

Note:

You can't use the callback_edit_field and callback_add_field for this field. You can use callback_edit_field and callback_add_field only for the first field

example 2:
attachicon.gifschema.jpg


function test()
{
$crud = new grocery_CRUD();
$crud->set_table('zone_allocate_subzone');
$crud->set_relation('category_id', 'category', 'category_name');
$crud->set_relation('subcategory_id', 'sub_category', 'subcategory_name');
$crud->set_relation('zone_id', 'zone', 'zone_name');
$crud->set_relation('subzone_id', 'sub_zone', 'subzone_name');
$this->load->library('gc_dependent_select');

$fields = array(
'category_id' => array(// first dropdown name
'table_name' => 'category', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
'subcategory_id' => array(// second dropdown name
'table_name' => 'sub_category', // table of state
'title' => 'subcategory_name', // state title
'id_field' => 'id', // table of state: primary key
'relate' => 'category_id', // table of state:
'data-placeholder' => 'Select Subcategory' //dropdown's data-placeholder:
)
);
$config = array(
'main_table' => 'dd_goods',
'main_table_primary' => 'goods_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
);
$categories = new gc_dependent_select($crud, $fields, $config);
$js = $categories->get_js();

$fields2 = array(
'zone_id' => array(// first dropdown name
'table_name' => 'zone', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
'subzone_id' => array(// second dropdown name
'table_name' => 'sub_zone', // table of state
'title' => 'subzone_name', // state title
'id_field' => 'id', // table of state: primary key
'relate' => 'zone_id', // table of state:
'data-placeholder' => 'Select Subzone' //dropdown's data-placeholder:
)
);
// change the "segment_name"!!!
$config2 = array(
'main_table' => 'dd_goods',
'main_table_primary' => 'goods_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
'segment_name' => "other_name"
);
$categories2 = new gc_dependent_select($crud, $fields2, $config2);
$js2 = $categories2->get_js();

$output = $crud->render();
$output->output.= $js . $js2;

$this->_example_output($output);
}

---------------------------------------------------
P.S. Thanks to web-johnny, thanks to KaBaDaBrA and to me /public/style_emoticons/#EMO_DIR#/smile.png .
P.S.S. Code can contain some bugs . If you find a bug - report, please.
Sorry, my English is poor.

 

I'm new as developer web in PHP, my speciality is Oracle Developer & DBA. I'm using Grocery CRUD and I had added dependent dropdown functionality in project library, so far so good. My dropdowns has two levels Countries & States. My first dropdown (countries) working very well, but when I selected a optiond on this, the ajax loader icon be hold working and never finish and my second dropdown (states) stay disabled, and can not to select no one option.

 

I have three tables (mysql)

Table: hoa_condominius (main table)
    id_condom  integer(7) NOT NULL AUTO INCREMENT PRIMARY KEY
    ky_country varchar(3) NOT NULL COMMENT "(FK with cat_countries)"
    ky_state   varchar(4) NOT NULL COMMENT "(FK with cat_states)"
 
Table: cat_countries
    id_country   varchar(3) NOT NULL PRIMARY KEY
 
Table: cat_states
    id_state    varchar(4) NOT NULL PRIMARY KEY
    id_country  varchar(3) NOT NULL COMMENT "(FK with id_country from cat_countries)"

 

MY CODE
  public function hoa_condominiums() {
       $crud = new grocery_CRUD() ;
       $crud->set_subject('Condominiums') ;
       $state_crud = $crud->getState() ;
       $crud->set_theme('flexigrid') ;
       $crud->set_table('hoa_condominiums') ;
       $crud->columns('ds_name','ds_text','ky_country','ky_state','nu_codezip','ds_address','am_rentfee','nu_building','nu_condom','am_billing','gm_urlplace','sw_status','dt_status') ;
       $crud->set_relation('ky_country','cat_countries','ds_lcountry',array('sw_status' => 'A')) ;
       $crud->set_relation('ky_state','cat_states','ds_lstate',array('sw_status' => 'A')) ;
       $this->load->library('gc_dependent_select') ;
 
       $fields = array('ky_country' => array('table_name'       => 'cat_countries',
                                             'title'            => 'Country', // country title
                                             'relate'           => null,
                                             'data-placeholder' => ':select a country'),
                       'ky_state'   => array('table_name'       => 'cat_states',
                                             'title'            => 'State',
                                             'id_field'         => 'id_state',
                                             'relate'           => 'id_country',
                                             'data-placeholder' => ':select a state')) ;
 
       $config = array('main_table' => 'hoa_condominiums',
                       'main_table_primary' => 'ky_condom',
                       // "url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/', // path to method
                       "url" => site_url() . '/examples/',
                       'ajax_loader' => base_url() . 'ajax-loader.gif') ;
 
       $categories = new gc_dependent_select($crud, $fields, $config) ;
 
       /* Properties initial */
       $crud->display_as('ds_name','Condominiun name')
            ->display_as('ds_text','Description')
            ->display_as('ky_country','Country')
            ->display_as('ky_state','State')
            ->display_as('nu_codezip','Code zip')
            ->display_as('ds_address','Address')
            ->display_as('am_rentfee','Rent fee monthly')
            ->display_as('nu_building','# Building')
            ->display_as('nu_condom','# Condominium')
            ->display_as('am_billing','Billing monthly')
            ->display_as('sw_status','Status')
            ->display_as('dt_status','Date status')
            ->display_as('gm_urlplace','Location') ;
 
       $crud->field_type('dt_status','invisible') ;
       $crud->field_type('ds_text','text') ;
       /* Properties finish */
 
       $js = $categories->get_js();
       $output = $crud->render();
       $output->output.= $js;
       $this->_example_output($output);
    }

 

MY PROJECT PATH

 

C:\Projects\htdocs\condom2\site\CodeIgniter

............. \applications

..........................\controllers\Examples.php

............. \assets

............. \system


Valentín Lazbal

Valentín Lazbal
  • profile picture
  • Member

Posted 14 December 2016 - 12:15 PM

Hi!

Thanks you! This saved my life  :D

I needed to check a field against an array like a Where_in, and I don't know if this can be done with Where, so I added it.

Just add:

1) In  gc_dependent_select library's method function callback_edit($value, $primary_key, $field_info) at line 141:

if (!empty($this->fields[$field_info->name]['where_in'])) {
    $this->ci->db->where_in($this->fields[$field_info->name]['where_in']['where_in_field'], $this->fields[$field_info->name]['where_in']['where_in_array'], FALSE);
}

2) In gc_dependent_select library's method public function get_json() at line 300:

if (!empty($this->fields[$this->field_name]['where_in'])) {
   $this->ci->db->where_in($this->fields[$this->field_name]['where_in']['where_in_field'], $this->fields[$this->field_name]['where_in']['where_in_array'], FALSE);
}

3) In your controller

$fields=array(
 'first_dropdown' => array(
   'table_name'=>'first_table',
   'title'=>'id_first_table',
   'relate'=>null),
 'second_dropdown' => array(
   'table_name'=>'second_table',
   'title'=>'id',
   'id_field'=>'id',
   //This is new
   'where_in'=> array(
             'where_in_field'=>'field_to_check',
             'where_in_array'=>$array_with_values
             ),
   //end of the new thing
   'relate'=>'first_table_FK',
   'data-placeholder'=>'Select second dropdown')
);

man2000

man2000
  • profile picture
  • Member

Posted 17 February 2017 - 08:30 AM

There seem to be a bug in this plugin. If you try to save a post after you saved a post. The related selects are not showing correct values. That´s because of that the selects doesn´t clear after you hit the save button.

 

How to re-create this issue: Try saving multiple posts without going back to the list. The go back to the list and check the values saved.


sugarmoren

sugarmoren
  • profile picture
  • Member

Posted 24 May 2017 - 17:11 PM

Please help

 

The "Cargos" select not work

 

 

$this->load->library('gc_dependent_select');
            
            $fields = array(
 
            // first field:
            'idcorporation' => array( // first dropdown name
            'table_name' => 'corporation', // table of country
            'title' => 'name', // country title
            'relate' => null // the first dropdown hasn't a relation
            ),
            //second field
            'idjob_title' => array( // second dropdown name
            'table_name' => 'corp_job_title', // table of state
            'title' => 'title', // state title
            'id_field' => 'idjob_title', // table of state: primary key
            'relate' => 'idcorporation', // table of state:
            'data-placeholder' => 'select cargo' //dropdown's data-placeholder:
 
            )
            );
 
            $config = array(
            'main_table' => 'user',
            'main_table_primary' => 'iduser',
             "url" => base_url() .'cms/'.strtolower(__CLASS__) . '/' . __FUNCTION__ . '/',
            
            );
            $categories = new gc_dependent_select($crud, $fields, $config); 
 
            // Cracion interfaz
            $js = $categories->get_js();
            $output = $crud->render();
            $output->output.= $js;
 
 
            $this->_output_view('cms/general.php', $output);

 


Jorge Fuentes

Jorge Fuentes
  • profile picture
  • Member

Posted 14 September 2017 - 17:03 PM

I´m traying to use the library but I get this problem

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' dd_country.country_title as se3dbd3a3 FROM `dd_country` ORDER BY `se3dbd3a3`' at line 1

SELECT dd_country., dd_country.country_title as se3dbd3a3 FROM `dd_country` ORDER BY `se3dbd3a3`

 

 

Has anyone else had it? 

 

How did you solve it?

 

 

Thanks for you help

 

 


Jorge Fuentes

Jorge Fuentes
  • profile picture
  • Member

Posted 14 September 2017 - 17:03 PM

How do you solve it?


mariati2017

mariati2017
  • profile picture
  • Member

Posted 31 October 2017 - 10:47 AM

Hi there,

I hope somebody will help me!

I have a problem with my code. I am following the example code but I have the following error:

 

Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\OrgAqua\application\libraries\gc_dependent_select.php on line 138

 

I am using 1.5.1_gc_dependent_select.php and I am working with Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11.

 

Please let me know if you have ideas for the solution.

 

Thank you in advance!

 

MT

 

 

 

 

 


amurgiles

amurgiles
  • profile picture
  • Member

Posted 27 February 2018 - 16:05 PM

Hi Victor, please help my second dropdown list is not getting update when I chose an item from the first dropdown list. My array dough does catch the item and get update with the information for the second list, but the second list remains dissabled.


amurgiles

amurgiles
  • profile picture
  • Member

Posted 27 February 2018 - 16:16 PM

 

Please help

 

The "Cargos" select not work

 

 

$this->load->library('gc_dependent_select');
            
            $fields = array(
 
            // first field:
            'idcorporation' => array( // first dropdown name
            'table_name' => 'corporation', // table of country
            'title' => 'name', // country title
            'relate' => null // the first dropdown hasn't a relation
            ),
            //second field
            'idjob_title' => array( // second dropdown name
            'table_name' => 'corp_job_title', // table of state
            'title' => 'title', // state title
            'id_field' => 'idjob_title', // table of state: primary key
            'relate' => 'idcorporation', // table of state:
            'data-placeholder' => 'select cargo' //dropdown's data-placeholder:
 
            )
            );
 
            $config = array(
            'main_table' => 'user',
            'main_table_primary' => 'iduser',
             "url" => base_url() .'cms/'.strtolower(__CLASS__) . '/' . __FUNCTION__ . '/',
            
            );
            $categories = new gc_dependent_select($crud, $fields, $config); 
 
            // Cracion interfaz
            $js = $categories->get_js();
            $output = $crud->render();
            $output->output.= $js;
 
 
            $this->_output_view('cms/general.php', $output);

 

Hi, sugarmoren, did you find out how to solve it? I'm having the same problem


amurgiles

amurgiles
  • profile picture
  • Member

Posted 27 February 2018 - 17:10 PM

Hi, sugarmoren, did you find out how to solve it? I'm having the same problem

I solved it!!!

 

The problem was that I set print_r function, because I wanted to see the results of each variable, before $categories, I commented it and it worked!, conclusion, do not set anything before $categories!:

 

$config = array(
      'main_table' => 'usuario',
      'main_table_primary' => 'IDUSUARIO',
     "url" => base_url() .'index.php/'. __CLASS__ . '/' . __FUNCTION__ . '/',
    // 'segment_name' =>'Usuario'
        );
        //print_r ($crud);
        //print_r ($fields);
        //print_r ($config);

      $categories = new Gc_dependent_select($crud, $fields, $config);