How can I set active class for navigation in codeigniter? - codeigniter

I have a navigation which consists of different pages, I want to set the current pages active. My code is as follow
<div class="container tablistitem">
<ul>
<li class="active">Timeline</li>
<li>About</li>
<li>Friends</li>
<li>Photos</li>
</ul>
</div>
I am using following code to get the result but not working:
<div class="container tablistitem">
<ul>
<li class="<?php if($currentpage="") { echo "active"; } ?>">Timeline</li>
<li class="<?php if($currentpage="about") { echo "active"; } ?>">About</li>
<li class="<?php if($currentpage="friends") { echo "active"; } ?>">Friends</li>
<li class="<?php if($currentpage="photos") { echo "active"; } ?>">Photos</li>
</ul>
</div>

You can try this solution for your problem
<?php
$controller = $this->router->fetch_class(); // You can Match controller name
$method = $this->router->fetch_method(); // You can Match mathod name
?>
<div class="container tablistitem">
<ul>
<li class="<?php
if ($controller = "dashboard" && $method = "index") {
echo "active";
}
?>">Timeline</li>
<li class="<?php
if ($controller = "pages" && $method = "about") {
echo "active";
}
?>">About</li>
<li class="<?php
if ($controller = "pages" && $method = "friends") {
echo "active";
}
?>">Friends</li>
<li class="<?php
if ($controller = "pages" && $method = "photos") {
echo "active";
}
?>">Photos</li>
</ul>
</div>

Related

How to display 3 levels of hierarchical menu in WordPress using "wp_get_nav_menu_items"?

I am trying to display 3 levels of hierarchical menu in WordPress
using wp_get_nav_menu_items function, but somehow the structure is not
displaying in that manner.
This is my code which I have included in header file, but the structure is not proper.
$menu_name = 'primary-menu';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC'));
$ParentArray = array();
foreach ($menuitems as $item) {
// if (!empty($item->menu_item_parent) && !in_array($item->menu_item_parent, $ParentArray)) {
array_push($ParentArray, $item->ID);
// }
}
?>
<nav>
<ul class="main-nav">
<?php
$count = 0;
$submenu = false;
foreach ($menuitems as $item):
$link = $item->url;
$title = $item->title;
// item does not have a parent so menu_item_parent equals 0 (false)
if (!$item->menu_item_parent):
// save this id for later comparison with sub-menu items
$parent_id = $item->ID;
?>
<li class="item">
<a href="
<?php echo $link; ?>" class="title">
<?php echo $title; ?>
</a>
<?php endif; ?>
<?php if (in_array($item->menu_item_parent, $ParentArray)): ?>
<?php if (!$submenu): $submenu = true; ?>
<ul class="sub-menu">
<?php endif; ?>
<li class="item">
<a href="
<?php echo $link; ?>" class="title">
<?php echo $title; ?>
</a>
</li>
<?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id && $submenu): ?>
</ul>
<?php
$submenu = false;
endif;
?>
<?php endif; ?>
<?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id): ?>
</li>
<?php
$submenu = false;
?>
<?php
$count++;
endforeach;
?>
</ul>
</nav>
After making changes in above code,this things work for me.
$menu_name = 'primary-menu';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC'));
$ParentArray = array();
foreach ($menuitems as $item) {
// if (!empty($item->menu_item_parent) && !in_array($item->menu_item_parent, $ParentArray)) {
array_push($ParentArray, $item->ID);
// }
}?>
<nav>
<ul class="main-nav">
<?php
$count = 0;
$submenu = false;
foreach ($menuitems as $item):
$link = $item->url;
$title = $item->title;
// item does not have a parent so menu_item_parent equals 0 (false)
if (!$item->menu_item_parent):
// save this id for later comparison with sub-menu items
$parent_id = $item->ID; ?>
<li class="item">
<a href="
<?php echo $link; ?>" class="title">
<?php echo $title; ?>
</a>
<?php endif; ?>
<?php if (in_array($item->menu_item_parent, $ParentArray)): ?>
<?php if (!$submenu): $submenu = true; ?>
<ul class="sub-menu">
<?php endif; ?>
<li class="item">
<a href="
<?php echo $link; ?>" class="title">
<?php echo $title; ?>
</a>
</li>
<?php if (!isset($item[$count + 1]) || $item[$count + 1]->menu_item_parent != $parent_id && $submenu): ?>
</ul>
<?php
$submenu = false;
endif;
?>
<?php endif; ?>
<?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id): ?>
</li>
<?php
$submenu = false;
?>
<?php
$count++;
endforeach;
?>
</ul>
</nav>

Codeigniter : How to detect the first row of a query result

