Hello guyyz i need help in design,i don't have ideas about design, i have created dynamic menu in codeigniter i want to set that menu in treeview.
First click on parent menu after open child menus, so how to implement it help me
Here is My View Code:
<?php
foreach ($test as $val) {
$array = explode(",", $val->category_id);
}
foreach ($get_cat as $key => $value) {
if (in_array($value->category_id, $array)) {
echo $value->category_name . ' '; //Here My Menu Print
}
}
?>
You need to fetch all category with parent_id=0 then fetch subCategory. Try something like given below.
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id', 0);
//Add here role condition
$parent = $this->db->get();
$categories = $parent->result();
$i=0;
foreach($categories as $p_cat){
$categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
$i++;
}
return $categories;
}
Your subcategory function.
public function sub_categories($id){
$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id', $id);
//add here role condition
$child = $this->db->get();
$categories = $child->result();
$i=0;
foreach($categories as $p_cat){
$categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
$i++;
}
return $categories;
}
And your controller.
public function categories(){
$this->load->model('model_categories');
$data = $this->model_categories->get_categories();
print_r($data);
}
Here I got solution of dynamic menu design in CodeIgniter:
<?php
//GET CATEGORY ID FROM USER REGISTARTION
foreach ($test as $val) {
$array = explode(",", $val->category_id);
//CATEGORY ID MATCH WITH CATEGORY NAME FROM CATEGORY TABLE
foreach ($listMenuLevel1 as $key => $value) {
if (in_array($value->category_id, $array)) {
?>
<ul class="sidebar-menu">
<li class="treeview">
<a href="#">
<i class="fa fa-share"></i> <span><?php echo $value->category_name; ?></span>
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<?php foreach ($this->main_model->listchildMenus($value->category_id) as $menu2) : ?>
<li class="treeview">
<i class="fa fa-circle-o"></i><?php echo $menu2->category_name; ?><i class="fa fa-angle-left pull-right"></i>
<?php foreach ($this->main_model->listchildMenus($menu2->category_id) as $menu3): ?>
<ul class="treeview-menu">
<li class="treeview">
<i class="fa fa-circle-o"></i><?php echo $menu3->category_name; ?>
</li>
</ul>
<?php endforeach; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
</ul>
<?php } ?>
<?php
}
}
?>
Related
Ok, I have done lots of research and looked at lots of stackoverflow questions but none have answered my question.
I'm building a simple blog and am at the moment i'm trying to build a simple categories/subcategories system but have hit an obstacle in making the subcategories load under their parent. If you know an easier method to the one i am doing below please let me know.
Here is how my db is structured:
id || name || parent_id || status
------------------------------------------
1 || category1 || NULL || 1
2 || subcategory1 || 1 || 1
Then i have my model code:
public function getCategories()
{
$results = $this->db->select('msi_items_categories','status = 1 AND parent_id = NULL');
if( !is_array($results[0]) ) {
$new_results = array();
array_push($new_results, $results);
return $new_results;
} else {
return $results;
}
}
public function getSubCategories($parent)
{
$bind = [':parent' => $parent];
$results = $this->db->select('msi_items_categories','status = 1 AND parent_id = :parent');
if( !is_array($results[0]) ) {
$new_results = array();
array_push($new_results, $results);
return $new_results;
} else {
return $results;
}
}
This is my controller code:
public function error()
{
$getSettings = $this->setting->getAll();
$getCategories = $this->setting->getCategories();
$getSubCategories = $this->setting->getSubCategories();
if(is_array($getCategories[0]) ) {
$isCategory = true;
} else {
$isCategory = false;
}
if(is_array($getSubCategories[0]) ) {
$isSubCategory = true;
} else {
$isSubCategory = false;
}
$data = [
'settings' => $getSettings,
'mainCategory' => $getCategories,
'subCategory' => $getSubCategories,
'isCategory' => $isCategory
'isSubCategory' => $isSubCategory
];
$this->view('index', $data);
}
Then I have my templatecode which is where im trying to do the foreach and the top category works fine but i just can't figure out how i do the subcategories. Before i started using the MVC model, i would simply just put the class inside the main category foreach and then put the id but now its all done in the controller so i cannot figure out how i do subcategories using the MVC model.
<?php foreach($data['mainCategory'] as $category) : ?>
<li class="dropdown">
<a class="dropdown-toggle nav-link dropdown-toggle pl-0" data-toggle="dropdown" aria-expanded="false" href="#"><?php echo $category['name']; ?></a>
<div class="dropdown-menu" role="menu">
<a class="dropdown-item" role="presentation" href="#"><i class="text-black-50 fas fa-box"></i> All <?php echo $category['name']; ?></a>
<div class="dropdown-divider" role="presentation"></div>
<?php foreach($data['subCategory'] as $scategory) : ?>
<a class="dropdown-item" role="presentation" href="#"><i class="text-black-50 <?php echo $scategory['icon']; ?>"></i> <?php echo $scategory['name']; ?></a>
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
Thanks very much!
$query = 'SELECT id, parent_id, name, icon FROM categories ORDER BY name';
$result = mysqli_query($conn, $query) OR trigger_error($query.'<br>'.mysqli_error($conn),E_USER_ERROR);
$refs = Array();
$categories = Array();
while($row = mysqli_fetch_assoc($result))
{
$thisref = &$refs[$row['id']];
$thisref['name'] = $row['name'];
$thisref['icon'] = $row['icon'];
if($row['parent_id']) $refs[$row['parent_id']]['children'][$row['id']] = &$thisref;
else $categories[$row['id']] = &$thisref;
}
echo '<ul class="nav navbar-nav mr-auto">';
foreach($categories as $category)
{
echo '<li class="dropdown">';
echo is_array($category['children'])
? '<a class="dropdown-toggle nav-link dropdown-toggle pl-0" data-toggle="dropdown" aria-expanded="false" href="#">'.$category['name'].'</a>'.sub($category['children'])
: $category['name'];
echo '</li>';
}
echo '</ul>';
function sub(&$subCat)
{
echo '<div class="dropdown-menu" role="menu">
<a class="dropdown-item hide-me" role="presentation" href="#"><i class="fas fa-fire mr-2"></i> Most Popular </a>
<div class="dropdown-divider hide-me" role="presentation"></div>';
foreach($subCat as $id => $subcategory)
{
echo '<a class="dropdown-item" role="presentation" href="'.FULL_ROOT.'/category/'.$id.'/"><i class="'.$subcategory['icon'].' mr-2"></i>'.$subcategory['name'].'</a>';
}
echo '</div>';
}
Hi i want to add active class to li tag when a subItems menu is active. My code below:
<?php foreach ($items as $key => $item) : ?>
<?php
$subItems = $block->getMenuItems($key);
$class = '';
if (count($subItems) > 0) {
$class .= 'dropdown ';
}
if ($block->isActive($item)) {
$class .= 'active ';
}
?>
<li class="c-sidebar__item <?php /* #escapeNotVerified */ echo $class; ?>">
<?php if (count($subItems) > 0) : ?>
<a class="c-sidebar__link" href="#">
<div class="c-sidebar__item-content">
<i class="c-sidebar__item-icon c-sidebar__item-icon--main glyphicon glyphicon-dashboard"></i>
<p class="c-sidebar__item-name"><?php echo __($item['label']); ?></p>
<i class="c-sidebar__item-icon c-sidebar__item-icon--carot glyphicon glyphicon-menu-down"></i>
</div>
</a>
Below is the category table
My Model
public function get_categories($parent_id = 0){
$query = $this->db->where('parent_id', $parent_id)
->order_by('cat_name', 'ASC')
->get('category');
$return = array();
foreach ($query->result() as $category){
$return[$category->cat_id] = $category;
}
if($query->num_rows() > 0){
foreach ($query->result() as $cat){
$return[$cat->cat_id] = $cat;
}
return $return;
}
return $return;
}
My view
<div class="navbar-collapse collapse" id="navigation">
<ul class="nav navbar-nav navbar-left">
<li class="active">
Home
</li>
<?php foreach ($categories as $row): ?>
<li><?php echo $row->cat_name; ?></li>
<ul class="dropdown-menu">
<li><?php echo $row->cat_name; ?></li>
</ul>
<?php endforeach; ?>
</ul>
</div>
I want to display the diskpad as a child because not displaying. My mistake is there where? Please help me
You have to structure your data (to be honest you should study this piece of code, because you should understand it)
modify your model with the following code:
public function get_categories()
{
$query = $this->db->order_by('cat_name', 'ASC')->get('category');
$arrData = $query->result();
$arrTreeById = [];
foreach($arrData AS $objItem)
{
$arrTreeById[$objItem->cat_id] = $objItem;
$objItem->arrChilds = [];
}
$arrChildIds = [];
foreach($arrTreeById AS $objItem)
{
if (isset($arrTreeById[$objItem->parent_id]))
{
$arrTreeById[$objItem->parent_id]->arrChilds[] = $objItem;
$arrChildIds[] = $objItem->cat_id;
}
}
array_walk($arrChildIds, function($val) use (&$arrTreeById) {
unset($arrTreeById[$val]);
});
return $arrTreeById;
}
and your view
<div class="navbar-collapse collapse" id="navigation">
<ul class="nav navbar-nav navbar-left">
<li class="active">
Home
</li>
<?php foreach ($categories as $row): ?>
<li><?php echo $row->cat_name; ?>
<?php
if (count($row->arrChilds) > 0) :
?>
<ul class="dropdown-menu">
<?php
foreach ($row->arrChilds as $row) :
?>
<li><?php echo $row->cat_name; ?></li>
<?php
endforeach;
?>
</ul>
<?php
endif;
?>
</li>
<?php endforeach; ?>
</ul>
</div>
I want to show subcategory inside the main category. My table looks like this. but when i print_r($cat_id) it returns only the last id of the category. How can I fix this? Here is my controller:
$data['categories'] = $this->courses_model->get_all_categories_for_courses();
foreach($data['categories'] as $ct){
$cat_id = $ct['id'];
}
$data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_id);
$this->load->view('templates/header');
$this->load->view('courses/courses-grid-fullwidth', $data);
$this->load->view('templates/footer');
}
and here is my model:
public function get_all_categories_for_courses() {
$query = $this->db->get_where('categories', array('parent_id' => NULL));
return $query->result_array();
}
function get_all_subcategories_for_courses($cat_id) {
$query = $this->db->get_where('categories', array('parent_id' => $cat_id));
return $query->result_array();
}
and here is my view page:
<?php foreach($categories as $cat): ?>
<div class="col-md-3 col-sm-6 incat">
<h4>
<a href="#" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
<?= $cat['name_rus']; ?></a>
</h4>
<ul>
<?php foreach($subcategories as $subcat): ?>
<li><?= $subcat['name_rus']; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
Thanks in advance!
Or you can do this
<?php foreach($categories as $cat): ?>
<div class="col-md-3 col-sm-6 incat">
<h4>
<a href="<?= site_url('/courses/category/'.$cat['category_slug']); ?>" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
<?= $cat['name_rus']; ?></a>
</h4>
<ul>
<?php
$query = $this->db->get_where('categories', array('parent_id' => $cat['id']));
?>
<?php foreach($query->result() as $subcat): ?>
<li><?= $subcat->name_rus; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
As Ukasyah said in a comment on your question, you're overwriting the $cat_id value each time you loop through your categories.
What you will need to do is get an array populated with the IDs and use $this->db->where_in() to match an array rather than a single value.
Your controller will need to look more like this:
$data['categories'] = $this->courses_model->get_all_categories_for_courses();
$cat_ids = [];
foreach($data['categories'] as $ct){
$cat_ids[] = $ct['id'];
}
$data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_ids);
$this->load->view('templates/header');
$this->load->view('courses/courses-grid-fullwidth', $data);
$this->load->view('templates/footer');
While you will need to change your model function to look like this:
function get_all_subcategories_for_courses($cat_ids) {
$query = $this->db->where_in('parent_id', $cat_ids)->get('categories');
return $query->result_array();
}
Best of luck.
I'm trying to display all the categories below the main title, so I want to display four categories in each column. The below is my code
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<ul class="pull-left">
<?php foreach ($collection as $cat):?>
<li>
<a style="color:#fff;" href="<?php echo $helper->getCategoryUrl($cat);?>">
<cite><?php echo $cat->getName();?><?php
?></cite>
</a>
</li>
<?php endforeach;?>
</ul>
HTML Output
<ul class="pull-left">
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
.
.
.
.
</ul>
I want to define four li items in every ul. Like this
<ul class="pull-left">
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
<ul class="pull-left">
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
I also want to display product count and I've tried this
<?php
$products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCount();
?>
but it is not working.
I am using this code on my category page and it is giving me count.
Please have a look at the code I have added before tag.
<?php
$loadCategory = Mage::registry('current_category');
//echo 'loadCategory = '.$_categoryID = $loadCategory->getId();
$subCategories = explode(',', $loadCategory->getChildren());
?>
<ul class="pull-left">
<?php $i=1;
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($cat->getImageUrl())
{
//echo ''.$cat->getName().'</br>';
echo '<li>';
echo '<a style="color:#fff;" href="'.$cat->getUrl().'">'.$this->escapeHtml($cat->getName()); echo"<br>";
echo $total = Mage::getModel('catalog/layer')->setCurrentCategory($subCategoryId)->getProductCollection()->getSize();
echo'</a>';
echo '</li>';
if($i%4==0)
{
echo'</ul><ul class="pull-left">';
}
}
}
$i++;
}
?>
Please use this code. This will surely work for you.
<?php
$category = Mage::getModel('catalog/category')->load($categoryID);
echo $total = Mage::getModel('catalog/layer')->setCurrentCategory($category)->getProductCollection()->getSize();
?>
Thanks,
Please use this one.
You can adjust ul ,li as per wish. Right now this will list four subcategories in a single row.
This code will give you the subcategories of a parent category.
<?php
$loadCategory = Mage::registry('current_category');
//echo 'loadCategory = '.$_categoryID = $loadCategory->getId();
$subCategories = explode(',', $loadCategory->getChildren());
?>
<ul class="pull-left">
<?php $i=1;
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($cat->getImageUrl())
{
//echo ''.$cat->getName().'</br>';
echo '<li>';
echo '<a style="color:#fff;" href="'.$cat->getUrl().'">'.$this->escapeHtml($cat->getName()).'</a>';
echo '</li>';
if($i%4==0)
{
echo'</ul><ul class="pull-left">';
}
}
}
$i++;
}
?>
</ul>