⚠ 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

[EXTRA] Categories and Subcategories Dropdown list.



web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 12 April 2012 - 21:17 PM

This is a customization without changing the core of grocery_CRUD.

In the end of this tutorial you will have a result that will look like this: [attachment=101:categories-and-sub.png]

Everything starts from the request of [member='vnt'] at: http://www.grocerycr...-grocery-croud/

So a quick solution for this is the below code ( You can also find the below code as a file at: [attachment=107:examples-categories-sub-categories.php]):


function categorie()
{
$crud = new grocery_CRUD();

$crud->set_table('vnt_categorie');

/* Add this customisation for parent_relation */
$primary_id_field = 'categoria_id';
$table_name = 'vnt_categorie';
$relation_field_name = 'parent_id';
$parent_field = 'parent_id';
$title_field = 'categoria';
$title_display_as = 'Select Categoria';
$where = array('stato'=>'1');//not required
$same_table = true; //not required, the default is false

$this->set_parent_relation($primary_id_field,$table_name,$parent_field,$title_field,$crud,$title_display_as,$relation_field_name,$where,$same_table);
/* ------------------------------------------- */

$output = $crud->render();

$this->_example_output($output);
}

function set_parent_relation($primary_id_field,$table_name,$parent_field,$title_field,$crud,$title_display_as,$relation_field_name,$where = null,$same_table = false)
{
$this->gc_id = $primary_id_field;
$this->gc_table = $table_name;
$this->gc_field_name = $relation_field_name;
$this->gc_parent = $parent_field;
$this->gc_title = $title_field;
$this->gc_where = $where;
$this->gc_title_display_as = $title_display_as;
$this->gc_same_table = $same_table;

$crud->set_css('assets/grocery_crud/css/jquery_plugins/chosen/chosen.css');
$crud->set_js('assets/grocery_crud/js/jquery-1.7.1.min.js');
$crud->set_js('assets/grocery_crud/js/jquery_plugins/jquery.chosen.min.js');
$crud->set_js('assets/grocery_crud/js/jquery_plugins/ajax-chosen.js');
$crud->set_js('assets/grocery_crud/js/jquery_plugins/config/jquery.chosen.config.js');

$crud->set_relation($this->gc_field_name,$this->gc_table,$this->gc_title);

$crud->callback_field($this->gc_field_name,array($this,'parent_relation_callback'));
}

function parent_relation_callback($value = '', $primary = null)
{
if(!empty($value))
{
$this->gc_history = $this->get_parent_history($value);
}

$return_string = '<script type="text/javascript"> var ajax_relation_url = ""; </script>';
$return_string .= '<select name="'.$this->gc_field_name.'" class="chosen-select" data-placeholder="'.$this->gc_title_display_as.'"><option value=""></option>';
$return_string .= $this->parent_repeat(null,-1,$value);
$return_string .= '</select>';

return $return_string;
}

function parent_repeat($parent = null, $tree_level = -1, $value = '')
{
$return_string = '';
if($this->gc_where !== null)
$this->db->where($this->gc_where);
if($parent === null)
$this->db->where($this->gc_parent. ' IS NULL');
else
$this->db->where($this->gc_parent,$parent);
$db_result = $this->db->get($this->gc_table);

if($db_result->num_rows() > 0)
{
$tree_level++;
foreach($db_result->result() as $result)
{
$tree_string = $tree_level != 0 ? '|'.str_repeat('-',$tree_level*4) : '';
$selected = $value !== '' && $value == $result->{$this->gc_id} ? 'selected = "selected"' : '';
$disabled = $this->gc_same_table && !empty($value) && in_array($result->{$this->gc_id},$this->gc_history) ? 'disabled="disabled"' : '';

$return_string .= "<option value='".$result->{$this->gc_id}."' {$selected} {$disabled} >{$tree_string} ".$result->{$this->gc_title}."</option>";
$return_string .= $this->parent_repeat($result->{$this->gc_id}, $tree_level, $value);
}
}
return $return_string;
}

function get_parent_history($id)
{
$history = array();

$this->db->where($this->gc_id,$id);
$result = $this->db->get($this->gc_table)->row();

$history[] = $result->{$this->gc_id};

while(!empty($result->{$this->gc_parent}))
{
$this->db->where($this->gc_id,$result->{$this->gc_parent});
$result = $this->db->get($this->gc_table)->row();

$history[] = $result->{$this->gc_id};
}

return $history;
}



after this you can use the the parent relation by simply adding to your project this simple lines of code:



/* Add this customisation for parent_relation */
$primary_id_field = 'categoria_id';
$table_name = 'vnt_categorie';
$relation_field_name = 'parent_id';
$parent_field = 'parent_id';
$title_field = 'categoria';
$title_display_as = 'Select Categoria';
$where = array('stato'=>'1');//not required
$same_table = true; //not required, the default is false

