How to create menuview with submenu in CakePHP - php

i want to create in CakePHP a Elemnt that renders my Menu (Bootstrap).
My Database
My Element:
<?php
if (! isset ( $menus ) || empty ( $menus )) :
$menus = $this->requestAction('/menus/index');
endif;
foreach ( $menus as $menu ){
if($menu['Menu']['parent'] == 0){
?>
<li>
<?php
$inside = "<i class='".$menu['Menu']['icon']."'></i><span>".$menu['Menu']['name']."</span></a>";
echo $this->Html->link( $inside, array('controller' => $menu['Menu']['controller'],'action' => $menu['Menu']['action']), array( 'escape'=>false)); ?>
</li>
<?php } }
?>
How I render it:
<?php echo $this->element('Menus/main'); ?>
Example how a Dropdown should be:
<li class="dropdown">
<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
<i class="icon-long-arrow-down"></i>
<span>Drops</span>
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li>Icons</li>
<li>FAQ</li>
<li>Pricing Plans</li>
<li>Login</li>
<li>Signup</li>
<li>404</li>
</ul>
</li>
I tried to render it in the View, but the Problem is I cant call Functions of the Controller in example to ask the has the Entrie Childs.
Some Ideas how to Render a Menu right with CakePHP?

Related

How I can make drop down menu from my categories mysql table only show if parent_id is not 0

