Saturday, 26 December 2015

Codeigniter Categories sub categories dictionary

Some times you want categoriesOr i can say that a dictionary which needs to be fetch from mysql , there for all links , content will be fetch from database mysql by creating a singel view file with support of jQuery for processing menus. There will be your Administration OR Admin panelwho will create these categories , sub categories.
 
1.Your Controller functions will goes like below.
  1. defined('BASEPATH') OR exit('No direct script access allowed');
  2. class School extends MY_Controller {
  3. public function __construct() {
  4. parent::__construct();
  5. }
  6. public function get_dictionary() {
  7. $this->load->model('schools');
  8. $this->load->model('trading_tools');
  9. $data['meta_title'] = 'School';
  10. $data['meta_keywords'] = 'School';
  11. $data['meta_desc'] = 'School';
  12. $data['signup_signin_model'] = 'root/signin_up_model_html';
  13. $parent_slug = trim($this->uri->segment(2));
  14. $child_slug = trim($this->uri->segment(3));
  15. if (isset($child_slug) && $child_slug != '') {
  16. $data = array($child_slug);
  17. $where = 'slug_name';
  18. $data['school_info'] = $this->schools->get_records_where_in('schools_category', $where, $data);
  19. } else {
  20. $data['school_info'] = $this->schools->get_school_info($parent_slug);
  21. }
  22. $menu = $this->trading_tools->get_items();
  23. $data['side_bar_menu'] = $this->buildMenu(0, $menu);
  24. $data['loginheader'] = '';
  25. $data['top_header'] = 'root/top_header';
  26. $data['top_menu'] = 'root/top_menu';
  27. $data['parent_slug_name'] = $parent_slug;
  28. $data['child_slug_name'] = $child_slug;
  29. if ($parent_slug != 'glossary') {
  30. $data['main'] = 'root/school_child';
  31. } else {
  32. $data['main'] = 'root/school_glossary';
  33. }
  34. $data['footer'] = 'root/footer';
  35. $this->load->vars($data);
  36. $this->load->view($this->template);
  37. }
  38. public function buildMenu($parent, $menu) {
  39. $html = "";
  40. if (isset($menu['parent_menus'][$parent])) {
  41. if ($parent <= 0)
  42. $html .= "<ul class='nav nav-pills nav-stacked' style='background:#E9E9E9;'>";
  43. else
  44. $html.= '<ul class="dropdown-menu">';
  45. $countfor_active = 0;
  46. foreach ($menu['parent_menus'][$parent] as $menu_id) {
  47. $li_parentclass = '';
  48. $ul_inside_li = '<ul class="dropdown-menu ' . $menu['menus'][$menu_id]['slug_name'] . '>';
  49. if ($countfor_active == 0) {
  50. $li_parentclass = 'dropdown ' . $menu['menus'][$menu_id]['slug_name'] . '';
  51. }
  52. if (!isset($menu['parent_menus'][$menu_id])) {
  53. $html .= "<li class='" . $li_parentclass . "'>
  54. <a href='" . $menu['menus'][$menu_id]['page_link'] . "'>
  55. " . $menu['menus'][$menu_id]['category_name'] . "</a></li>";
  56. }
  57. if (isset($menu['parent_menus'][$menu_id])) {
  58. $html .= "<li class='" . $li_parentclass . "'>
  59. <a href='" . $menu['menus'][$menu_id]['page_link'] . "'> <i
  60. class='fa fa-angle-right pull-right'></i>
  61. " . $menu['menus'][$menu_id]['category_name'] . "</a>";
  62. $html .= $this->buildMenu($menu_id, $menu);
  63. $html .= "</li>";
  64. }
  65. }
  66. $html .= "</ul>";
  67. }
  68. return $html;
  69. }
  70. }
2. Basically i am using two views filesbecause layout OR html is different for both. One view file will contain Dictionary (school_glossary) and sencond view (school_child) file will contain simple content to display.
 
