Wordpress menu function - php

Ok so i made my own theme and i used this code for my menu:
function the_nav_menu() {
$menu_name = 'nav-primary';
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$current_item = 0;
foreach ((array) $menu_items as $key => $menu_item) {
$current_item++;
$title = $menu_item->title;
$url = $menu_item->url;
if($current_item == 3)
$menu_list .= "\t\t\t\t\t". '<li>'. $title .'<i class="material-icons right">arrow_drop_down</i><ul class="dropdown-content" id="dropdown-nav">' ."\n";
else
$menu_list .= "\t\t\t\t\t". '<li>'. $title .'</li>' ."\n";
}
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list . '</ul></li>';
}
When i try to use that menu in the header.php i get the following error:
Notice: Undefined variable: menu_list in /home/u191493746/public_html/wp-content/themes/startedu/functions.php on line 56
Any ideeas?

before if inizialize $menu_list="";
function the_nav_menu() {
$menu_name = 'nav-primary';
$menu_list="";
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$current_item = 0;
foreach ((array) $menu_items as $key => $menu_item) {
$current_item++;
$title = $menu_item->title;
$url = $menu_item->url;
if($current_item == 3)
$menu_list .= "\t\t\t\t\t". '<li>'. $title .'<i class="material-icons right">arrow_drop_down</i><ul class="dropdown-content" id="dropdown-nav">' ."\n";
else
$menu_list .= "\t\t\t\t\t". '<li>'. $title .'</li>' ."\n";
}
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list . '</ul></li>';
}

Related

Display taxonomy of a post in Wordpress

I have a page where all my WP posts are displayed. The category of the post displays fine but when I try to also display the taxonomy, it doesn't work.
This is working :
<span><i class="fa fa-list"></i><?php $categories = get_the_category();
$separator = ", ";
$output = '';
if($categories){
if($link_on_cat == "yes"){
foreach($categories as $category){
$output .= '' . $category->cat_name . '' . $separator;
}
} else {
foreach($categories as $category){
$output .= $category->cat_name . $separator;
}
}
echo trim($output, $separator);
} ?></span>
This is not working :
<span><i class="fa fa-list"></i><?php $categories = get_the_category();
$separator = ", ";
$output = '';
global $post, $post_id;
$post_type = $post->post_type;
$taxonomies = get_object_taxonomies($post_type);
if($categories){
if($link_on_cat == "yes"){
foreach($categories as $category){
$output .= '' . $category->cat_name . '' . $separator;
}
foreach ($taxonomies as $taxonomy) { $out .= "<li>".$taxonomy.": "; $terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ) {
foreach ( $terms as $term )
$out .= ''.$term->name.' ';
} }
} else {
foreach($categories as $category){
$output .= $category->cat_name . $separator;
}
}
echo trim($output, $separator);
} ?></span>
Of course I'm doing something wrong, but what ?

menu in my wordpress theme don't show 3 level it's only show 2 level

i want show three level in my main menu but it's only show two level this is my function for menu
function print_menu($menu_name){
// $menu_name = 'main-menu';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '';
$count = 0;
$submenu = false;
$cpi=get_the_id();
foreach( $menu_items as $current ) {
if($cpi == $current->object_id ){if ( !$current->menu_item_parent ) {$cpi=$current->ID;}else{$cpi=$current->menu_item_parent;}$cai=$current->ID;break;}
}
foreach( $menu_items as $menu_item ) {
$link = $menu_item->url;
$title = $menu_item->title;
$menu_item->ID==$cai ? $ac2=' current_menu' : $ac2='';
if ( !$menu_item->menu_item_parent ) {
$parent_id = $menu_item->ID;$parent_id==$cpi ? $ac=' active' : $ac='';
if(!empty($menu_items[$count + 1]) && $menu_items[ $count + 1 ]->menu_item_parent == $parent_id ){//Checking has child
$menu_list .= '<li class="nav-item dropdown'.$ac.'"><a class="nav-link dropdown-toggle" href="'.$link.'" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span>'.$title.'</span></a>';
}else{
$menu_list .= '<li class="nav-item '.$ac.'">' ."\n";$menu_list .= '<span>'.$title.'</span>' ."\n";
}
}
if ( $parent_id == $menu_item->menu_item_parent ) {
if ( !$submenu ) {
$submenu = true;
$menu_list .= '<div class="dropdown-menu" aria-labelledby="navbarDropdown">';
}
$menu_list .= ''.$title.'' ."\n";
if(empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id && $submenu){
$menu_list .= '</div>' ."\n";
$submenu = false;
}
}
if (empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id ) {
$menu_list .= '</li>' ."\n";
$submenu = false;
}
$count++;
}
} else {
$menu_list = '<li>Menu "' . $menu_name . '" not defined.</li>';
}
echo $menu_list;
}

