codeigniter dynamic menu creation - codeigniter

hi I am trying to create a dynamic menu with submenus. I am almost done but the loop is repeating as you can see in the output image, which should not happen. Error is like 1.Fruits main menu is repeating .[2].fruits sub category are splitting and displaying .
error output
menu.php
$where_array = array('role_id' => $currentRolename , 'active_status' => '1'); //step1
$table = 'bg_assignment';
$query = $this->db->where($where_array)->get($table);
foreach ($query->result() as $perm)
{
$bg = $perm->bg_id;
$temp = '1';
$where_array2 = array('role_id' => $currentRolename , 'permission' => '1'); //step2
$table2 = 'role_permission';
$query2 = $this->db->where($where_array2)->get($table2);
foreach ($query2->result() as $r2)
{
$bg_id = $r2->bg_id;
$where_array3 = array('bg_id' => $bg_id);
$table3 = 'bg_forms';
$query3 = $this->db->where($where_array3)->get($table3);
foreach ($query3->result() as $r3)
{
$where_array4 = array('phpfile_name' => $r3->phpfile_name);
$table4 = 'bg_forms';
$query4 = $this->db->where($where_array4)->get($table4);
foreach ($query4->result() as $r4)
{
?>
<li class="treeview <?php echo menu_li_active($r4->category_name); ?>">
<a href="#">
<i class="fa fa-sitemap"></i>
<span> <?php echo ($r4->category_name); ?></span>
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<?php echo menu_link($r4->controller.'/'.$r4->phpfile_name, $r4->displayform_name); ?>
</ul>
<?php } }?>
</li>
</li>
<?php
}
}
?>

I didn't do any test, just throw some ideas here, I think it will be much easier and clearer if you just use inner join in the query. Below code might have a syntax error, I don't have a similar environment as yours to do the tests.
<?php
//Let me just use open query to make this clear.
$sql_query = '
select rp.bg_id,
bf.category_name,
bf.controller,
bf.phpfile_name,
bf.displayform_name
from bg_assignment ba
inner join Role_permission rp
on ba.role_id = rp.role_id
and ba.bg_id = rp.bg_id --You might also want this line
inner join Bg_forms bf
on bg_id = bf.bg_id
where rp.permission = 1
and ba.active_status = 1
order by rp.bg_id, bf.displayform_name
';//I missed this semicolon
//Above sql query will give you two records
//Please test above query in your mysql to see if you can get below two records
/*
bg_id category_name controller phpfile_name displayform_name
5 Fruits Product index Apple
5 Frutis Product sales Orange
*/
$query = $this->db->query($sql_query);
$result = $query->result_array();
if(count($result) > 0){ // We get records!
$html = '';
for($i = 0; $i<count($result); $i++){
$s = $result[$i];
//Make the first li no matter what
if($i === 0){
$html.= '<li class="treeview">'.menu_li_active($s->category_name);
$html.= '<a href="#">';
$html.= '<i class="fa fa-sitemap"></i>';
$html.= '<span>'.$s->category_name.'</span>';
$html.= '<i class="fa fa-angle-left pull-right"></i>';
$html.= '</a>';
$html.= '</li>';
$html.= '<ul class="treeview-menu">';
$html.= menu_link($s->controller.'/'.$s->phpfile_name, $s->displayform_name);
if(count($result)===1){
$html.= '</ul>';
}
}
else{
if($s->category_name === $result[$i-1]->category_name){
//If current category is the same as previous one, we put them in one tab
$html.= menu_link($s->controller.'/'.$s->phpfile_name, $s->displayform_name);
}
else{
//If different, create a new tab
$html.='</ul>';
$html.= '<li class="treeview">'.menu_li_active($s->category_name);
$html.= '<a href="#">';
$html.= '<i class="fa fa-sitemap"></i>';
$html.= '<span>'.$s->category_name.'</span>';
$html.= '<i class="fa fa-angle-left pull-right"></i>';
$html.= '</a>';
$html.= '</li>';
$html.= '<ul class="treeview-menu">';
$html.= menu_link($s->controller.'/'.$s->phpfile_name, $s->displayform_name);
}
}
}
$html.='</li>';
echo $html;
}
?>

Related

How I can active $record

