Jump to content

via Ad Packs

[EXTRA] Categories and Subcategories Dropdown list.


  • Please log in to reply
12 replies to this topic

#1 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 858 posts
  • LocationLondon

Posted 12 April 2012 - 09: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: categories-and-sub.png

Everything starts from the request of 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: Attached File  examples-categories-sub-categories.php   4.43K   301 downloads):

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: 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.

Attached Thumbnails

  • categories-and-sub.png

Posted Image

#2 carlinchisart

carlinchisart

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts

Posted 15 April 2012 - 05: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?

#3 carlinchisart

carlinchisart

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts

Posted 15 April 2012 - 05:52 PM

@web-jhonny i found this http://demo.edynamic...mers_management in this topic http://www.grocerycr...rch__1#entry817

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

#4 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 858 posts
  • LocationLondon

Posted 15 April 2012 - 05:58 PM

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

#5 brunohauck

brunohauck

    Member

  • Members
  • PipPip
  • 12 posts

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:


Ocorreu um erro no banco de dados

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

#6 fdias

fdias

    grocery CRUD Addict

  • Members
  • PipPipPip
  • 87 posts

Posted 09 July 2012 - 11:27 PM

The error message is quite clear:

Unknown column 'prod_categoria_id'

That means you probably misspelled the columns name.

#7 brunohauck

brunohauck

    Member

  • Members
  • PipPip
  • 12 posts

Posted 10 July 2012 - 12: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.

#8 brunohauck

brunohauck

    Member

  • Members
  • PipPip
  • 12 posts

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.

#9 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 858 posts
  • LocationLondon

Posted 20 July 2012 - 06:18 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.


Hello brunohauck

as for the upload error you can check this post: http://www.grocerycr...-for-uploading/
Posted Image

#10 Bogdan Ciprian

Bogdan Ciprian

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 12 October 2012 - 08: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');


#11 Hamed

Hamed

    Member

  • Members
  • PipPip
  • 25 posts

Posted 09 January 2013 - 05:51 AM

I get sth like:
cdi.jpg

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

#12 Hamed

Hamed

    Member

  • Members
  • PipPip
  • 25 posts

Posted 12 January 2013 - 12:49 PM

I think this problem is the same with:http://www.grocerycrud.com/forums/topic/1219-fixing-persian-datepicker-ui-problem/

#13 tlc033

tlc033

    Advanced Member

  • Members
  • PipPipPip
  • 55 posts
  • LocationChisinau

Posted 06 March 2013 - 12: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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users