Adding products to Magento menu

Today I am trying to add products like this:
– Category
– – SubCategory1
– – – Product 1
– – – Product 2
– – – Product 3
– – SubCategory2
– – – Product 1
– – – Product 2
– – – Product 3
My current magento version is ver. 1.9.0.1
I was searching about it on the internet I found this code but its giving me syntax error not sure why. Please check but this code tested on Magento ver 1.4 I am not sure if it will work for me or not.
protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
$isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
{
if (!$category->getIsActive()) {
return '';
}
$html = array();
// get all children
if (Mage::helper('catalog/category_flat')->isEnabled()) {
$children = (array)$category->getChildrenNodes();
$childrenCount = count($children);
} else {
$children = $category->getChildren();
$childrenCount = $children->count();
}
$hasChildren = ($children && $childrenCount);
// select active children
$activeChildren = array();
foreach ($children as $child) {
if ($child->getIsActive()) {
$activeChildren[] = $child;
}
}
$activeChildrenCount = count($activeChildren);
$hasActiveChildren = ($activeChildrenCount > 0);
// prepare list item html classes
$classes = array();
$classes[] = 'level' . $level;
$classes[] = 'nav-' . $this->_getItemPosition($level);
if ($this->isCategoryActive($category)) {
$classes[] = 'active';
}
$linkClass = '';
if ($isOutermost && $outermostItemClass) {
$classes[] = $outermostItemClass;
$linkClass = ' class="'.$outermostItemClass.'"';
}
if ($isFirst) {
$classes[] = 'first';
}
if ($isLast) {
$classes[] = 'last';
}
if ($hasActiveChildren) {
$classes[] = 'parent';
}
// prepare list item attributes
$attributes = array();
if (count($classes) > 0) {
$attributes['class'] = implode(' ', $classes);
}
if ($hasActiveChildren && !$noEventAttributes) {
$attributes['onmouseover'] = 'toggleMenu(this,1)';
$attributes['onmouseout'] = 'toggleMenu(this,0)';
}
// assemble list item with attributes
$htmlLi = '<li'; foreach="" ($attributes="" as="" $attrname=""> $attrValue) {
$htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
}
$htmlLi .= '>';
$html[] = $htmlLi;
$html[] = '<a href="'.$this->getCategoryUrl($category).'" '.$linkclass.'="">';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
// Grabbing the products for the category if it's level is 1
if ($level == 1) {
$catId = $category->getId();
$categorie = new Mage_Catalog_Model_Category();
$categorie->load($catId); // this is category id
$collection = $categorie->getProductCollection()->addAttributeToSort('name', 'asc');
$html[] = '<ul>';
foreach ($collection as $pc)
{
$p = new Mage_Catalog_Model_Product();
$p->load($pc->getId());
$data = $p->_data;
$html[] = '<li>'.$data['name'] .'</li>';
}
$html[] = "</ul>\n";
}
// Done
// render children
$htmlChildren = '';
$j = 0;
foreach ($activeChildren as $child) {
$htmlChildren .= $this->_renderCategoryMenuItemHtml(
$child,
($level + 1),
($j == $activeChildrenCount - 1),
($j == 0),
false,
$outermostItemClass,
$childrenWrapClass,
$noEventAttributes
);
$j++;
}
if (!empty($htmlChildren)) {
if ($childrenWrapClass) {
$html[] = '<div class="' . $childrenWrapClass . '">';
}
$html[] = '<ul class="level' . $level . '">';
$html[] = $htmlChildren;
$html[] = '</ul>';
if ($childrenWrapClass) {
$html[] = '</div>';
}
}
$html[] = '';
$html = implode("\n", $html);
return $html;
}
</li';>
Its giving syntax error on line:
$htmlLi = '<li'; foreach="" ($attributes="" as="" $attrname=""> $attrValue) {
and at the end of code:
</li';>

Restore standard Mage_Catalog_Block_Navigation functionality in top navigation for a single website in a multi-store Magento installation

I'm currently working on a Magento installation with multiple websites and store views. I'm attempting to redesign one of the four sub websites.
There has been a custom module added which appears to rewrite/extend the default top navigation menu to automatically include CMS pages and add a banner slot to the menu. The problem is that I want to restore the default Magento top menu (i.e. no CMS pages) for a single website view.
I've tried disabling the module inside System -> Config -> Advanced -> Advanced for that website, however this seems to make the entire top navigation disappear. I believe the function I want to remove is this:
<?php
/**
* extend functions from navigation.php
*
*/
class Fvzzy_Category_Block_Navigation extends Mage_Catalog_Block_Navigation
{
protected $_menus;
protected function _getCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
$isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
{
if (!$category->getIsActive()) {
return '';
}
$html = array();
// get all children
if (Mage::helper('catalog/category_flat')->isEnabled()) {
$children = (array)$category->getChildrenNodes();
$childrenCount = count($children);
} else {
$children = $category->getChildren();
$childrenCount = $children->count();
}
$hasChildren = ($children && $childrenCount);
// select active children
$activeChildren = array();
foreach ($children as $child) {
if ($child->getIsActive()) {
$activeChildren[] = $child;
}
}
$activeChildrenCount = count($activeChildren);
$hasActiveChildren = ($activeChildrenCount > 0);
// prepare list item html classes
$classes = array();
$classes[] = 'level' . $level;
//$classes[] = 'nav-' . $this->_getItemPosition($level);
$text = preg_replace('/[^A-Za-z0-9]/i', '-', strtolower($category->getName()));
//$classes[] = $category->;
if ($this->isCategoryActive($category)) {
$classes[] = 'active';
}
$linkClass = '';
if ($isOutermost && $outermostItemClass) {
$classes[] = $outermostItemClass;
$linkClass = ' class="'.$outermostItemClass.'"';
}
if ($isFirst) {
$classes[] = 'first';
}
if ($isLast) {
//$classes[] = 'last';
}
if ($hasActiveChildren) {
$classes[] = 'parent';
}
if(strtolower($text) == 'sale') $classes[] = strtolower($text);
// prepare list item attributes
$attributes = array();
if (count($classes) > 0) {
$attributes['class'] = implode(' ', $classes);
}
if ($hasActiveChildren && !$noEventAttributes) {
$attributes['onmouseover'] = 'toggleMenu(this,1)';
$attributes['onmouseout'] = 'toggleMenu(this,0)';
}
// assemble list item with attributes
$htmlLi = '<li';
foreach ($attributes as $attrName => $attrValue) {
$htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
}
$htmlLi .= '>';
$html[] = $htmlLi;
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
// render children
$htmlChildren = '';
$j = 0;
foreach ($activeChildren as $child) {
$htmlChildren .= $this->_getCategoryMenuItemHtml(
$child,
($level + 1),
($j == $activeChildrenCount - 1),
($j == 0),
false,
$outermostItemClass,
$childrenWrapClass,
$noEventAttributes
);
$j++;
}
$promotion = $this->addPromotions($category->getId());
if (!empty($htmlChildren)) {
if ($childrenWrapClass) {
$html[] = '<div class="' . $childrenWrapClass . '">';
}
$html[] = '<ul class="level' . $level . '"><div class="menu-text">';
if($promotion) $html[] = '';
$html[] = $htmlChildren;
if($promotion) $html[] = '';
if($promotion) $html[] = '</div><li class="level1 parent menu-promotion">'.$promotion.'</li>';
$html[] = '</ul>';
if ($childrenWrapClass) {
$html[] = '</div>';
}
}
$html[] = '</li>';
$html = implode("\n", $html);
return $html;
}
public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
{
$activeCategories = array();
foreach ($this->getStoreCategories() as $child) {
if ($child->getIsActive()) {
$activeCategories[] = $child;
}
}
$activeCategoriesCount = count($activeCategories);
$hasActiveCategoriesCount = ($activeCategoriesCount > 0);
if (!$hasActiveCategoriesCount) {
return '';
}
$html = '';
$j = 0;
foreach ($activeCategories as $category) {
$html .= $this->_getCategoryMenuItemHtml(
$category,
$level,
($j == $activeCategoriesCount - 1),
($j == 0),
true,
$outermostItemClass,
$childrenWrapClass,
true
);
$j++;
}
//main store
if(Mage::app()->getStore()) $html .= $this->addMenu($childrenWrapClass,Mage::app()->getStore());
return $html;
}
protected function addPromotions($id = 0){
if($id){
$base = Mage::getBaseUrl('media',true).'promotion_box_images/';
$file_base = Mage::getBaseDir('media').'/promotion_box_images';
$html = ''; $img = '';
$promotion = Mage::getModel('promotion/box')->getCollection()->addFieldToFilter('menu_ids',array(
array('like'=>$id),
array('like'=>$id.',%'),
array('like'=>'%,'.$id.',%'),
array('like'=>'%,'.$id)))->setOrder('position')->getFirstItem(); //default is desc
if($promotion && $promotion->getId()){
$type=$promotion->getDisplayType();
$link = ''; $img = ''; $content = ''; $width = '';
if(file_exists($file_base.'/'.$promotion->getImage())){
list($width) = getimagesize($file_base.'/'.$promotion->getImage());
}
if($type!=null){
switch($type){
case 0:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if(trim($promotion->getLink())!='') $link = $promotion->getLink();
break;
case 1:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if($promotion->getCategoryId()){
$c = Mage::getModel('catalog/category')->load($promotion->getCategoryId());
if($c->getId() && $c->getIsActive()) $link = Mage::helper('catalog/category')->getCategoryUrl($c);
}
break;
case 2:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if($promotion->getProductSku()){
$id = Mage::getModel('catalog/product')->getIdBySku($promotion->getProductSku());
$p = Mage::getModel('catalog/product')->load($id);
if($p->getId()) $link = $p->getProductUrl();
}
break;
case 3:
if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
if($promotion->getPageId()){
$_link = Mage::Helper('cms/page')->getPageUrl($promotion->getPageId());
if($_link != ''){
$nodes = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection()
->addFieldToFilter('page_id', array('eq' => $promotion->getPageId()));
foreach($nodes as $_node){
$link = $_node->getRequestUrl();
}
if(!$link){$link = Mage::Helper('cms/page')->getPageUrl($promotion->getPageId());}
}
}
break;
case 4:
$content = $promotion->getContent();
break;
}
}
if($img && $link){
$html = ''.$img.'';
}
elseif($img){
$html = ''.$img.'';
}
else{
$html = $content;
}
}
return $html;
}else{return false;}
}
//$menu = array( array('label'=>'News','href'=>'blog','module'=>'blog','sub'=>array(array('label'=>'submenu','href'=>'blog2','module'=>'blog'))) );
public function setOtherMenu($menu){
$this->_menus = $menu;
}
public function addMenu( $childrenWrapClass = '', $store ) {
$this->_node_ids = array();
$current_module = $this->getRequest()->getModuleName();
$page_id = $this->getRequest()->getParam('page_id');
// get all the cms page nodes not folder or containers
$nodes = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection()
->joinMetaData()
->addFieldToFilter( 'level', 1 )
//->addFieldToFilter( 'menu_visibility', 1 )
->addFieldToFilter( 'main_table.page_id', array( 'notnull' => true ))
->setOrder('sort_order','ASC');
if ( $store instanceof Mage_Core_Model_Store ) {
$store = $store->getId();
$storeIds = array( 0, $store );
$nodes->getSelect()
->joinLeft( array( 'cs' => 'cms_page_store' ), 'main_table.page_id=cs.page_id', array( 'cs.store_id' ) )
->where( 'cs.store_id IN ('. implode( ',', $storeIds ) .')' ); // not the best sql here but it works as store id will be one at a time.
}
$active_node = Mage::registry('current_cms_hierarchy_node');
$active_node_ids = array();
if($active_node){
$xpath = $active_node->getXpath();
$all = explode( '/', $xpath );
foreach ( $all as $index ) $active_node_ids[] = $index;
}
$this->_node_ids = $active_node_ids;
if ( $this->_menus ) {
$menus = $this->_menus;
}
else {
$menus = array();
}
$links = '';
foreach ( $nodes as $node ) {
$tree = $node->setCollectActivePagesOnly(true)
->setCollectIncludedPagesOnly(true)
->setTreeMaxDepth(0)
->setTreeIsBrief(1) //this way it won't show the container link
->getTreeSlice(0, 1); //up to tree top and down to one level
$links .= $this->drawCmsMenu($tree,0,1,$childrenWrapClass);
}
foreach ( $menus as $menu ) {
if(isset($menu['sub'])) $has_sub_class = ' parent'; else $has_sub_class = '';
if (isset($menu['module']) && $current_module == $menu['module'] ) {
$links .= '<li class="level0 level-top active'.$has_sub_class.'">';
}
else {
$links .= '<li class="level0 level-top'.$has_sub_class.'">';
}
$html = '';
if(isset($menu['sub']) && is_array($menu['sub'])){
$submenu = $menu['sub'];
$html .= '<div class="'.$childrenWrapClass.'"><ul class="level0">';
foreach($submenu as $sub){
if(is_array($sub) &&isset($sub['href']) && isset($sub['label']))
//2012-11-20 AU TEAM coding for change <h2> -> <span>
$html .= '<li class="level1"><span>'.$sub['label'].'</span></li>';
}
$html .= '</div>';
}
if(isset($menu['href']) && isset($menu['label']))
$links .= '<a class="level-top" href="'.Mage::getBaseUrl().$menu['href'].'"><span>'.$menu['label'].'</span></a>';
//add submenu
if($html != '') $links .= $html;
$links .= '</li>';
}
return $links;
}
/* Add Maximal Depth filter
* 2012.04.11 by Bruce
*/
public function drawCmsMenu( array $tree, $parentNodeId = 0, $level = 0, $childrenWrapClass = '' ) {
$html = '';
if ( !isset($tree[$parentNodeId ])) return $html;
foreach ( $tree[ $parentNodeId ] as $nodeId => $node ) {
/* #var $node Enterprise_Cms_Model_Hierarchy_Node */
$nested = $this->drawCmsMenu( $tree, $nodeId, $node->getLevel()+1, $childrenWrapClass);
$hasChildren = ( $nested != '' );
// set style classes
$class = array();
$class[] = 'level'. ( $level - 1 );
if ( $level - 1 == 0 )
$class[] = 'level-top';
if ( $this->_node_ids && in_array( $node->getNodeId(), $this->_node_ids ) )
$class[] = 'active';
if ( $hasChildren )
$class[] = 'parent';
// li wrapper header
$html .= '<li class="'. implode( ' ', $class ) .'">';
$html .= $this->_getNodeLabel( $node, $level - 1 );
// div wrapper header
if ( $hasChildren ){
if ( $childrenWrapClass ) $html .= '<div class="'. $childrenWrapClass .'">';
$html .= '<ul class="level'. ( $level - 1 ) .'">';
}
// children
$html .= $nested;
// div wrapper footer
if ( $hasChildren ) {
$html.='</ul>';
if ( $childrenWrapClass ) $html .= '</div>';
}
// li wrapper footer
$html .= '</li>';
}
return $html;
}
protected function _getNodeLabel($node,$level)
{
if($level==0) $class = 'level-top'; else $class = '';
if($node->getPageTitle()) return '<a class="'.$class.'" href="'.$node->getUrl().'"><span>'.$node->getPageTitle().'</span></a>';
else return '<a class="'.$class.'" href="'.$node->getUrl().'"><span>'.$node->getLabel().'</span></a>';
}
}
I know that the config file for this module looks like the following:
<?xml version="1.0"?>
<config>
<modules>
<Fvzzy_Category>
<version>0.1.0</version>
</Fvzzy_Category>
</modules>
<global>
<blocks>
<fvzzy_category>
<class>Fvzzy_Category_Block</class>
</fvzzy_category>
<catalog>
<rewrite>
<navigation>Fvzzy_Category_Block_Navigation</navigation>
</rewrite>
</catalog>
</blocks>
<resources>
<fvzzy_category_setup>
<setup>
<module>Fvzzy_Category</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
</fvzzy_category_setup>
</resources>
</global>
</config>
And that if I'm able to remove the following part from that file, I get my intended outcome:
<catalog>
<rewrite>
<navigation>Fvzzy_Category_Block_Navigation</navigation>
</rewrite>
</catalog>
The problem is, I just want to get the top navigation menu function restored back to normal for one website view.
Is there any way to overwrite that navigation rewrite event for one store/website view?
If not, is it possible to disable this module without it disabling my whole top menu for that website view?
I've tried completely duplicating the module, disabling the original one and enabling the new one in my desired website but that also appears to be making the top menu disappear entirely.
I'm really not sure what else to do with this - if anyone is able to help it would be hugely appreciated.
Thanks so much.
if you want this extension still for your Magento application, then disabling the extension will not produce expecting result.
In this case, you can do two things
Overwrite this custom extension with your module and make changes according to your need
comment out the config section that defines that core block overwrite. You have already tried it and worked it
There is another alternative. You can do a condition checking inside the rewrite block class. This condition check somewhat look like this.
if(Mage::app()->getStore()->getCode()== 'website_code_for_sub_site'){
//execute existing code
}
This method ensure that, the method that defined inside the rewrite block class will get executed only when the site with a code website_code_for_sub_site is viewing.
Hope that helps

slim php function menu grom database

I a trying to generate a menu for a webshop. I have written a method for it.
Slim says "undefined variable on line 53. I have marked the line with *
Class Menu {
function generate_menu() {
$menu = 'ul';
$catergory = Model::factory('category')->find_many();
***foreach ($category as $item) {***
$menu .= '<li>';
$menu .= '' . $item->title . '';
$menu .= '</li>';
$count = Model::factory('sub_category')->where('category_title', $item->title)->count();
if ($count >= 0) {
$sub_catergory = Model::factory('sub_category')->where('category_title', $item->title)->find_many();
$menu .= '<ul>';
foreach ($sub_catergory as $sub_catergory) {
$menu .= '<li>';
$menu .= '' . $sub_category->title . '';
$menu .= '</li>';
}
$menu .= '</ul>';
}
}
$menu .= '</ul>';
return $menu;
}
}
Could someone show me how to pass this function to the template in slim php ?
This is the get request i am trying to pass the returned $menu variable to.
$app->get('/', function () use ($app) {
$site_info = Model::factory('Site_info')->where('id', '1')->find_one();
$app->render('front_page.php', array(
'site_name' => $site_info->site_name,
'site_slogan' => $site_info->site_slogan,
'domain' => $site_info->domain
));
});
Also could someone point me in the right direction to what paris returns if a database request returns nothing?
My method for creating the menu :
Class Menu {
function generate_menu() {
$menu = '<ul>';
$category = Model::factory('category')->find_many();
foreach ($category as $item) {
$menu .= '<li>';
$menu .= '' . $item->title . '';
$menu .= '</li>';
$count = Model::factory('sub_category')->where('category_title', $item->title)->count();
if ($count >= 0) {
$sub_category = Model::factory('sub_category')->where('category_title', $item->title)->find_many();
$menu .= '<ul>';
foreach ($sub_category as $value) {
$menu .= '<li>';
$menu .= '' . $value->title . '';
$menu .= '</li>';
}
$menu .= '</ul>';
}
}
$menu .= '</ul>';
return $menu;
}
}
After that i assigned the method output to a variable and passed it to the template :
$app->get('/', function () use ($app) {
$site_info = Model::factory('Site_info')->where('id', '1')->find_one();
$category_menu = Menu::generate_menu();
$app->render('front_page.php', array(
'site_name' => $site_info->site_name,
'site_slogan' => $site_info->site_slogan,
'domain' => $site_info->domain,
'menu' => $category_menu
));
});

Categories