A) school_glossary View
  1. <div class="l-main-container">
  2. <div class="b-breadcrumbs f-breadcrumbs">
  3. <div class="container">
  4. <ul>
  5. <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown"
  6. href="#"><i class="fa fa-home"></i>Home</a></li>
  7. <li><i class="fa fa-angle-right"></i><a href="#">Forex Tools</a></li>
  8. <li><i class="fa fa-angle-right"></i><span>Forex School</span></li>
  9. </ul>
  10. </div>
  11. </div>
  12. <div class="l-inner-page-container">
  13. <div class="container">
  14. <div class="row">
  15. <div class="col-md-9 col-md-push-3">
  16. <div class="b-shortcode-example">
  17. <div class="b-tabs f-tabs j-tabs b-tabs-reset ui-tabs ui-widget ui-widget-content ui-corner-all">
  18. <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix
  19. ui-widget-header ui-corner-all" role="tablist">
  20. <?php if (count($school_info) > 0) { ?>
  21. <?php
  22. $alphabet = array('A'=>'glossary-a','B'=>'glossary-b','C'=>'glossary-c','D'=>'glossary-d',
  23. 'E'=>'glossary-e','F'=>'glossary-f','G'=>'glossary-g','H'=>
  24. 'glossary-h','I'=>'glossary-i','J'=>'glossary-j','K'=>'glossary-k',
  25. 'L'=>'glossary-l','M'=>'glossary-m','N'=>'glossary-n','O'=>
  26. 'glossary-o','P'=>'glossary-p','Q'=>'glossary-q','R'=>'glossary-r','S'=>'glossary-s'
  27. ,'T'=>'glossary-t','U'=>'glossary-u','V'=>'glossary-v','W'=>'glossary-w',
  28. 'X'=>'glossary-x','Y'=>'glossary-y','Z'=>'glossary-z');
  29. foreach ($school_info as $key => $value) {?>
  30. <li class="ui-state-default ui-corner-top" role="tab" tabindex="-1"
  31. aria-controls="tabs-21" aria-labelledby="ui-id-1" aria-selected="false" aria-expanded="false">
  32. <a href="#tabs-<?php echo $school_info[$key]->s_category_id;?>"
  33. class="ui-tabs-anchor" role="presentation" tabindex="-1"
  34. id="ui-id-<?php echo $school_info[$key]->s_category_id;?>">
  35. <?php foreach ($alphabet as $al => $alp) {?>
  36. <?php if($alp==$school_info[$key]->slug_name){ ?>
  37. <?php echo $al; ?>
  38. <?php }?>
  39. <?php }?>
  40. </a>
  41. </li>
  42. <?php } ?>
  43. <?php }?>
  44. </ul>
  45. <div class="b-tabs__content">
  46. <?php if (count($school_info) > 0) { ?>
  47. <?php foreach ($school_info as $key => $value) {?>
  48. <div id="tabs-<?php echo $school_info[$key]->s_category_id;?>"
  49. aria-labelledby="ui-id-<?php echo $school_info[$key]->s_category_id;?>"
  50. class="ui-tabs-panel ui-widget-content ui-corner-bottom" role="tabpanel"
  51. aria-hidden="true" style="display: none;">
  52. <?php echo html_entity_decode($school_info[$key]->page_body); ?>
  53. </div>
  54. <?php }?>
  55. <?php }?>
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. <div class="visible-xs-block visible-sm-block b-hr"></div>
  61. <div class="col-md-3 col-md-pull-9">
  62. <?php if (isset($side_bar_menu)) { ?>
  63. <?php echo $side_bar_menu; ?>
  64. <?php } ?>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. <script>
  71. $(document).ready(function () {
  72. var p_slug_name=".<?php echo $parent_slug_name;?>";
  73. $(".nav").find(p_slug_name).addClass('active');
  74. });
  75. </script>
B) school_child View
  1. <div class="l-main-container">
  2. <div class="b-breadcrumbs f-breadcrumbs">
  3. <div class="container">
  4. <ul>
  5. <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown"
  6. href="#"><i class="fa fa-home"></i>Home</a></li>
  7. <li><i class="fa fa-angle-right"></i><a href="#">Forex Tools</a></li>
  8. <li><i class="fa fa-angle-right"></i><span>Forex School</span></li>
  9. </ul>
  10. </div>
  11. </div>
  12. <div class="l-inner-page-container">
  13. <div class="container">
  14. <div class="row">
  15. <?php if (count($school_info) > 0) { ?>
  16. <div class="col-md-9 pull-right"><h4><?php echo $school_info[0]->category_name;?></h4></div>
  17. <?php } ?>
  18. <div class="col-md-9 col-md-push-3">
  19. <div class="b-blog-listing__block">
  20. <div class="b-blog-listing__block-top"> </div>
  21. <div class="b-infoblock-with-icon b-blog-listing__infoblock">
  22. <div class="b-infoblock-with-icon__info f-infoblock-with-icon__info">
  23. <?php if (count($school_info) > 0) { ?>
  24. <?php echo html_entity_decode($school_info[0]->page_body); ?>
  25. <?php } ?>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. <div class="visible-xs-block visible-sm-block b-hr"></div>
  31. <div class="col-md-3 col-md-pull-9">
  32. <?php if (isset($side_bar_menu)) { ?>
  33. <?php echo $side_bar_menu; ?>
  34. <?php } ?>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. <script>
  41. $(document).ready(function () {
  42. var p_slug_name=".<?php echo $parent_slug_name;?>";
  43. $(".nav").find(p_slug_name).addClass('active');
  44. });
  45. </script>
3. Your model function will goes like below
  1. class Schools extends CI_Model {
  2. function __construct() {
  3. // Call the Model constructor
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->database();
  7. }
  8. function get_school_info($parent_slug){
  9. $q_school="select * from cbf_schools_category where parentid =(select s_category_id
  10. from cbf_schools_category where slug_name='".$parent_slug."')";
  11. $query=$this->db->query($q_school);
  12. if ($query->num_rows() > 0)
  13. return $query->result();
  14. else
  15. $q_get_main="select * from cbf_schools_category where slug_name='".$parent_slug."'";
  16. $query1=$this->db->query($q_get_main);
  17. return $query1->result();
  18. }
  19. function get_records_where_in($table, $where, $data) {
  20. $this->db->where_in($where, $data);
  21. $query = $this->db->get($table);
  22. if ($query->num_rows() > 0)
  23. return $query->result();
  24. else
  25. return NULL;
  26. }
  27. }
4. You will need to define a routes in your routes.php
  1. $route['school/(:any)'] = 'school/get_dictionary/$1';
5. Your Mysql table goes like below which will contain parent , child relationship in single table.