I have table like this
public function getDoctorPhotoRecord($count)
{
$this->db
->select('u.id, fullname, image, unvan_code, baslik, onayli')
->from('user as u')
->join('user_detail as ud', 'u.id = ud.id')
->join('tanim_sehir as us', 'ud.sehir_code = us.code')
->where('group', 'doktor')
->where('image != \'\' ')
->order_by('rand()')
->limit($count);
$query = $this->db->get();
return $query->result_array();
}
and I want this code work as
<?php
$onay = $record->detail->onayli;
?>
<?php
if ($onay == 0) {
?>
<i class="fa fa-user-md" aria-hidden="true"></i> Bu <b>Doktor</b> siz misiniz?
<?php
} else {
?>
<span class="verified" id="verifyuser" title="" data-toggle="tooltip" data-placement="bottom" data-original-title="Onaylı Üye"><i class="fa fa-check"></i></span>
<?php
}
?>
When I tried it getting data with this :
<?php echo $item['onayli'];?>
but I want active This code
<?php
$onay = $record->detail->onayli;
?>
How I can active this code ?
Thank you

Add "active" class to topmenu, magento 1.9

I need to add active class to my topmenu. And i already saw a bunch of subjects on forums, but i didn't saw code like i have. Can you please hepl me. I tried to "echo $currentUrl" but this what i saw " localhost.com/app/etc/local.xml "
<?php $_menu = $this->getHtml('level-top') ?>
<?php if($_menu): ?>
<?php
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$baseUrl = Mage::getBaseUrl();
$locale = Mage::app()->getLocale()->getLocaleCode();
$_topMenuItems['ru_RU'] = array(
/*$this->__('Home') => $baseUrl,*/
$this->__('IIW, Delivery and payment') => $baseUrl . 'dostavka-oplata.html',
$this->__('IIW, About us') => $baseUrl . 'pro-nas-kategorija.html',
$this->__('IIW, Partners') => $baseUrl . 'partneram.html',
$this->__('IIW, Contacts') => $baseUrl . 'contacts',
$this->__('IIW, It is interesting') => $baseUrl . 'korisna-informacija.html',
/* $this->__('IIW, Where to buy') => $baseUrl . 'de-prudbatu.html', */
);
?>
<nav id="nav">
<ol class="nav-primary">
<?php
$i = 0;
$length = count($_topMenuItems[$locale]);
foreach ($_topMenuItems[$locale] as $label => $_itemUrl) {
if ($i == 0) {
echo $_menu;
}
$customMenuClass = ' cm-' . ($i + 1);
if ($i == 0) {
$customMenuClass .= ' first';
} elseif ($i == ($length - 1)) {
$customMenuClass .= ' last';
}
if (trim($currentUrl, '/') == trim($_itemUrl, '/')) {
$customMenuClass .= ' active';
}
?>
<li class="level0<?php echo $customMenuClass ?>">
<?php echo $label ?>
</li>
<?php
$i++;
}
?>
</ol>
</nav>
<script>
jQuery(document).ready(function($){
var locationUrl = location.href;
$(".yourmenu a.yourclass").each(function() {
var menuLinkHref = $(this).attr("href");
if (~locationUrl.indexOf(menuLinkHref)) {
$(this).addClass("active");
}
});
});
</script>
JS solution if you need
.yourmenu - class of the menu, where your links must change class .actve;
.yourclass - class allready used for each link in the menu (ex.".menu_item_link").

Very slow product list issue with magento

