How do I do a query to the case of multi-level menus in yii?
For example I have a table like this :
menu_name will follow parrent_id it has, so it will form a hierarchical menu
How do I use Yii on my layout so that the output as html above
My code :
<?php
Yii::import('zii.widgets.CMenu', true);
class ActiveMenu extends CMenu
{
public function init(){
$criteria = new CDbCriteria;
$criteria->condition='published=:idpub AND menu_controller=:menu';
$criteria->params=array(':idpub'=>1, ':menu'=>'#');
$items = Menu::model()->findAll($criteria);
echo "<ul id='yw1' class='nav'>";
foreach ($items as $item)
{
echo "<li class='dropdown'>
<a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
<i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>
</li>";
}
echo "</ul>";
parent::init();
}
}
But in the above code I only get parents data. How do I want to loop to get the childs data include in every parents menu like this html code :
<ul id="yw1" class="nav">
<li><span class="icon-white icon-tasks"></span> Home</li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<i class="icon-white icon-list"></i>Master<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Expedition</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Partner</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> User</li>
</ul>
</li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<i class="icon-white icon-list"></i>Transaction<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Process In</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Process Out</li>
</ul>
etc ....
etc ....
etc ....
</li>
</ul>
Thanks
You can create a recursive function for loop your childs like this
<?php
class ActiveMenu extends CMenu
{
public function init(){
$this->renderChilds();
}
protected function getChilds($parent_id)
{
$criteria = new CDbCriteria;
$criteria->condition='published=:idpub AND menu_controller=:menu AND parent_id = :parent';
$criteria->params=array(':idpub'=>1, ':menu'=>'#', ':parent'=>$parent_id);
$items = Menu::model()->findAll($criteria);
return $items;
}
public function renderChilds($parent_id=0, $class='nav')
{
$items = $this->getChilds($parent_id);
if (!empty($items))
{
echo "<ul class='$class'>";
foreach ($items as $item)
{
echo "<li class='dropdown'>
<a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
<i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>";
$this->renderChilds($item->id, 'dropdown-menu');
echo "</li>";
}
echo "</ul>";
}
}
...
}
Related
I want to return multiple notifications within a loop. I have 2 functions, the first one which pulls the notifications from the database and the second one for the dashboard header. The dashboard header is where the notifications will be displayed.
Now, the issue is, for some reason only one notification will be displayed. I've tried changing the return inside the loop to echo but that outputs the notifications at the beginning of dashboardTop().
What's going wrong and how can I resolve this?
public function loopNotifications() {
$stmt = $this->conn->prepare("SELECT * FROM notification WHERE isto=:user_id");
$stmt->execute(array(':user_id'=>$_SESSION['user_id']));
while ($notification = $stmt->fetch(PDO::FETCH_ASSOC)) {
$from = $this->pullName($notification['isto']);
return '<li>' . $notification['content'] . '</li>';
}
}
public function dashboardTop() {
echo '
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">notifications</i>
<span class="notification">' . $this->notificationCount() . '</span>
<p class="hidden-lg hidden-md">Notifications</p>
</a>
<ul class="dropdown-menu">
' . $this->loopNotifications() . '
</ul>
</li>
<li>
<a href="index.php?page=profile" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">person</i>
<p class="hidden-lg hidden-md">Profile</p>
</a>
</li>
</ul>
</div>
';
}
public function loopNotifications() {
$stmt = $this->conn->prepare("SELECT * FROM notification WHERE isto=:user_id");
$stmt->execute(array(':user_id'=>$_SESSION['user_id']));
$out=''; //empty var tof concaternation
while ($notification = $stmt->fetch(PDO::FETCH_ASSOC)) {
$from = $this->pullName($notification['isto']);
$out.= '<li>' . $notification['content'] . '</li>'; //concaternate each line in to string
}
return $out; //return whole string
}
public function dashboardTop() {
echo '
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">notifications</i>
<span class="notification">' . $this->notificationCount() . '</span>
<p class="hidden-lg hidden-md">Notifications</p>
</a>
<ul class="dropdown-menu">
' . $this->loopNotifications() . '
</ul>
</li>
<li>
<a href="index.php?page=profile" class="dropdown-toggle" data-toggle="dropdown">
<i class="material-icons">person</i>
<p class="hidden-lg hidden-md">Profile</p>
</a>
</li>
</ul>
</div>
';
}
I'm trying to view submenu after click on parent.
Classes->Primary School->(Grade1,Grade2,Grade3,Grade4,Grade5,Grade6)
Classes->Middle School->(Grade7,Grade8,Grade9)
Classes->Secondary School->(Grade10,Grade11,Grade12)
<div class="collapse navbar-collapse" id="main-navigation">
<ul class="nav navbar-nav navbar-right">
<?php foreach ($data as $menu) { ?>
<?php if(!$menu->children) { ?>
<li><?php echo $menu->name; ?></li>
<?php }
else {
?>
<li class="dropdown open">
<a href="#" class="dropdown-toggle " data-toggle="dropdown">
<?php echo $menu->name; ?>
<span class="fa fa-angle-down"></span>
</a>
<ul class="dropdown-menu open" role="menu">
<?php
foreach ($menu->children as $child) {
?>
<li><?php echo $child->name; ?></li>
<?php
foreach ($child->children as $sub_child) {
?>
<li class="dropdown-submenu">
<?php echo $sub_child->name; ?><span class="fa fa-angle-down"></span>
<ul class="dropdown-submenu" role="menu">
<?php } ?>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
</li>
</ul>
</div>
The result:
Problem with result
I want to view Grade 1 to Grade 6 after click on Primary school and from Grade 7 to Grade 9 after click on Middle school ...
Please help!
You have an open <ul class="dropdown-submenu" role="menu"> tag without any <li> or <a>'s being generated inside of it. Then you close two each statements before closing the <ul> tag.
<?php foreach ($child->children as $sub_child) { ?>
<li class="dropdown-submenu">
<a href="#" style="color:black;" class="dropdown-toggle " data-toggle="dropdown">
<?php echo $sub_child->name; ?><span class="fa fa-angle-down"></span>
</a>
<ul class="dropdown-submenu" role="menu">
<?php } ?>
<?php } ?>
</ul>
</li>
This will be causing all sorts of unintended markup output. Fix that and it should fix your problem. If not, you'll atleast be a lot closer.
I'm in the process on covering the navigation bar on all the pages to a global navigation with the PHP. I'm using PHP also to add a class and show the current page.
The challenge that I'm facing is the parent navigation selection. It's underlined when it's on the child page and the sub-nav is also selected. Such as I'm on the "History" page, child page to the "About Us" page. Both are underlined.
The challenge that I'm facing is when I move over to the next page, "Service Areas" or any of it's children, the "About Us" nav selection is still underline. I'm trying to use the || logic to prevent that from happening.
Below is the following code:
<li class="dropdown <?php if ($thisPage=='About Us'||'History'||'Mission Values'||'Process'||'Our People'||'Testimonials'||'Capstone Cares') echo 'active'; ?>">
Is there a way that I can deselect the parent nav when I'm under a different nav section?
Website is http://capstone.dgpehrson.com
Here is the rest of the code...
On individual pages I'm adding:
<?php $thisPage="About Us"; ?>
I'm changing the name according to the page.
Here is the Navigation code:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="dropdown <?php if ($thisPage=='About Us' || 'History' || 'Mission Values' || 'Process' || 'Our People' || 'Testimonials' || 'Capstone Cares') echo 'active'; ?>">
About Us<span class="caret"></span> <!-- Alink extentions that's been removed: data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" -->
<ul class="dropdown-menu">
<li <?php if ($thisPage=="History") echo "class=\"active\""; ?>>History</li>
<li <?php if ($thisPage=="Mission Values") echo "class=\"active\""; ?>>Mission & Values</li>
<li <?php if ($thisPage=="Process") echo "class=\"active\""; ?>>Process</li>
<li <?php if ($thisPage=="Our People") echo "class=\"active\""; ?>>Our People</li>
<li <?php if ($thisPage=="Testimonials") echo "class=\"active\""; ?>>Testimonials</li>
<li <?php if ($thisPage=="Capstone Cares") echo "class=\"active\""; ?>>Capstone Cares</li>
</ul>
</li>
<li class="dropdown <?php if ($thisPage=='Service Areas'||'Apartments'||'Capital'||'Development Services'||'Manufactured Housing'||'Complimentary') echo 'active'; ?>">
Service Areas<span class="caret"></span> <!-- Alink extentions that's been removed: data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" -->
<ul class="dropdown-menu">
<li <?php if ($thisPage=="Apartments") echo "class=\"active\""; ?>>Apartments</li>
<li <?php if ($thisPage=="Capital") echo "class=\"active\""; ?>>Capital</li>
<li <?php if ($thisPage=="Development Services") echo "class=\"active\""; ?>>Developement Services</li>
<li <?php if ($thisPage=="Manufactured Housing") echo "class=\"active\""; ?>>Manufactured Housing</li>
<li <?php if ($thisPage=="Complimentary") echo "class=\"active\""; ?>>Complimentary Value Analysis</li>
</ul>
</li>
<li class="dropdown">
Offerings<span class="caret"></span> <!-- Alink extentions that's been removed: data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" -->
<ul class="dropdown-menu">
<li>Apartment</li>
<li>Manufactured Housing</li>
<li>Multi-family Land</li>
</ul>
</li>
<li class="dropdown">
Market Reports<span class="caret"></span> <!-- Alink extentions that's been removed: data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" -->
<ul class="dropdown-menu">
<li>Florida</li>
<li>Kentucky</li>
<li>North Carolina</li>
<li>South Carolina</li>
<li>Tennessee</li>
<li>Virginia</li>
</ul>
</li>
<li>News</li>
<li>Careers</li>
<li>Contact Us</li>
</ul>
You have to have the compare between each 'or'. The string is just evaluating to a true.
if ($thisPage=='About Us'||$thisPage=='History'||$thisPage=='Mission Values'||$thisPage=='Process'||$thisPage=='Our People'||$thisPage=='Testimonials'||$thisPage=='Capstone Cares') echo 'active';
I am really trying to get this to work and am close:
It loops through the array and does create the submenus (with an error albeit) but:
the first menu with submenu items appears correctly but on the second menu that has a submenu items it repeats the first submenu items and then the second submenu items..... what am I missing?
Any help would be greatly appreciated.
<ul class="nav navbar-nav">
<?php
$html = new cacheHTML('topmenu');
if(!$html->isCached){
$menuitems = getMenuLevelsArray(25, 1 , 1);
$submenuI = "0";
foreach($menuitems as $item){
if($item['submenu']){
$subs[$submenuI] = $item['submenu'];
}
?>
<li <?php if($subs[$submenuI]){ ?> class="dropdown" <?php }?>>
</span> <?php }?>
<!-- submenu begins here -->
<?php foreach($subs as $submenuI => $menu){ ?>
<ul class="dropdown-menu" role="menu">
<li>
<?php
for($a=0; $a < count($menu); $a++){
?>
<a href="<?php echo $menu[$a]['url']; ?>" <?php if(!$menu[$a+1]){ echo "class='last'"; } ?>><?php echo $menu[$a]['text']; ?></a>
<?php } ?>
</li>
</ul>
<?php $submenuI++; } ?>
</li>
<?php
}
?>
<?php } $html->show(); ?>
</ul>
`<ul class="nav navbar-nav">
<li> Home <br>
<b>Warning</b>: Invalid argument supplied for foreach() in <b>/layout.php</b> on line 95<br>
</li>
<li class="dropdown open">
About<span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>
About
Board of Directors
Structure
</li>
</ul>
</li>
<li>
Standards & Codes
<ul class="dropdown-menu" role="menu">
<li>
About
Board of Directors
Structure
</li>
</ul>
</li>
<li class="dropdown">
Resources <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>
About
Board of Directors
Structure
</li>
</ul>
<ul class="dropdown-menu" role="menu">
<li>
Frequently Asked Questions
News Archives
Resource Links
Safety Alerts
</li>
</ul>
</li>
<li>Contact Us
<ul class="dropdown-menu" role="menu">
<li>
About
Board of Directors Structure
</li>
</ul>
<ul class="dropdown-menu" role="menu">
<li>
Frequently Asked Questions
News Archives
Resource Links
Safety Alerts
</li>
</ul>
</li>
</ul>`
Under the first "foreach" loop you should reset your $subs array to make sure it's empty. Otherwise it gets down to the second loop, sees the old "$subs" from the previous loop, and loops through it again:
foreach ( $menuitems as $item ) {
$subs = array();
if ($item ['submenu']) {
$subs [$submenuI] = $item ['submenu'];
}
...
I found the mixture of PHP and HTML hard to read, so I rewrote the code to make it easier for my eyes:
$html = new cacheHTML ( 'topmenu' );
if (! $html->isCached) {
$menuitems = getMenuLevelsArray ( 25, 1, 1 );
$submenuI = "0";
foreach ( $menuitems as $item ) {
if ($item ['submenu']) {
$subs [$submenuI] = $item ['submenu'];
}
echo '<li';
if($subs[$submenuI]){
echo 'class="dropdown"';
}
echo ">
<a href='{$item['url']}'";
if($subs[$submenuI]){
echo 'class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"';
}
echo ">{$item['text']}";
if($subs[$submenuI]){
echo '<span class="caret"></span>';
}
echo '</a> <!-- submenu begins here -->
';
foreach($subs as $submenuI => $menu){
echo '<ul class="dropdown-menu" role="menu">
<li>
';
for($a = 0; $a < count ( $menu ); $a ++) {
echo "<a href='{$menu[$a]['url']}'";
if(!$menu[$a+1]){
echo "class='last'";
}
echo ">{$menu[$a]['text']}</a>
";
}
echo '</li>
</ul>
';
$submenuI++;
}
echo '</li>';
}
}
$html->show(); ?>
</ul>
I hope this helps!
Finally got it...
I need to learn me some more PHP if statements and foreach's
here is what I did:
<ul class="nav navbar-nav">
<?php
$html = new cacheHTML('topmenu');
if(!$html->isCached){
$menuitems = getMenuLevelsArray(25, 1 , 1);
$submenuI = "0";
foreach($menuitems as $item){
if($item['submenu']){
$subs[$submenuI] = $item['submenu'];
}
?>
<li <?php if($subs[$submenuI]){ ?> class="dropdown" <?php }?>>
</span> <?php }?>
<!-- submenu begins here -->
<?php foreach($subs as $submenuI => $menu){ ?>
<ul class="dropdown-menu" role="menu">
<li>
<?php
for($a=0; $a < count($menu); $a++){
?>
<a href="<?php echo $menu[$a]['url']; ?>" <?php if(!$menu[$a+1]){ echo "class='last'"; } ?>><?php echo $menu[$a]['text']; ?></a>
<?php } ?>
</li>
</ul>
<?php $submenuI++; } ?>
</li>
<?php
}
?>
<?php } $html->show(); ?>
</ul>
How do I do a query to the case of multi-level menus in yii?
For example I have a table like this :
menu_name will follow parrent_id it has, so it will form a hierarchical menu
How do I use Yii DAO on my layout so that the output as html above
My Code :
<?php
Yii::import('zii.widgets.CMenu', true);
class ActiveMenu extends CMenu
{
public function init(){
$criteria = new CDbCriteria;
$criteria->condition='published=:idpub AND menu_controller=:menu';
$criteria->params=array(':idpub'=>1, ':menu'=>'#');
$items = Menu::model()->findAll($criteria);
echo "<ul id='yw1' class='nav'>";
foreach ($items as $item)
{
echo "<li class='dropdown'>
<a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
<i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>
</li>";
}
echo "</ul>";
parent::init();
}
}
But in the above code I only get parents data. How do I want to loop to get the childs data include in every parents menu like this html code :
<ul id="yw1" class="nav">
<li><span class="icon-white icon-tasks"></span> Home</li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<i class="icon-white icon-list"></i>Master<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Expedition</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Partner</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> User</li>
</ul>
</li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<i class="icon-white icon-list"></i>Transaction<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Process In</li>
</ul>
<ul class="dropdown-menu">
<li><span class="icon-bar icon-list"></span> Process Out</li>
</ul>
etc ....
etc ....
etc ....
</li>
</ul>
You should first have the relations set up properly in your Menu model (Menu.php).
public function relations()
{
return array(
'parent' => array(self::BELONGS_TO, 'Menu', 'parent_id'),
'children' => array(self::HAS_MANY, 'Menu', 'parent_id'),
);
}
And then modify the codes:
//other codes
foreach ($items as $item)
{
echo "<li class='dropdown'>
<a href=".$item->menu_controller." data-toggle='dropdown' class='dropdown-toggle'>
<i class='icon-white icon-list'></i>".$item->menu_name."<b class='caret'></b></a>
</li>";
foreach ($item->children as $child) {
echo "<ul class='dropdown-menu'>
<li><a href='".$child->menu_controller."'><span class='icon-bar icon-list'></span> ".$child->menu_name."</a></li></u>";
}
}
//other codes