My viewis like below.
<div class="carousel-inner">
<?php
foreach($news_data as $nws){
?>
<div class="item **If this is the first row, then echo active**">
<div class="col-md-12 news-item" style="padding-left: 0;">
<p><?php echo $nws->news_desc; ?></p>
</div>
</div>
<?php } ?>
</div>
Model:
function get_news_update()
{
$this->db->select('news_desc');
$this->db->from('news');
$this->db->where('stat', '1');
$this->db->order_by('id', 'desc');
$query = $this->db->get();
return $query->result();
}
Please see the <div class="item. If it is the first row of query result, then it will be item active.
How to do that ?
Modify your view like this:
<div class="carousel-inner">
<?php
foreach($news_data as $key => $nws){
?>
<div class="item <?php echo $key == 0 ? "active":""; ?>">
<div class="col-md-12 news-item" style="padding-left: 0;">
<p><?php echo $nws->news_desc; ?></p>
</div>
</div>
<?php } ?>
</div>
Here is your updated code
<div class="carousel-inner">
<?php
$i = 0;
foreach($news_data as $nws){
// it should be $i. not $i%2 right ?
if($i == 0)
{
$class="item active";
}
else
{
$class = "item";
}
?>
<div class="<?php echo $class; ?>">
<div class="col-md-12 news-item" style="padding-left: 0;">
<p><?php echo $nws->news_desc; ?></p>
</div>
</div>
<?php $i++; } ?>
</div>
As per my knowledge,You are looking for below code.Please let me know if its not working.
function get_news_update()
{
$this->db->select('news_desc');
$this->db->from('news');
$this->db->where('stat', '1');
$this->db->order_by('id', 'desc');
$query = $this->db->get();
return $query->first_row();
}
You can use row() function, which return always the first result.
Model.php
function get_news_update(){
$this->db->select('news_desc');
$this->db->from('news');
$this->db->where('stat', '1');
$this->db->order_by('id', 'desc');
$query = $this->db->get();
//check if exist
if (isset($query->row())){
return $query->row();
}
}
For more informations you will find here
View.php
<div class="carousel-inner">
<div class="item **If this is the first row, then echo active**">
<div class="col-md-12 news-item" style="padding-left: 0;">
<p><?php echo $nws->news_desc; ?></p>
</div>
</div>
</div>

codeigniter invalid argument supplied for foreach() on join clause