I have a category called new arrivals which has 5 subcategories. I created a custom view called new-arrivals.phtml. I created custom Static Block with the following code in magento
{{block type="core/template" template="custom/new-arrivals.phtml"}}
then from Category setting I changed the display setting>CMS Block to the this cms block that I created. And the display mode is static block only
This view bring all the subcategories of New Arrivals(Bags, Tops, Shoes etc..) with it's products.. Like on this website As you can see on this website it is very fast but on my website with my code it is not. Here it is...
How can I make this load faster. Cache and everything is enabled. I assume that there is something wrong with my code. thank you for your help.
and this is new-arrivals.phtml content:
Magento CE 1.8.1
<?php
//I load all the subcategories here
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<?php foreach ($categories as $category): ?>
<div class="page-title category-title">
<h1>
<a href="<?php echo $category->getUrl() ?>">
<span><?php echo $category->getName() ?></span>
</a>
</h1>
<?php
// here I load all the products for each category
$_helper = $this->helper('catalog/output');
$category = Mage::getModel('catalog/category')->load($category->getId());
$_productCollection = $category->getProductCollection()->addCategoryFilter($category)
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('status', 1) // enabled
->addAttributeToFilter('visibility', 4) //visibility in catalog,search
->addAttributeToSort('date_added', 'ASC');
?>
<div class="product-count"><?php echo $count; ?> <?php echo $this->__('Products'); ?></div>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php // Grid Mode ?>
<?php
$_span = 'span3';
$_grid_pro = 4;
?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $_grid_pro; //$this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<?php // all the others stuff ....... ?>
Load this file using ajax or jquery.
Second one you run your code on footer and make one variable then using jquery or java script, you will show your all data in particular div, for example like this
if (count($_subcategories) > 0){
$top_menu .= '<ul>';
foreach($_subcategories as $_subcategory){
$prodCollection = Mage::getModel('catalog/category')- >load($_subcategory->getId())
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('type_id', array('eq' => 'grouped'))
->addAttributeToFilter('no_to_show', array('F' => 78));
if($prodCollection->count() !=0){
$top_menu .= '<li'; if($current_category == $_subcategory->getId()){$top_menu .= ' id="activesub"';}$top_menu .= '>'.$_subcategory->getName().'';
$_subcategories = Mage::getModel('catalog/category')->load($_subcategory->getId());
$_childsub = $_subcategories->getChildrenCategories();
$CatTotalCount = count($_childsub);
if($cntr == 0){
$firstCount = $CatTotalCount;
}
if($firstCount >= 7){
$loopCount = ($firstCount-1);
}else{
$loopCount = 6;
}
if($CatTotalCount >0){
$top_menu .= '<ul>';
$secCntr = 0;
foreach($_childsub as $_child_subcat){
$prodCollection2 = Mage::getModel('catalog/category')->load($_child_subcat->getId())->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('type_id', array('eq' => 'grouped'))->addAttributeToFilter('no_to_show', array('F' => 78));
if( $secCntr>$loopCount){
$secCntr = 0;
$top_menu .= '</ul><ul>';
}
$secCntr++;
if($prodCollection2->count() !=0):
$top_menu .= '<li'; if($current_category == $_child_subcat->getId()){$top_menu .= ' id="activesub"';}$top_menu .= '>'.$_child_subcat->getName().'</li>';
endif;
} }
}
$top_menu .= '</ul></li>';
$cntr++;
}
$top_menu .= '</ul>';
}
jQuery(document).ready(function() {
document.getElementById('top_menus').innerHTML = '';
jQuery('#mega-menu-8').dcMegaMenu({
rowItems: '3',
speed: 'fast',
effect: 'fade'
});
});

Codeigniter nested foreach in slideshow?