So hey there as the title said I am looking for away to make my categories with subcategories. I been looking in stackoverflow for what I need but none has help me of the examples..
Here is how my table look like
So I know what I want and what I need but I have no idea how I can do that possible
I have to SELECT * FROM categories ORDER by position ASC
I have to check if parent_id is bigger then 0.
I have to remove the parent_id from my navbar and show them only under the category name where it should be by dropdown menu .
But I have no idea how I could do all of that ..
Here is how I am selecting only my categories and display them
$catsq = mysqli_query($con,"SELECT * FROM categories ORDER by position ASC");
while($catinfo=mysqli_fetch_assoc($catsq)) {
echo '
<li class="nav-item'.(isset($_GET["cat"]) && $_GET["cat"]==$catinfo["id"] ? " active" : "").'">
<a class="nav-link" href="./index.php?cat='.$catinfo["id"].'">'.$catinfo["name"].'</a>
</li>
';
}
and it's look like this
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="cat=1">TestCat</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cat=2">TestCat2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="cat=3">TestSub</a>
</li>
</ul>
but I want It to look like this
<ul class="nav navbar-nav">
<li class="">TestCat</li>
<li class="dropdown ">
//TestCat2 have to doing nothing always.
TestCat2</i>
<ul class="dropdown-menu">
<li><a class="nav-link" href="cat=3">TestSub</a></li>
</ul>
</li>
</ul>
when the parent_id is more then 0..
If anyone can help me with this would be great..
Thanks to everybody.
There are several approaches you can take:
Build an array
Nested queries
Recursion
Array
This approach builds a data structure that you can iterate through in your view. Working example
<?php
// get db connection...
// create categories array
$stmt = mysqli_query($con, "SELECT * FROM categories ORDER BY position ASC");
while( $row = mysqli_fetch_assoc($stmt)) {
// $category[ $row['parent_id] ][ $row['id'] ] = $row; // use if you need to access other fields in addition to name
$category[ $row['parent_id] ][ $row['id'] ] = $row['name'];
}
// other php stuff...
?>
<html>
... snip ...
<ul class="nav navbar-nav">
<?php foreach($category[0] as $id => $name): ?>
<?php if( isset( $category[$id]) ): ?>
<li class="dropdown ">
<?= $name ?>
<ul class="dropdown-menu">
<?php foreach($category[$id] as $sub_id => $sub_name): ?>
<li><a class="nav-link" href="?cat=<?= $sub_id ?>" ><?= $sub_name ?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php else: ?>
<li class="">
<?= $name ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Nested Queries
This method is easiest to display using an imaginary class that does all the sql stuff behind the scenes. For the sake of argument, we will assume a class Category that has a method named listByParent($parent_id) which returns a list of rows having the designated parent_id.
<?php
$cat = new Category();
$topLevel = $cat->listByParent(0);
?>
<html>
... snip ...
<ul class="nav navbar-nav">
<?php foreach( $topLevel as $topRow ): ?>
<!-- note, this method is run on every iteration of top level categories -->
<?php $subRows = $cat->listByParent($topRow['id']) ?>
<?php if( count($subRows)): ?>
<li class="dropdown ">
<?= $topRow['name'] ?>
<ul class="dropdown-menu">
<?php foreach($subRows as $row): ?>
<li><a class="nav-link" href="?cat=<?= $row['id'] ?>" ><?= $row['name'] ?></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php else: ?>
<li class="">
<?= $topRow['name'] ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Recursion
Using recursion would allow you to have “unlimited” levels of subcategories. However, it’s a level of complexity that does not seem warranted in this case. But should you want to pursue it, note that the best way to approach it would be to make a template for the html that could be accessed programatically, with $cat->findByParent() being a key player...

Categories are repeating for each subcategory

I am showing categories in Menu. Some categories have subcategories.
function for getting parent categories
function get_parent_category(){
$query="select * from blog_categories where parent_id=0
ORDER BY
CASE id
WHEN '2' THEN 1
WHEN '1' THEN 2
WHEN '3' THEN 3
ELSE id
END";
$rows=array();
$result=$this->query($query);
while($row=$this->fetch_array($result)){
$row['url']=$this->get_cat_url($row);
$rows[]=$row;
}
return $rows;
}
Function for subcategories
function get_child_category(){
$query="select * from blog_categories where parent_id!=0";
$rows=array();
$result=$this->query($query);
while($row=$this->fetch_array($result)){
$row['url']=$this->get_cat_url($row);
$rows[]=$row;
}
return $rows;
}
Showing on the page like this:
<ul class="nav navbar-nav">
<li>Home</li>
<?php
foreach($this->parent_category as $cat){
foreach($this->child_category as $child_cat){
if($cat['id']==$child_cat['parent_id']){
?>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $cat['name'];?>
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><?php echo $child_cat['name']?></li>
</ul>
</li>
<?php
}elseif($cat['parent_id']==0){
?>
<li><span><?php echo $cat['name'];?></span></li>
<?php
}
?>
<?php }}?>
Output and Problem
The Main category circle in red color is seerah which has two subcategories. showing two times for first one in drop down one subcategory and for second time second subcategory is showing.
DB structure
What i wants:
I wants to show each subcategories under each parent category without repetition , how can i achieve this?
Here is how i handled the problem
<?php
foreach($this->parent_category as $cat){
$html = '';
foreach($this->child_category as $child_cat){
if($cat['id']==$child_cat['parent_id']){
$html .= '<li>' . $child_cat['name'] . '</li>';
// here is all child categories are saved in var.
}
}
if ($html == '') {
?>
<li><span><?php echo $cat['name'];?></span></li>
<?php
} else {
?>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $cat['name'];?>
<span class="caret"></span></a>
<ul class="dropdown-menu">
<?php echo $html; ?> // here is displayed under parent category
</ul>
</li>
<?php
}
}
?>
Output
To me it seem you did not split your html and loops properly here:
foreach($this->parent_category as $cat){
foreach($this->child_category as $child_cat){
if($cat['id']==$child_cat['parent_id']){
?>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $cat['name'];?>
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><?php echo $child_cat['name']?></li>
</ul>
</li>
Usually whenever you have a loop you should have some output before any nested loop started. In your case first level loop is about Categories which should become an <li> of parent main menu <ul>.
I think. You need to transform this fragment to:
foreach($this->parent_category as $cat){ ?>
<li ...>
...
<ul ...> <?php
foreach($this->child_category as $child_cat){ ?>
<li>...</li> <?php
} ?>
</ul>
</li> <?php
}
when you do not have repetitive value in $rows, why you use this part again for $cat['name']?
elseif($cat['parent_id']==0){
?>
<li><span><?php echo $cat['name'];?></span></li>
<?php
}
when in first part of foreach you create
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $cat['name'];?>

Conditional Login Link - Wordpress - PHP

Thanks in advanced for any help provided and taking the time to review my question. I basically just have a Wordpress site that once you login, you see your name and avatar in the top right corner and when you hover over your name an "Edit Profile" and "Log out" link appear. All I want is for a "Login" link to be in that same spot when you're not logged in. (This saves me from having to put a login widget in the sidebar).
I was able to track down the PHP file that holds that specific piece of code and find the spot where the user menu is setup. I'm just not sure what piece of code I could add so that there's a login link there that leads to mysite.com/login/. Here is the current code:
<?php }
//user menu
$user_show_info = ot_get_option('user_show_info');
if ( is_user_logged_in() && $user_show_info =='1') {
$current_user = wp_get_current_user();
$link = get_edit_user_link( $current_user->ID );
?>
<li class="menu-item current_us">
<?php
echo '<a class="account_cr" href="#">'.$current_user->user_login;
echo get_avatar( $current_user->ID, '25' ).'</a>';
?>
<ul class="sub-menu">
<li class="menu-item"><?php _e('Edit Profile','cactusthemes') ?></li>
<li class="menu-item"><?php _e('Logout','cactusthemes') ?></li>
Here's an image to show you what I mean as well. Top is logged out, bottom logged in.
http://6chill.com/loli.jpg
Thanks again for any help that you're able to provide and if you require further details just let me know and I'll do my best to provide them.
Thanks for the prompt reply everyone :) As requested, here's more of the code to ensure nothing was left out.
<?php }
//user menu
$user_show_info = ot_get_option('user_show_info');
if ( is_user_logged_in() && $user_show_info =='1') {
$current_user = wp_get_current_user();
$link = get_edit_user_link( $current_user->ID );
?>
<li class="menu-item current_us">
<?php
echo '<a class="account_cr" href="#">'.$current_user->user_login;
echo get_avatar( $current_user->ID, '25' ).'</a>';
?>
<ul class="sub-menu">
<li class="menu-item"><?php _e('Edit Profile','cactusthemes') ?></li>
<li class="menu-item"><?php _e('Logout','cactusthemes') ?></li>
</ul>
</li>
<?php }?>
<?php //submit menu
if(ot_get_option('user_submit',1)) {
$text_bt_submit = ot_get_option('text_bt_submit');
if($text_bt_submit==''){ $text_bt_submit = 'Submit Video';}
if(ot_get_option('only_user_submit',1)){
if(is_user_logged_in()){?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php }
} else{
?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php
}
} ?>
I hope that's what you're looking for. Where you see the comment "//submit menu", that's where the "Submit Video" button comes into play. Thanks so much again guys, you're freaking awesome!
Please try this code, I think this will help you
<?php
if ( is_user_logged_in() )
{ ?>
LOGOUT
<!-- add another action as per your requirement-->
<?php } else
{ ?>
<a class="login" href="<?php echo site_url(); ?>/login/">LOGIN</a>
<! -- give link to your login page -->
<?php }
put this code in header file.
You need to add an else to the if statement that checks if the user is logged in. If they aren't logged then you output a link to the login page. You may need to tweak the CSS or the classes on the menu item for the login link.
<?php }
$user_show_info = ot_get_option('user_show_info');
if ( is_user_logged_in() && $user_show_info =='1') {
$current_user = wp_get_current_user();
$link = get_edit_user_link( $current_user->ID );
?>
<li class="menu-item current_us">
<?php
echo '<a class="account_cr" href="#">'.$current_user->user_login;
echo get_avatar( $current_user->ID, '25' ).'</a>';
?>
<ul class="sub-menu">
<li class="menu-item"><?php _e('Edit Profile','cactusthemes') ?></li>
<li class="menu-item"><?php _e('Logout','cactusthemes') ?></li>
</ul>
</li>
<?php } else { ?>
<li class="menu-item current_us">
Login
</li>
<?php } ?>
<?php //submit menu
if(ot_get_option('user_submit',1)) {
$text_bt_submit = ot_get_option('text_bt_submit');
if($text_bt_submit==''){ $text_bt_submit = 'Submit Video';}
if(ot_get_option('only_user_submit',1)){
if(is_user_logged_in()){?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php }
} else{
?>
<li class="menu-item"><a class="" href="#" data-toggle="modal" data-target="#submitModal"><?php _e($text_bt_submit,'cactusthemes'); ?></a></li>
<?php
}
} ?>

Dynamically insert class="active" into php function that generates bootstrap menu

I have tried pretty much every solution that was available online but cannot get the class="active" to change dynamically for each menu section.
When someone clicks the top level menu item it opens up the second level menu and if someone click the on a second level menu item in that section class="active" needs to remain on the top level menu li tag.
I have the following function that generates my bootstrap menu:
function getMenu() {
global $connection;
mysqli_select_db($connection, "c9");
$query = ("SELECT testName, testId FROM testType");
$result_set = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($result_set)) {
$testId = $row['testId'];
$testName = $row['testName'];
echo '<li>'; //This is where class="active" needs to be added
echo '<span class="nav-label">'.$testName.'</span>';
echo '<ul class="nav nav-second-level collapse">';
echo '<li>Summary Report</li>';
echo '<li>Add Data</li>';
echo '</ul>';
echo '</li>';
}
}
And the menu structure is as follows:
<ul class="nav metismenu" id="side-menu">
<li class="active">
<a href="/">
<span class="nav-label">Summary</span>
<ul class="nav nav-second-level collapse in">
<li>Reports</li>
</ul>
</li>
<li><a href="#">
<span class="nav-label">Temperature</span>
<ul class="nav nav-second-level collapse">
<li>Summary Report</li>
<li>Add Data</li>
</ul>

How to add active class to codeigniter hyperlinks?

I know this question comes across a lot, but I just can't figure out how to do this using the, already answered posts.
I have a header with navigation links. I would like to add class="active" to the link that's active at the moment.
How could I do this if I have the following navigation?
<nav>
<ul id="main_nav">
<li class="home">
<a href="search">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
</li>
<li class="categorie">
<a href="categorieen/all">
<i class="icon-list-ul"></i>
<span>CATEGORIE</span>
</a>
</li>
<li class="aanbieding">
<a href="aanbiedingen">
<i class="icon-shopping-cart"></i>
<span>AANBIEDING</span>
</a>
</li>
<li class="vacature">
<a href="vacatures">
<i class="icon-copy"></i>
<span>VACATURE</span>
</a>
</li>
<li class="agenda">
<a href="agenda">
<i class="icon-calendar"></i>
<span>AGENDA</span>
</a>
</li>
<li class="contact">
<a href="contact">
<i class="icon-envelope"></i>
<span>CONTACT</span>
</a>
</li>
</ul>
</nav>
I tried this, but it did not work:
<script>
$(function() {
var href = $(this).find('a').attr('href');
alert(window.location.pathname)
if (href === window.location.pathname) {
$(this).addClass('active');
}
});
</script>
Maybe there's a better Codeigniter-ish way?
try this one.i think no need of javascript or jquery.
If you are using codeigniter then you can use URI Class.
<li class="home">
<a class="<?php if($this->uri->segment(1)=="search"){echo "active";}?>" href="<?=base_url('search')?>">
<i class="icon-search"></i>
<span>BEDRIJF ZOEKEN</span>
</a>
</li>
please let me know if you face any problem
I created a helper and saved it into the helper directory named as "menu_helper.php":
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
if(!function_exists('active_link')) {
function activate_menu($controller) {
// Getting the class instance.
$ci = get_instance();
// Getting the router class to actived it.
$class = $ci->router->fetch_class();
return ($class == $controller) ? 'active' : '';
}
}
Then in config/autoload.php, I added "menu" as a helper on line 91.
The last step is to put the code for print the "active" class when accessing the page (i.e. Login Page):
<li class="<?php echo activate_menu('login'); ?>">
<?php echo anchor('login', 'Login'); ?>
</li>
if (href === window.location.pathname) {
$('a[href='+ href +']').addClass('active');
}
Try this one:
<a class="<?=(current_url()==base_url('search')) ? 'active':''?>" href="<?=base_url('search')?>">

Categories