$this->set_parent_relation($primary_id_field,$table_name,$parent_field,$title_field,$crud,$title_display_as,$relation_field_name,$where,$same_table);
/* ------------------------------------------- */


And tadaaaaaa you will have something similar to this: [attachment=101:categories-and-sub.png]

let me explain:

$primary_id_field is the primary key of your table (in our case categoria_id)
$table_name is the name of our table (in our case vnt_categorie)
$parent_field is the parent field name for example parent_id (in our case the name is parent_id)
$title_field is the title field that you want to appear from the table (in our case categoria)
$title_display_as the string that it will show for example to select the categories (in our case 'Select Categoria' )
$where is the where field . It works like the where of the set_relation. You can simply add it as a null if you don't want to add.

carlinchisart

carlinchisart
  • profile picture
  • Member

Posted 15 April 2012 - 17:47 PM

hi @web-johny i try to use this becouse i have to do a select to contry and region, but i don't understand, i try but i don't know becouse my not is the contry or the region, my table is one table that i have one ralation with region, but my client want to display contry and after the region, i can use this?

carlinchisart

carlinchisart
  • profile picture
  • Member

Posted 15 April 2012 - 17:52 PM

@web-jhonny i found this http://demo.edynamics.co.za/grocery_crud/index.php/examples/customers_management in this topic /topic/72-categories-and-subcategories/page__p__817__hl__country__fromsearch__1#entry817

so i'm going to do this, sorry for the reply.

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 15 April 2012 - 17:58 PM

Yes this is the one that you want to use for this. My example is for a different case.

brunohauck

brunohauck
  • profile picture
  • Member

Posted 09 July 2012 - 11:16 AM

I’m having a problems with this code for a product and categories tables.

First I have a table called produto with a fk prod_categorias_id and than I have a table categorias with pk cat_id and when I try to edit a item in the table produto is giving this error:


[b] Ocorreu um erro no banco de dados[/b]

Error Number: 1054
Unknown column 'prod_categoria_id' in 'where clause'
SELECT * FROM (`categorias`) WHERE `prod_categoria_id` IS NULL
Filename: /hermes/bosweb/web103/b1036/ipg.idsgeocom/softwareon/dev/controllers/crud.php
Line Number: 268

This is the code that I’m using:

/* Add this customisation for parent_relation */
$primary_id_field = 'cat_id';
$table_name = 'categorias';
$relation_field_name = 'prod_categoria_id';
$parent_field = 'prod_categoria_id';
$title_field = 'cat_nome';
$title_display_as = 'Selecione uma categoria';
//$where = array('stato'=>'1');//not required
$same_table = false; //not required, the default is false

$this->set_parent_relation($primary_id_field,$table_name,$parent_field,$title_field,$crud,$title_display_as,$relation_field_name,$where,$same_table);

fdias

fdias
  • profile picture
  • Member

Posted 09 July 2012 - 23:27 PM

The error message is quite clear:

[b]Unknown column 'prod_categoria_id'[/b]

That means you probably misspelled the columns name.

brunohauck

brunohauck
  • profile picture
  • Member

Posted 10 July 2012 - 00:35 AM

But I want to know what is the name of column that I put in "$relation_field_name" ?

I think that is the error.

brunohauck

brunohauck
  • profile picture
  • Member

Posted 12 July 2012 - 11:06 AM

I already solved the problem using 1 N relationship.
Right now I’m having problems with the uploads files. When I try to upload the system return 1 and don’t upload the file and I will open another topic for this issue .
Thanks.

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 20 July 2012 - 06:18 AM

[quote name='brunohauck' timestamp='1342091188' post='2663']
I already solved the problem using 1 N relationship.
Right now I’m having problems with the uploads files. When I try to upload the system return 1 and don’t upload the file and I will open another topic for this issue .
Thanks.
[/quote]

Hello [member='brunohauck']

as for the upload error you can check this post: /topic/263-just-a-heads-up-for-uploading/

Bogdan Ciprian

Bogdan Ciprian
  • profile picture
  • Member

Posted 12 October 2012 - 20:54 PM

In the latest version following lines must be removed to make jQuery run properly :


$crud->set_css('assets/grocery_crud/css/jquery_plugins/chosen/chosen.css');
$crud->set_js('assets/grocery_crud/js/jquery-1.7.1.min.js');
$crud->set_js('assets/grocery_crud/js/jquery_plugins/jquery.chosen.min.js');
$crud->set_js('assets/grocery_crud/js/jquery_plugins/ajax-chosen.js');
$crud->set_js('assets/grocery_crud/js/jquery_plugins/config/jquery.chosen.config.js');

Hamed

Hamed
  • profile picture
  • Member

Posted 09 January 2013 - 05:51 AM

I get sth like:
[attachment=428:cdi.jpg]