I ask this https://stackoverflow.com/a/14277726/1670630 on other post but my problem still exist.
In codeigniter 2.1 I'm trying to display channels by category. So if i have a category called Film, i should see a list of Channels within Film. I tried a nested foreach loop to accomplish this but can't seem to get it to work in the slidshow and limit by number of row.
My model:
<?php
class Pages_model extends CI_Model {
function get_channels_by_categ_tv()
{
$this->db->select('categories.category_name, channels.channel_name');
$this->db->from('type_categ');
$this->db->join('categories', 'categories.category_id = type_categ.category_id');
$this->db->join('channels', 'channels.channel_id = type_categ.channel_id');
$this->db->order_by('categories.category_id');
//$this->db->group_by(array('categories.category_id'));
$query = $this->db->get();
if($query->num_rows() == 0)
{
#no channels
return false;
}
return $query->result_array();
}
}
I have this in the view:
<ul class="slides">
<li>
<?php $cat_shown = ''; ?>
<div class="programe-tv_link">
<?php $cat_show = ''; $cnl_show = '';?>
<?php foreach ($category_chaneels as $category): ?>
<?php
if ($cat_show != $category['category_name']) {
$cat_show = $category['category_name'];
echo '<p>' . $cat_show . '</p>';
}
$cnl_show = $category['channel_name'];
echo '<dd> >>' . $cnl_show . '</dd> ';
?>
<?php endforeach; ?>
</div>
</li>
<li>
<div class="programe-tv_link">
<p>Arte</p>
<dd> >> Acasa</dd>
<dd> >> Antena 1</dd>
<dd> >> Pro TV</dd>
</div>
<div class="programe-tv_link">
<p>Music Box</p>
<dd> >> Acasa</dd>
<dd> >> Antena 1</dd>
<dd> >> Pro TV</dd>
<dd> >> TLC</dd>
</div>
</li>
</ul>
I atache image with ilustration,
sorry for my english and if you don't understund me please write here. THX in advance.
My finale code is this.
<div id="programe-tv-slide" class="flexslider">
<strong>Programe TV</strong>
<div class="redLine"></div>
<?php $cat_cnl = array();
$list = array();
$i=1;
foreach ($category_chaneels as $option) {
$catname = $option['category_name'];
$chlname = $option['channel_name'];
$cat_cnl[$catname][$i] = $chlname;
$list[$i] = $catname;
$i++;
};
?>
<?php
$rows = array_chunk($cat_cnl, 4, TRUE);
foreach ($rows as $row) { //var_dump($rows);
?>
<ul class="slides">
<?php
echo ('<li>');
foreach ($row as $category => $channels) {
echo '<div class="programe-tv_link">';
echo '<p>' . $category . '</p>';
foreach ($channels as $channel) {
echo '<dd>' . $channel . '</dd> ';
};
echo '</div>';
};
echo ('</li>');
?>
</ul>
<?php }; ?>
</div>

I need to get a list of sibling categories in magento 1.7

So far I think i am working at something like this
<?php
$parentId = $_category->getParentId();
$cats = Mage::getModel('catalog/category')->load($parentId)->getChildred();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
<li>
<?php
$category=Mage::getModel('catalog/category')->load($catId)
echo $category->getName();
?>
</li>
<?php endforeach; ?>
</ul>
I am trying to have lateral movement in subcategories without having to go up a layer.
Let me know if that made sense.
The below code will helps you to load 2nd level (sub)category. you can set the $_rootCatId as dynamic also by Mage::app()->getStore()->getRootCategoryId();.
$_rootCatId = 2;//Root Category
$_rootCategory = Mage::getModel('catalog/category')->load($_rootCatId);
$_catName = $_rootCategory->getName();
if($_rootCategory->hasChildren())
{
$_collection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->addAttributeToSort('position', 'asc')
->joinUrlRewrite()
->addIdFilter($_rootCategory->getChildren())
->load();
foreach($_collection AS $_sub)
{
$_subCat = Mage::getModel('catalog/category')->load($_sub->getId());
if($_subCat->hasChildren())
{
echo '<li class="leve10 nav-'. $_subCat->getId() .'">';
echo '<a id="subCatLink">';
echo '<span>'. $this->htmlEscape($_subCat->getName()) .'</span>
</a>
</li>';
echo '<ul id="subCatUl" class="no-display" style="padding:0px 13px;">';
$__collection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->addAttributeToSort('position', 'asc')
->joinUrlRewrite()
->addIdFilter($_subCat->getChildren())
->load();
foreach($__collection AS $__sub)
{
$__subCat = Mage::getModel('catalog/category')->load($__sub->getId());
echo '<li class="leve20 nav-'. $__subCat->getId() .'">
<a href="'. $this->getCategoryUrl($__subCat) .'">
<span>'. $this->htmlEscape($__subCat->getName()) .'</span>
</a>
</li>';
}
echo '</ul>';
}
else
{
echo '<li class="leve10 nav-'. $_subCat->getId() .'">
<a href="'. $this->getCategoryUrl($_subCat) .'">
<span>'. $this->htmlEscape($_subCat->getName()) .'</span>
</a>
</li>';
}
}
}
else
echo 'No Categories Found...';
Assuming that your $_category is a valid instance of Mage_Catalog_Model_Category:
<?php $_siblings = $_category->getParentCategory()->getChildrenCategories(); ?>
<ul>
<?php foreach($_siblings as $_sibling): ?>
<li>
<?php echo $_sibling->getName(); ?>
</li>
<?php endforeach; ?>
</ul>

Resources