i try all of away that i know and suggestion on internet.
but all time return false.
please help me...
thanks
my error message is:
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/index.php
Line Number: 10
model.php
public function getBanner()
{
$this->db->select('album.id, album.cat_id , album.poster, album.slug, album.modify, category.id, category.name, category.slug');
$this->db->from('album,category');
$this->db->where('album.cat_id', 'category.id');
$query = $this->db->get();
if($query->num_rows() != 0)
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
controllers.php
$data['banner'] = $this->Fornt_model->getBanner();
showbanner.php
<section class="banner">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php $item = 1; foreach($banner as $latest_poster): $item_class = ($item == 1) ? 'item active' : 'item'; ?>
<div class="<?php echo $item_class; ?>">
<img src="<?php echo base_url('images/poster/'.$latest_poster['poster']); ?>" class="img-responsive img-thumbnail">
<div class="carousel-caption caption">
<a href="<?php echo site_url('category/bollywood/'.$latest_poster['slug']); ?>" class="box">
<b class="h3"><?php echo $latest_poster['name']; ?></b>
<small class="p">Exclusive AT*<?php echo substr($latest_poster['modify'], 0, 10); ?></small>
</a>
</div>
</div>
<?php $item++; endforeach; ?>
</div>
</div>
</div>
</div>
<div class="clearfix" style="padding: 10px 0;"></div>
<div class="row hidden-xs">
<?php $itemto = 0; foreach($banner as $latest_poster): $item_class_active = ($itemto == 0) ? 'active' : ''; ?>
<div class="col-sm-2 pointer" data-target="#myCarousel" data-slide-to="<?php echo $itemto; ?>"><img src="<?php echo base_url('images/poster/'.$latest_poster['poster']); ?>" class="img-responsive img-thumbnail" /></div>
<?php $itemto++; endforeach; ?>
</div>
</div>
I think something wrong with your query.. try
if($query->num_rows() != 0)
{
$result = $query->result_array();
print_r($result);
}
make sure your array data is available..
If there is no rows, front_model->getBanner() will return the result as false. Due to this you are getting an error. Use following code
public function getBanner()
{
$this->db->select('album.id, album.cat_id , album.poster, album.slug, album.modify, category.id, category.name, category.slug');
$this->db->from('album,category');
$this->db->where('album.cat_id', 'category.id');
$query = $this->db->get();
return $query->result_array();
}

CodeIgniter - Undefined variable & Trying to get property of non-object

I'm trying to get data from API in my Controller and pass the data to the View. But it don't seem working and causing error:
Message:Undefined variable: data and
Message:Trying to get property of non-object
Both errors happen in my View.php, on the line when I call variable $data.
This is part of my controller related to data I want to get
$jdwl['jadwal'] = $this->bolalob->api('schedules/bydate|'.$start.'/'.$end.'/10');
$data['jadwal'] = $this->load->view('jadwal', $jdwl, TRUE);
$template['content'] = $this->load->view('jadwal',$data, TRUE);
$this->load->view('template', $template);
And this is my view
<div class="float-left first-column">
<div class="box box-shadow red round-top round-bottom">
<div class="title-box"><strong>JADWAL</strong> TV</div>
<ul class="text-center no-padding">
<?php if ($data->data == 'no data') { ?>
<li class="jadwal-tv"><strong>Belum ada jadwal</strong>
</li>
<?php } else { ?>
<?php foreach ($data->data as $key=>$row) { ?>
<?php if ($key == 0) { ?>
<li class="jadwal-tv"><strong><?php echo $row->team_1->name; ?></strong><br />VS<br /><strong><?php echo $row->team_2->name; ?></strong><br /><span><?php echo $row->television; ?> <?php echo date('d/m/y H:i', $row->pubdate); ?> WIB</span></li>
<?php } else { ?>
<li class="side-padding">
<strong><?php echo $row->team_1->name; ?></strong> vs <strong><?php echo $row->team_2->name; ?></strong>
<br />
<?php echo $row->television; ?> <?php echo date('d/m/y H:i', $row->pubdate); ?> WIB
</li>
<?php } ?>
<?php if ($key == 5) break; ?>
<?php } ?>
<?php } ?>
</ul>
</div>
</div>
The error happens to $data on line the 5 and 9. I've googled it and tried to use $data['jadwal'] instead of $data in my view but I didn't seem working too.
Anyone can help me with this?
Thanks :D
In your view no need to acess the $data array.
you can directly call your data key as a variable.
Ex:
In controller
$data['myVar']="Some string";
$this->load->view('viewName',$data);
In view
echo $myVar;
Try with the following view page..
<div class="float-left first-column">
<div class="box box-shadow red round-top round-bottom">
<div class="title-box"><strong>JADWAL</strong> TV</div>
<ul class="text-center no-padding">
<?php if (empty($jadwal)) { ?>
<li class="jadwal-tv"><strong>Belum ada jadwal</strong>
</li>
<?php } else { ?>
<?php foreach ($jadwal as $key => $row) { ?>
<?php if ($key == 0) { ?>
<li class="jadwal-tv"><strong><?php echo $row->team_1->name; ?></strong><br />VS<br /><strong><?php echo $row->team_2->name; ?></strong><br /><span><?php echo $row->television; ?> <?php echo date('d/m/y H:i', $row->pubdate); ?> WIB</span></li>
<?php } else { ?>
<li class="side-padding">
<strong><?php echo $row->team_1->name; ?></strong> vs <strong><?php echo $row->team_2->name; ?></strong>
<br />
<?php echo $row->television; ?> <?php echo date('d/m/y H:i', $row->pubdate); ?> WIB
</li>
<?php } ?>
<?php if ($key == 5) break; ?>
<?php } ?>
<?php } ?>
</ul>
</div>

Magento hide navigation menu item from guest

I'm running a magento custom template on magento community edition 1.7. How do I hide $custom_tab3 from guests? I want that tab to only show up for logged in users. Please see code below. Any help is super highly appreciated!
<ul id="nav">
<?php if (Mage::getStoreConfig('celebritysettings/celebritysettings_header/navigation_home')): ?>
<li class="level0 level-top">
<span><?php echo $this->__('How It Works'); ?></span>
</li>
<?php endif; ?>
<?php
echo $_menu;
$custom_tab = Mage::getModel('cms/block')->load('celebrity_navigation_block');
if($custom_tab->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'gift" class="level-top">
<span>'.$custom_tab->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block')->toHtml().'</div>
</li>';
}
$custom_tab2 = Mage::getModel('cms/block')->load('celebrity_navigation_block2');
if($custom_tab2->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block" >
<a href="'.$this->getBaseUrl().'plans" class="level-top">
<span>'.$custom_tab2->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block2')->toHtml().'</div>
</li>';
}
$custom_tab3 = Mage::getModel('cms/block')->load('celebrity_navigation_block3');
if($custom_tab3->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'showroom" class="level-top">
<span>'.$custom_tab3->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block3')->toHtml().'</div>
</li>';
}
$custom_tab4 = Mage::getModel('cms/block')->load('celebrity_navigation_block4');
if($custom_tab4->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'magazine" class="level-top">
<span>'.$custom_tab4->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block4')->toHtml().'</div>
</li>';
}
?>
</ul>
You can use Mage::getSingleton('customer/session')->isLoggedIn() to check whether the current user is logged in. In your code you can use it like this:
$custom_tab3 = Mage::getModel('cms/block')->load('celebrity_navigation_block3');
if($custom_tab3->getIsActive() && Mage::getSingleton('customer/session')->isLoggedIn()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'showroom" class="level-top">
<span>'.$custom_tab3->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block3')->toHtml().'</div>
</li>';
}
or even
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$custom_tab3 = Mage::getModel('cms/block')->load('celebrity_navigation_block3');
if($custom_tab3->getIsActive()) {
echo '
<li class="level0 level-top parent custom-block">
<a href="'.$this->getBaseUrl().'showroom" class="level-top">
<span>'.$custom_tab3->getTitle().'</span>
</a>
<div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block3')->toHtml().'</div>
</li>';
}
}
to prevent unnecessary retrieval of the cms/block model.

Resources