I want to change cid to "Category" and also use chozen plugin.

Hamed

Hamed
  • profile picture
  • Member

Posted 12 January 2013 - 12:49 PM

I think this problem is the same with:/topic/1219-fixing-persian-datepicker-ui-problem/

tlc033

tlc033
  • profile picture
  • Member

Posted 06 March 2013 - 00:53 AM

Hi. Somebody managed to use this code on GC 1.3.3 or newest ? Can share table structure ?

 

I try to use it on my project but without success.

Please.


slav123

slav123
  • profile picture
  • Member

Posted 15 April 2014 - 02:21 AM

Works like a charm !

 

Code is correct, just need to make small amends in JS section:

$crud->set_css('assets/grocery_crud/css/jquery_plugins/chosen/chosen.css');
$crud->set_js('assets/grocery_crud/js/jquery-1.10.2.min.js');

to run latest jQuery,

 

database structure just require primary key - like 'id', parent key indicator = 'parent_id' and generic column name 'name' then you have fields like:

/* Add this customisation for parent_relation */
$primary_id_field    = 'id';
$table_name          = 'navigation';
$relation_field_name = 'parent_id';
$parent_field        = 'parent_id';
$title_field         = 'name';
$title_display_as    = 'Chose parent category';

You can checkout this Gist


gede bhama

gede bhama
  • profile picture
  • Member

Posted 05 August 2014 - 06:12 AM

could you display the database table structure you were used ?

Iam newbie use grocery_CRUD, i am trying with my table

 

my table name is "menuku" and the table structure is :

id ->autoincreament

id_parent ->int(10)

menu -> varchar(100)

 

And my data look like this :

 

view.png

 

and if me add record show error like this

 

error.png

 

I am used code :

================================================================

function categorie()
    {
      $crud = new grocery_CRUD();
    
      $crud->set_table('menuku');
    
      /* Add this customisation for parent_relation */
      $primary_id_field  = 'id';
      $table_name   = 'menuku';
      $relation_field_name = 'id_parent';
      $parent_field   = 'id_parent';
      $title_field   = 'id_parent';
      $title_display_as  = 'Select id_parent';
      $where    = array('stato'=>'1');//not required
      $same_table   = true; //not required, the default is false
     $crud->columns('id','id_parent','menu');
      $this->set_parent_relation($primary_id_field,$table_name,$parent_field,$title_field,$crud,$title_display_as,$relation_field_name,$where,$same_table);
      /* ------------------------------------------- */
    
      $output = $crud->render();
    
      $this->_example_output($output);
    }
    
    function set_parent_relation($primary_id_field,$table_name,$parent_field,$title_field,$crud,$title_display_as,$relation_field_name,$where = null,$same_table = false)
    {
      $this->gc_id    = $primary_id_field;
      $this->gc_table    = $table_name;
      $this->gc_field_name  = $relation_field_name;
      $this->gc_parent   = $parent_field;
      $this->gc_title   = $title_field;
      $this->gc_where   = $where;
      $this->gc_title_display_as  = $title_display_as;
      $this->gc_same_table  = $same_table;
    
      $crud->set_css('assets/grocery_crud/css/jquery_plugins/chosen/chosen.css');
      $crud->set_js('assets/grocery_crud/js/jquery-1.7.1.min.js');
      $crud->set_js('assets/grocery_crud/js/jquery_plugins/jquery.chosen.min.js');
      $crud->set_js('assets/grocery_crud/js/jquery_plugins/ajax-chosen.js');
      $crud->set_js('assets/grocery_crud/js/jquery_plugins/config/jquery.chosen.config.js');
    
      $crud->set_relation($this->gc_field_name,$this->gc_table,$this->gc_title);
    
      $crud->callback_field($this->gc_field_name,array($this,'parent_relation_callback'));
    }
    
    function parent_relation_callback($value = '', $primary = null)
    {
      if(!empty($value))
      {
       $this->gc_history = $this->get_parent_history($value);
      }
    
      $return_string  = '<script type="text/javascript"> var ajax_relation_url = ""; </script>';
      $return_string  .= '<select name="'.$this->gc_field_name.'" class="chosen-select" data-placeholder="'.$this->gc_title_display_as.'"><option value=""></option>';
      $return_string  .= $this->parent_repeat(null,-1,$value);
      $return_string  .= '</select>';
    
      return $return_string;
    }
    
    function parent_repeat($parent = null, $tree_level = -1, $value = '')
    {
      $return_string = '';
      if($this->gc_where !== null)
       $this->db->where($this->gc_where);
      if($parent === null)
       $this->db->where($this->gc_parent. ' IS NULL');
      else
       $this->db->where($this->gc_parent,$parent);
      $db_result = $this->db->get($this->gc_table);
    
      if($db_result->num_rows() > 0)
      {
       $tree_level++;
       foreach($db_result->result() as $result)
       {  
        $tree_string = $tree_level != 0 ? '|'.str_repeat('-',$tree_level*4) : '';
        $selected = $value !== '' && $value == $result->{$this->gc_id} ? 'selected = "selected"' : '';
        $disabled = $this->gc_same_table && !empty($value) &&  in_array($result->{$this->gc_id},$this->gc_history) ? 'disabled="disabled"' : '';
       
        $return_string .= "<option value='".$result->{$this->gc_id}."' {$selected} {$disabled} >{$tree_string} ".$result->{$this->gc_title}."</option>";
        $return_string .= $this->parent_repeat($result->{$this->gc_id}, $tree_level, $value);
       }
      }
      return $return_string;
    }
    
    function get_parent_history($id)
    {
      $history = array();
    
      $this->db->where($this->gc_id,$id);
      $result = $this->db->get($this->gc_table)->row();
    
      $history[] = $result->{$this->gc_id};
    
      while(!empty($result->{$this->gc_parent}))
      {
       $this->db->where($this->gc_id,$result->{$this->gc_parent});
       $result = $this->db->get($this->gc_table)->row();
      
       $history[] = $result->{$this->gc_id};
      }
    
      return $history;
    }

 

