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
));
});
Related
I have 5 menu in the website which is coming dynamically :
menu1 menu2 menu3 menu4 menu5
Now I want to add menu6 before menu5 using php.
My code:
foreach ($collection as $category) {
$i++;
$menuCategory = $this->getCategoryAsArray($category, $currentCategory);
$class = '';
$class .= 'nav'. $i;
if($i == 1) {
$class .= ' first';
} elseif ($i == $count) {
$class .= ' last';
}
if($menuCategory['is_active']) {
$class .= ' active';
}
//if($this->hasChildProduct($category)) {
//$class .= ' parent';
//}
if($this->hasChildSubCategory($category)) {
$class .= ' parent';
}
$class .= ' level-top';
$html .= '<li class="level0 '. $class .'">';
$html .= '<a href="'. $menuCategory['url'] .'">';
$html .= '<span>'. $menuCategory['name'] .'</span>';
$html .= '</a>';
//if($this->hasChildProduct($category)) {
//$html .= $this->getChildProductMenuHtml($category, $i);
//}
if($this->hasChildSubCategory($category)) {
$html .= $this->getChildSubcategoryMenuHtml($category, $i);
}
$html .= '</li>';
}
Menu6 is the static link which code is:
<li class="vertical-submenu" id="static-menu"><a href="<?php echo $block->getUrl('menu6')?>"><?php echo __('menu6')?></li>
I am not getting the code because i am not aware of values coming in the array in foreach loop.
But I can explain the situation to you via Algorithm.
$i = 0;
foreach(expression){
if(value == "menu5"){
write("Menu 4")
}
Write("Menu ".$i)
$i++;
}
Hope your problem is resolved with this. If you need anything else or want to elaborate more about you problem text me at shahrukhusmaani#gmail.com
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>';
}
This is my current array that displays list items in a unordered list.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$output .= '<li><a href="'.$image['image'].'" data-width="'.$img_size[0].'" data-height="'.$img_size[1].'"';
$output .= '</a></li>';
}
$output .= '</ul>';
}else{
$output = '<label>Bottle Color</label><ul></ul>';
}
echo $output;
die();
}
In total there will be up to 16 list items generated by this. I need each list item to have its own class eg: list class="red", list class="green", etc. Any idea how i go about achieving this?
Found the solution thanks to Anant. Had to declare the class array like below.
function get_bottle_colors() {
if(empty($_GET['cap_id'])) return false;
$constructor_img = get_post_meta($_GET['cap_id'], 'product_constructor_image', true);
if(is_array($constructor_img) && count($constructor_img)>0 && !empty($constructor_img[0]['title'])){
$output = '<label>Bottle Color</label><ul>';
$i = 0;
$class_array = array("a","b","c","d","e","f","g","h","i","j","k","l","n","m","n","o","p");
foreach ($constructor_img as $key => $image) {
if(empty($image['image'])) continue;
$category = 9;
$img_size = getimagesize($image['image']);
$output .= '<li class= "'.$class_array[$i].'"><a href="'.$image['image'].'" data-width="'.$img_size[0].'"
data-height="'.$img_size[1].'"';
$output .= 'data-id="'.$_GET['cap_id'].'_'.$key.'" data-part="#constructor-bottles" class="sub-caps">'.$image['title'];
$output .= '</a></li>';
$i++;
}
$output .= '</ul>';
}else{
$output = '
<label>Bottle Color</label><ul></ul>'; } echo $output; die(); }
I what to put a span element for $term['nodes']
I have tried to put after bracket and between but nothing works for me
if (isset($term['nodes'])) {
$term['name'] = $term['name'] . ' (' . $term['nodes'] . ')';
}
here is the all functin
function bootstrap_taxonomy_menu_block($variables) {
$tree = $variables['items'];
$config = $variables['config'];
$num_items = count($tree);
$i = 0;
$output = '<ul class="nav nav-pills nav-stacked">';
foreach ($tree as $tid => $term) {
$i++;
// Add classes.
$attributes = array();
if ($i == 1) {
$attributes['class'][] = '';
}
if ($i == $num_items) {
$attributes['class'][] = '';
}
if ($term['active_trail'] == '1') {
$attributes['class'][] = 'active-trail';
}
if ($term['active_trail'] == '2') {
$attributes['class'][] = 'active';
}
// Alter link text if we have to display the nodes attached.
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
}
// Set alias option to true so we don't have to query for the alias every
// time, as this is cached anyway.
$output .= '<li' . drupal_attributes($attributes) . '>' . l($term['name'], $term['path'], $options = array('alias' => TRUE));
if (!empty($term['children'])) {
$output .= theme('taxonomy_menu_block__' . $config['delta'], (array('items' => $term['children'], 'config' => $config)));
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
i what this for the bootstrap cdn class , i have move the function on template.php , of drupal theme , but the span element is in plain text in browser
Try this:
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
echo $term['name']; // To see the output
}
I have this function working great
<?php
$categories = get_categories(array(
'hide_empty' => 0,
'taxonomy' => 'category',
'orderby' => 'title',
'order' => 'ASC'
));
foreach ($categories as $category) {
$firstletter = strtoupper(substr($category->name,0,1));
$link = get_category_link( $category->term_id );
if ($firstletter != $current) {
$postlist .= "<b><a name='$firstletter'> $firstletter </a></b><br>\n";
$nav .= "<a href='#$firstletter'> $firstletter </a> ";
$current = $firstletter;
}
$postlist .= "<a href='$link'>" . $category->cat_name . "</a><br>\n";
}
print $nav . "<br>" . $postlist;
?>
What I need, is to wrap the entire list starting with the letter. For example, Letter M will be wrapped in a div with all the categories starting with M.
This is what I currently get: http://i.imgur.com/PEmJw03.png - Any help would be appreciated, thank you.
You seem to be on the right path. I think this is what you are requesting to do:
$closeLastDiv = false;
foreach ($categories as $category) {
$firstletter = strtoupper(substr($category->name,0,1));
$link = get_category_link( $category->term_id );
if ($firstletter != $current) {
if ($firstletter == ""){ //first time through
$postlist .= "<div>";
$closeLastDiv = true;
} else {
$postlist .= "</div><div>";
}
$postlist .= "<b><a name='$firstletter'> $firstletter </a></b><br>\n";
$nav .= "<a href='#$firstletter'> $firstletter </a> ";
$current = $firstletter;
}
$postlist .= "<a href='$link'>" . $category->cat_name . "</a><br>\n";
}
if ($closeLastDiv) {
$postlist .= "</div>";
}
All you need is comparte the current letter with before letter, and if changed print the starting wrapper + content. When the letter changes, close the wrapper and open the next wraper.
Something like this:
$current = "-";
$postlist .= "<div>";
foreach ($categories as $category) {
$firstletter = strtoupper(substr($category->name,0,1));
$link = get_category_link( $category->term_id );
if ($firstletter != $current) {
$postlist .= "</div><div id='{$firstletter}'>";
$postlist .= "<b><a name='{$firstletter}'>".$firstletter."</a></b><br>\n";
$nav .= "<a href='#{$firstletter}'>".$firstletter."</a> ";
$current = $firstletter;
}
$postlist .= "<a href='$link'>" . $category->cat_name . "</a><br>";
}
$postlist .= "</div>";
The first div remain empty, but all other are ok.