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.
defined('BASEPATH') OR exit('No direct script access allowed');
class School extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function get_dictionary() {
$this->load->model('schools');
$this->load->model('trading_tools');
$data['meta_title'] = 'School';
$data['meta_keywords'] = 'School';
$data['meta_desc'] = 'School';
$data['signup_signin_model'] = 'root/signin_up_model_html';
$parent_slug = trim($this->uri->segment(2));
$child_slug = trim($this->uri->segment(3));
if (isset($child_slug) && $child_slug != '') {
$data = array($child_slug);
$where = 'slug_name';
$data['school_info'] = $this->schools->get_records_where_in('schools_category', $where, $data);
} else {
$data['school_info'] = $this->schools->get_school_info($parent_slug);
}
$menu = $this->trading_tools->get_items();
$data['side_bar_menu'] = $this->buildMenu(0, $menu);
$data['loginheader'] = '';
$data['top_header'] = 'root/top_header';
$data['top_menu'] = 'root/top_menu';
$data['parent_slug_name'] = $parent_slug;
$data['child_slug_name'] = $child_slug;
if ($parent_slug != 'glossary') {
$data['main'] = 'root/school_child';
} else {
$data['main'] = 'root/school_glossary';
}
$data['footer'] = 'root/footer';
$this->load->vars($data);
$this->load->view($this->template);
}
public function buildMenu($parent, $menu) {
$html = "";
if (isset($menu['parent_menus'][$parent])) {
if ($parent <= 0)
$html .= "<ul class='nav nav-pills nav-stacked' style='background:#E9E9E9;'>";
else
$html.= '<ul class="dropdown-menu">';
$countfor_active = 0;
foreach ($menu['parent_menus'][$parent] as $menu_id) {
$li_parentclass = '';
$ul_inside_li = '<ul class="dropdown-menu ' . $menu['menus'][$menu_id]['slug_name'] . '>';
if ($countfor_active == 0) {
$li_parentclass = 'dropdown ' . $menu['menus'][$menu_id]['slug_name'] . '';
}
if (!isset($menu['parent_menus'][$menu_id])) {
$html .= "<li class='" . $li_parentclass . "'>
<a href='" . $menu['menus'][$menu_id]['page_link'] . "'>
" . $menu['menus'][$menu_id]['category_name'] . "</a></li>";
}
if (isset($menu['parent_menus'][$menu_id])) {
$html .= "<li class='" . $li_parentclass . "'>
<a href='" . $menu['menus'][$menu_id]['page_link'] . "'> <i
class='fa fa-angle-right pull-right'></i>
" . $menu['menus'][$menu_id]['category_name'] . "</a>";
$html .= $this->buildMenu($menu_id, $menu);
$html .= "</li>";
}
}
$html .= "</ul>";
}
return $html;
}
}
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
<div class="l-main-container">
<div class="b-breadcrumbs f-breadcrumbs">
<div class="container">
<ul>
<li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown"
href="#"><i class="fa fa-home"></i>Home</a></li>
<li><i class="fa fa-angle-right"></i><a href="#">Forex Tools</a></li>
<li><i class="fa fa-angle-right"></i><span>Forex School</span></li>
</ul>
</div>
</div>
<div class="l-inner-page-container">
<div class="container">
<div class="row">
<div class="col-md-9 col-md-push-3">
<div class="b-shortcode-example">
<div class="b-tabs f-tabs j-tabs b-tabs-reset ui-tabs ui-widget ui-widget-content ui-corner-all">
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix
ui-widget-header ui-corner-all" role="tablist">
<?php if (count($school_info) > 0) { ?>
<?php
$alphabet = array('A'=>'glossary-a','B'=>'glossary-b','C'=>'glossary-c','D'=>'glossary-d',
'E'=>'glossary-e','F'=>'glossary-f','G'=>'glossary-g','H'=>
'glossary-h','I'=>'glossary-i','J'=>'glossary-j','K'=>'glossary-k',
'L'=>'glossary-l','M'=>'glossary-m','N'=>'glossary-n','O'=>
'glossary-o','P'=>'glossary-p','Q'=>'glossary-q','R'=>'glossary-r','S'=>'glossary-s'
,'T'=>'glossary-t','U'=>'glossary-u','V'=>'glossary-v','W'=>'glossary-w',
'X'=>'glossary-x','Y'=>'glossary-y','Z'=>'glossary-z');
foreach ($school_info as $key => $value) {?>
<li class="ui-state-default ui-corner-top" role="tab" tabindex="-1"
aria-controls="tabs-21" aria-labelledby="ui-id-1" aria-selected="false" aria-expanded="false">
<a href="#tabs-<?php echo $school_info[$key]->s_category_id;?>"
class="ui-tabs-anchor" role="presentation" tabindex="-1"
id="ui-id-<?php echo $school_info[$key]->s_category_id;?>">
<?php foreach ($alphabet as $al => $alp) {?>
<?php if($alp==$school_info[$key]->slug_name){ ?>
<?php echo $al; ?>
<?php }?>
<?php }?>
</a>
</li>
<?php } ?>               
<?php }?>
</ul>
<div class="b-tabs__content">
<?php if (count($school_info) > 0) { ?> 
<?php foreach ($school_info as $key => $value) {?>
<div id="tabs-<?php echo $school_info[$key]->s_category_id;?>"
aria-labelledby="ui-id-<?php echo $school_info[$key]->s_category_id;?>"
class="ui-tabs-panel ui-widget-content ui-corner-bottom" role="tabpanel" 
aria-hidden="true" style="display: none;">
<?php echo html_entity_decode($school_info[$key]->page_body); ?>
</div>
<?php }?>
<?php }?> 
</div>
</div>
</div>
</div>
<div class="visible-xs-block visible-sm-block b-hr"></div>
<div class="col-md-3 col-md-pull-9">
<?php if (isset($side_bar_menu)) { ?>
<?php echo $side_bar_menu; ?>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
var p_slug_name=".<?php echo $parent_slug_name;?>";
$(".nav").find(p_slug_name).addClass('active');
});
</script>
B) school_child View
<div class="l-main-container">
<div class="b-breadcrumbs f-breadcrumbs">
<div class="container">
<ul>
<li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown"
href="#"><i class="fa fa-home"></i>Home</a></li>
<li><i class="fa fa-angle-right"></i><a href="#">Forex Tools</a></li>
<li><i class="fa fa-angle-right"></i><span>Forex School</span></li>
</ul>
</div>
</div>
<div class="l-inner-page-container">
<div class="container">
<div class="row">  
<?php if (count($school_info) > 0) { ?>
<div class="col-md-9 pull-right"><h4><?php echo $school_info[0]->category_name;?></h4></div>
<?php } ?>
<div class="col-md-9 col-md-push-3">
<div class="b-blog-listing__block">
<div class="b-blog-listing__block-top"> </div>
<div class="b-infoblock-with-icon b-blog-listing__infoblock">
<div class="b-infoblock-with-icon__info f-infoblock-with-icon__info">
<?php if (count($school_info) > 0) { ?>
<?php echo html_entity_decode($school_info[0]->page_body); ?>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="visible-xs-block visible-sm-block b-hr"></div>
<div class="col-md-3 col-md-pull-9">
<?php if (isset($side_bar_menu)) { ?>
<?php echo $side_bar_menu; ?>
<?php } ?> 
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
var p_slug_name=".<?php echo $parent_slug_name;?>";
$(".nav").find(p_slug_name).addClass('active');
});
</script>
3. Your model function will goes like below
class Schools extends CI_Model {
function __construct() {
// Call the Model constructor
parent::__construct();
$this->load->library('session');
$this->load->database();
}
function get_school_info($parent_slug){
$q_school="select * from cbf_schools_category where parentid =(select s_category_id
from cbf_schools_category where slug_name='".$parent_slug."')";
$query=$this->db->query($q_school);
if ($query->num_rows() > 0)
return $query->result();
else
$q_get_main="select * from cbf_schools_category where slug_name='".$parent_slug."'";
$query1=$this->db->query($q_get_main);
return $query1->result();
}
function get_records_where_in($table, $where, $data) {
$this->db->where_in($where, $data);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
return $query->result();
else
return NULL;
}
}
4. You will need to define a routes in your routes.php
$route['school/(:any)'] = 'school/get_dictionary/$1';
5. Your Mysql table goes like below which will contain parent , child relationship in single table.