Please help me, Iam confuse...


gede bhama

gede bhama
  • profile picture
  • Member

Posted 05 August 2014 - 16:01 PM

Ok,, I Have fixed it

thank grocery_CRUD

:)


Sonelal Singh

Sonelal Singh
  • profile picture
  • Member

Posted 21 September 2015 - 07:48 AM

Hi web-johnny,

 

How can I use your code when we have to use category with multiple fields(for example editing user info).

 

For example - 

 

users table have following  fields --->user_id,name,gender,category

 

and category table have following fields ---> id,category,parent_id

 

 

       $this->load->library('grocery_CRUD');
 
        $crud = new grocery_CRUD();
        $crud->set_theme('datatables');
        $crud->set_table('users');
        $crud->set_subject('Users');
        $crud->set_relation('category', 'ci_categories', 'category');

 

 
 
       /* Add this customisation for parent_relation */
 
        $primary_id_field  = 'id';
        $table_name   = 'categories';
        $relation_field_name = 'parent_id';
        $parent_field   = 'parent_id';
        $title_field   = 'category';
        $title_display_as  = 'Select Categoria';
        $where    = array('stato'=>'1');//not required
 
        $same_table   = true; //not required, the default is false
 
        $this->set_parent_relation($primary_id_field,$table_name,$parent_field,$title_field,$crud,$title_display_as,$relation_field_name,$where,$same_table);
       
        
        $output['output'] = $crud->render();

        $this->load->view('template', $output);

 

 

I am not able to figure out how can I use category field with other tables.

 

 

 

 

 


jgalak1

jgalak1
  • profile picture
  • Member

Posted 02 July 2017 - 17:48 PM

Does this work on current version (1.5.8)?


jgalak1

jgalak1
  • profile picture
  • Member

Posted 02 July 2017 - 18:01 PM

Trying to follow this from the code.  Is this only useful for 1 to n relationships?  Or can it be used for n to n relationship?  I need both for a project I'm working on.


jgalak1

jgalak1
  • profile picture
  • Member

Posted 02 July 2017 - 19:14 PM

So I tried this code.  List view works fine.  But when I click "Edit", it does two things:

 

1) it throws errors which seem to originate in:

    function parent_repeat($parent = null, $tree_level = -1, $value = '')
    {
        $return_string = '';
        if($this->gc_where !== null)
            $this->db->where($this->gc_where);
        if($parent === null)
            $this->db->where($this->gc_parent. ' IS NULL');
        else
            $this->db->where($this->gc_parent,$parent);
        $db_result = $this->db->get($this->gc_table);

        if($db_result->num_rows() > 0)
        {
            $tree_level++;
            foreach($db_result->result() as $result)
            {
                $tree_string = $tree_level != 0 ? '|'.str_repeat('-',$tree_level*4) : '';
                $selected = $value !== '' && $value == $result->{$this->gc_id} ? 'selected = "selected"' : ''; //HERE!!!!
                $disabled = $this->gc_same_table && !empty($value) &&  in_array($result->{$this->gc_id},$this->gc_history) ? 'disabled="disabled"' : '';

                $return_string .= "<option value='".$result->{$this->gc_id}."' {$selected} {$disabled} >{$tree_string} ".$result->{$this->gc_title}."</option>";
                $return_string .= $this->parent_repeat($result->{$this->gc_id}, $tree_level, $value);
            }
        }

        return $return_string;
    }

in the line tagged "HERE", complaining that 'this->gc_id' is not set.

 

2) and then it crashes my firefox browser.....

 

Any ideas?