I want to look for any html files in a specific folder, and build up my menu and sub-menus based on the results:
For example my project files may be : 'project1_qa_qa2.html',..., 'project2_dev_dev1.html', etc... And the explode() should return 'project1', 'qa', 'qa2' ; ... ; 'project2', 'dev', 'dev1' ; ...
I need to loop through each elements {project ( 1 or 2), department (qa or dev) , and id (dev1, qa2, ..)} to build my menu / sub-menus in a dynamic way.
I got a base source from : http://callmenick.com/_development/slide-down-menu/
I added my hardcoded sample code, to show what I'd wanted it to be like.
<?php
$cdir = scandir('./projects');
foreach ($cdir as $filename){
if(preg_match('/(\w+).html/',$filename,$project_name)){
$projects_details = explode('_',$project_name[1]);
}
?>
<div class="container">
<nav>
<ul class="content clearfix">
<li>Welcome</li>
<li class="dropdown">
Projects
<ul class="sub-menu" style="display: none;">
<li>Project1</li>
<li class="dropdown">
Project2
<ul class="sub-menu" style="display: none;">
<li class="dropdown">
Quality Insurance
<ul class="sub-menu" style="display: none;">
<li>QA1</li>
<li>QA2</li>
</ul>
</li>
<li class="dropdown">
Development
<ul class="sub-menu" style="display: none;">
<li>Dev1</li>
<li>Dev2</li>
</ul>
</li>
</ul>
</li>
</ul>
</ul>
</nav>
</div>
This is just to show you which code I've used and point you in the right direction. For scanning the directory:
function getFromDir( $dir ) {
$cdir = scandir( $dir );
$result = array();
foreach( $cdir as $key => $value ) {
if( !in_array( $value, array('.', '..') ) ) {
if( is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) {
$result[$value] = getFromDir($dir . DIRECTORY_SEPARATOR . $value);
} else {
$result[] = $value;
}
}
}
return $result;
}
As shown in the comment for creating the html:
function outputMenu(array $array, $baseUrl = '/') {
$html = '';
foreach( $array as $key => $item ) {
if( is_array( $item ) ) {
$html .= '<li>'.$key.'<ul>';
$html .= outputMenu($item, $baseUrl.$key.'/');
$html .= '</ul></li>';
} else {
$html .= '<li>'.ucfirst(substr($item, 0, -4)).'</li>';
}
}
return $html;
}
echo outputMenu(getFromDir('./projects'));
Related
This class creates a menu with different sub-levels :
class Bootstrap_Collapse_NavWalker extends Walker_Nav_Menu{
public function start_lvl( &$output, $depth = 0, $args = null ) {
$output .= "<ul>";
}
public function end_lvl(&$output, $depth = 0, $args = []){
$output .= "</ul>";
}
public function start_el(&$output, $item, $depth = 0, $args = [], $id = 0){
$itemClasses = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names_li = "";
$class_names_a = "";
$class_names_i = "";
$item_output = "";
$hasChildrenElm = $args->walker->has_children;
if ( $hasChildrenElm ) {
$class_names_li = 'dropdown';
}
if ( $depth === 0 && !$hasChildrenElm ) {
$class_names_a = 'nav-link scrollto';
}
$activeElement = "";
if ( in_array( 'current-menu-item', $itemClasses ) ) $activeElement .'active';
$elementUrl = ! empty( $item->url ) ? esc_url($item->url) : '';
$output .= '<li class="'.$class_names_li.'">';
if ( $hasChildrenElm ) {
$output .= '<a class="'.$class_names_a.$activeElement.'" href="'.$elementUrl.'">
<span>'.$item->title.'</span>';
$class_names_i .= ($depth !== 0) ? "bi bi-chevron-right" : "bi bi-chevron-down";
$output .= '<i class="'.$class_names_i.'"></i>';
}elseif($item->title !== ""){
$output .= '<a class="'.$class_names_a.$activeElement.'" href="'.$elementUrl.'-1">'.$item->title.'</a>';
}
}
public function end_el(&$output, $item, $depth = 0, $args = []){
$output .= "</li>";
}
}
I am waiting for the following HTML :
<ul>
<li><a class="nav-link scrollto" href="#menu1">menu 1</a></li>
<li><a class="nav-link scrollto" href="#menu2">menu 2</a></li>
<li class="dropdown">
<span>Drop Down</span> <i class="bi bi-chevron-down"></i>
<ul>
<li>Drop Down 1</li>
<li class="dropdown">
<span>Deep Drop Down</span> <i class="bi bi-chevron-right"></i>
<ul>
<li>Deep Drop Down 1</li>
</ul>
</li>
</ul>
</li>
</ul>
But It print this HTML :
<ul>
<li><a class="nav-link scrollto" href="#menu1">menu 1</a></li>
<li><a class="nav-link scrollto" href="#menu2">menu 2</a></li>
<li class="dropdown">
<span>Drop Down</span> <i class="bi bi-chevron-down"></i>
<ul>
<a class="" href="#dropdownLink"></a>
<li>
<a class="" href="#dropdownLink"></a>
Drop Down 1
</li>
<li class="dropdown">
<span>Deep Drop Down</span> <i class="bi bi-chevron-right"></i>
<ul>
<a class="" href="#dropdownLink"></a>
<li>
<a class="" href="#dropdownLink"></a>
Deep Drop Down 1
</li>
</ul>
</li>
</ul>
</li>
</ul>
As you can see at each menu level the script prints again the anchor tag of the parent menu, I am not being able to identify when the script loop inserts the parent TAG anchor, you can help me with this problem
Fixed, missing close anchor tag correct code:
class Bootstrap_Collapse_NavWalker extends Walker_Nav_Menu{
public function start_lvl( &$output, $depth = 0, $args = null ) {
$output .= "<ul>";
}
public function end_lvl(&$output, $depth = 0, $args = [])
{
$output .= "</ul>";
}
public function start_el(&$output, $item, $depth = 0, $args = [], $id = 0){
$itemClasses = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names_li = "";
$class_names_a = "";
$class_names_i = "";
$item_output = "";
$hasChildrenElm = $args->walker->has_children;
if ( $hasChildrenElm ) {
$class_names_li = 'dropdown';
}
if ( $depth === 0 && !$hasChildrenElm ) {
$class_names_a = 'nav-link scrollto';
}
$activeElement = "";
if ( in_array( 'current-menu-item', $itemClasses ) ) $activeElement .'active';
$elementUrl = ! empty( $item->url ) ? esc_url($item->url) : '';
if(!empty($elementUrl)){
$output .= '<li class="'.$class_names_li.'">';
if ( $hasChildrenElm ) {
$output .= '<a class="'.$class_names_a.$activeElement.'" href="'.$elementUrl.'">
<span>'.$item->title.'</span>';
$class_names_i .= ($depth !== 0) ? "bi bi-chevron-right" : "bi bi-chevron-down";
$output .= '<i class="'.$class_names_i.'"></i></a>';
}else{
$output .= '<a class="'.$class_names_a.$activeElement.'11" href="'.$elementUrl.'">'.$item->title.'</a>';
}
}
}
public function end_el(&$output, $item, $depth = 0, $args = [])
{
$output .= "</li>";
}
}
I'm a newbie of Drupal. I'm trying to develop a bootstrap 3 template, but I have a problem with the navbar dropdown menu implementation. I followed these steps:
in my mytheme/templates folder I create a page.tpl.php file with the following code:
if ($page['header'])
...
$main_menu = variable_get('menu_main_links_source', 'main-menu');
$tree = menu_tree($main_menu);
print drupal_render($tree);
...
in mytheme folder I create a template.php file with these functions:
function mytheme_menu_tree($variables) {
return '<ul class="nav navbar-nav">' . $variables['tree'] . '</ul>';
}
function mytheme_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
$dropdown = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
$dropdown = 'class="dropdown"';
$element['#localized_options']['attributes']['class'][] = 'dropdown-toggle';
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li ' .$dropdown. ' >' . $output . $sub_menu . "</li>\n";
}
with this code I'm at a good point, but I need to remove classes "nav navbar-nav" from children and add the class "dropdown-menu".
This is the result of my code:
<ul class="nav navbar-nav">
<li>XYZ</li>
<li>ASD</li>
<li class="dropdown">XXX
<ul class="nav navbar-nav">
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</li>
</ul>
and this is what I would like to obtain:
<ul class="nav navbar-nav">
<li>XYZ</li>
<li>ASD</li>
<li class="dropdown">XXX
<ul class="dropdown-menu"> <!-- HERE IS THE DIFFERENCE -->
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</li>
</ul>
Maybe I can do something like this:
function mytheme_menu_tree($variables) {
if ( [check if I'm at the first level] ) {
return '<ul class="nav navbar-nav">' . $variables['tree'] . '</ul>';
} else {
return '<ul class="dropdown-menu">' . $variables['tree'] . '</ul>';
}
}
but I don't know how... Any idea?
I too am a bit of a newbie when it comes to Drupal and was also having this problem. I have tweaked your function and it works for me:
function SeatradeKorea_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
$dropdown = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
$sub_menu = str_replace('nav navbar-nav', 'dropdown-menu', $sub_menu);
$dropdown = 'class="dropdown"';
$element['#localized_options']['attributes']['class'][] = 'dropdown-toggle';
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li ' .$dropdown. ' >' . $output . $sub_menu . "</li>\n";
}
I have added a str_replace() into your #below element to replace the "nav navbar-nav" with "dropdown-menu"
I'm trying to create a Mega Menu using PHP and I'm having a problem getting the structure to output correctly. I've hard-coded the Mega Menu to test everything and it works fine, but obviously I need PHP to create it for me.
I have an example with the Mega Menu hard-coded so everyone can see what I'm trying to create:
http://www.libertyeaglearms.com/dev
Or here's the code:
DESIRED OUTPUT:
<div id="wrapper">
<ul class="mega-menu">
<li class="mega-menu-drop">
Firearms
<div class="mega-menu-content">
<div class="column">
<h4>Rifles</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
<div class="column">
<h4>Handguns</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
<div class="column">
<h4>Shotguns</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
</div>
</li>
<li class="mega-menu-drop">
Archery
<div class="mega-menu-content">
<div class="column">
<h4>Bows</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
<div class="column">
<h4>Arrows</h4>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
</div>
</li>
</ul>
</div>
CURRENT OUTPUT: (very messed up. be warned, lol)
<div id="wrapper">
<ul class="mega-menu">
<li class="mega-menu-drop">
archery
<div class="mega-menu-content">
<div class="column">
<h4>compound</h4>
<ul>
<h4>bows</h4>
<ul>
</div>
</li>
<li class="mega-menu-drop">
firearms
<div class="mega-menu-content">
<div class="column">
<h4>rifle ammunition</h4>
<ul>
<h4>ammunition</h4>
<ul>
</div>
</li>
</ul>
</div>
HERE MY PHP:
$json = json_decode($category->buildDepartments(NULL),true);
function buildDepartments($array,$parent)
{
$html = '';
foreach($array as $category)
{
if($category['parent'] == $parent)
{
if($category['parent'] == NULL)
{
$html .= '<li class="mega-menu-drop">' . "\n\t\t\t\t";
$html .= ''.$category['category_name'].'' . "\n\t\t\t\t";
$html .= '<div class="mega-menu-content">' . "\n\t\t\t\t\t";
$html .= '<div class="column">' . "\n\t\t\t\t\t\t";
$html .= buildDepartments($array,$category['category_id']);
$html .= '</div>' . "\n\t\t\t";
$html .= '</li>' . "\n\t\t\t";
}
else
{
$html .= buildDepartments($array,$category['category_id']);
$html .= '<h4>'.$category['category_name'].'</h4>' . "\n\t\t\t\t\t\t";
$html .= '<ul>' . "\n\t\t\t\t";
}
}
}
return $html;
}
print(buildDepartments($json,NULL));
HERE'S MY DATABSE:
EDIT AFTER BOUNTY
Building off what icktoofay suggested, I cannot seem to figure out the foreach() loops. The problem I'm getting is I get the department name inside the mega menu when I should see category and sub categories. I think the problem is I need to perform a loop with a specific parent id in order to get all the children, but I'm not really sure if that's the problem. Here's the code:
<div id="wrapper">
<ul class="mega-menu">
<?php $json = json_decode($category->buildDepartments(NULL)); ?>
<?php foreach($json as $category): ?>
<li class="mega-menu-drop">
<?php if($category->parent === NULL){echo $category->category_name;} ?>
<div class="mega-menu-content">
<?php foreach($category as $subcategory): ?>
<div class="column">
<?php if($category->category_id === $category->parent){echo '<h4>' . $category->category_name . '</h4>';}?>
<ul>
<?php foreach($category as $subcategory)
{
echo '<li>' . $category->category_name . '</li>';
}
?>
</ul>
</div>
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
Since it's a fixed depth and the format for each level is different, recursion may not be appropriate. Additionally, putting everything in strings is inelegant. Instead, you may want to try weaving the HTML and loops together like this:
<div id="wrapper">
<ul class="mega-menu">
<?php foreach($categories as $category): ?>
<li class="mega-menu-drop">
<?php echo $category->name; ?>
<div class="mega-menu-content">
<?php foreach($category->subcategories as $subcategory): ?>
<!-- and so on -->
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
The code I've used here assumes you've already got it from a sort-of-flat-array to a tree-like structure. For example, if we've got a Category class:
class Category {
public $id;
public $parentID;
public $name;
public $parent;
public $subcategories;
public function __construct($id, $parentID, $name) {
$this->id = $id;
$this->parentID = $parentID;
$this->name = $name;
$this->parent = null; // will be filled in later
$this->subcategories = array(); // will be filled in later
}
}
And an array of associative arrays like you might get from a database call (which we'll call $flatCategories), we can build a bunch of not-yet-connected Category instances like this:
$categories = array();
foreach($flatCategories as $flatCategory) {
$categories[$flatCategory['id']] =
new Category($flatCategory['category_id'],
$flatCategory['parent'],
$flatCategory['category_name']);
}
Then we can connect them all up into a hierarchal structure:
foreach($categories as $category) {
if($category->parentID !== null) {
$category->parent = $categories[$category->parentID];
$category->parent->subcategories[] = $category;
}
}
Now they're all linked up and we only care about keeping references to the roots:
$roots = array();
// This could, of course, be merged into the last loop,
// but I didn't for clarity.
foreach($categories as $category) {
if($category->parentID === null) {
$roots[] = $category;
}
}
Now $roots contains all the root categories, and it's fairly simple to traverse. In my example at the top, I was assuming $categories had something similar to $roots in it.
you need to generate something called a super tree and use a recursive function to echo out each branch, this will allow for an unlimited depth tree, but, it also allows for an unknown depth tree.
First, SQL:
SELECT category_id, parent, category_name
Second, use SQL to create tree:
$super_tree = array();
while(($row = mysql_fetch_assoc($res)) != NULL) {
$parent = $row['parent'] == NULL ? 0 : $row['parent']; //clean up the parent
$super_tree[$parent][$row['category_id']] = $row['category_name'];
}
Third, echo out the tree
function echo_branch($tree_branch, $tree_root) {
echo("<ul>\n"); //open up our list for this branch
foreach($tree_branch as $id => $name) { //foreach leaf on the branch
echo("<li>"); //create a list item
echo("{$name}"); //echo out our link
if(!empty($tree_root[$id])) { //if our branch has any sub branches, echo those now
echo_branch($tree_root[$id], $tree_root); //pass the new branch, plus the root
}
echo("</li>\n"); //close off this item
}
echo("</ul>\n"); //close off this list
}
echo_branch($super_tree[0], $super_tree); //echo out unlimited depth tree structure
this allows for unlimited depth in your tree structure and allows for simple code to be able to create the structure within the code base.
All that you would need to do now is add in your extra's such as classes and your extra html elements in the correct places.
if you are looking to track the current depth of the tree to be able to echo different things depending on the depth, you can make the following alterations
In the function definition
function echo_branch($tree_branch, $tree_root, $depth) {
In the recursive call within the if
echo_branch($tree_root[$id], $tree_root, $depth++);
In the initial call
echo_branch($super_tree[0], $super_tree, 0);
Look like you are using the variable
$category
when you should use
$subcategory
Try this:
<div id="wrapper">
<ul class="mega-menu">
<?php $json = json_decode($category->buildDepartments(NULL)); ?>
<?php foreach($json as $category): ?>
<li class="mega-menu-drop">
<?php if($category->parent === NULL){echo $category->category_name;} ?>
<div class="mega-menu-content">
<?php foreach($category as $subcategory): ?>
<div class="column">
<?php if($subcategory->category_id === $category->parent){echo '<h4>' . $category->category_name . '</h4>';}?>
<ul>
<?php foreach($subcategory as $subcategory2)
{
echo '<li>' . $subcategory2->category_name . '</li>';
}
?>
</ul>
</div>
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
I want add left, center & right class to my ordered lists while loop.
Code
<?php while ($fetch) { ?>
<li>haha</li>
<?php } ?>
Results should be
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>
Let me know
<?php $classes = array("left","center","right");
$i = 0;
while ($fetch) {
?>
<li class="<?php echo $classes[$i++ % 3] ?>">haha</li>
<?php } ?>
$cnt=0;
while ($fetch)
{
switch ($cnt%3)
{
case 0 : $class = 'left'; break;
case 1 : $class = 'center'; break;
case 2 : $class = 'right'; break;
}
echo '<li class="', $class, '">haha</li>';
++$cnt;
}
I've just tested the following code and verified that it produces the desired output:
<?php
$items = array('haha', 'haha', 'haha', 'haha', 'haha', 'haha');
$cssClasses = array('left', 'center', 'right');
echo "<ul>\n";
$i=0;
foreach ($items as $item) {
echo "\t<li class=\"" . $cssClasses[$i++ % 3] . '">' . $item . "</li>\n";
}
echo "</ul>\n";
?>
The output is:
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>
I have this issue with OpenCart where I want to display my shop categories in a custom way and count the parent categories.
I currently modified the code so far that I get the following output
<ul id="catOpContainer">
<li id="switchCatOp1">Parent Cat 1
<ul id="catOp1">
<li>Child cat 1</li>
Child Cat 2</li>
</ul>
</li>
Parent Cat 2
<ul id="catOp1">
<li>Child cat 1</li>
Child cat 2</li>
</ul>
</li>
Parent Cat 3</li>
</ul>
</ul>
instead of the desired
<ul id="catOpContainer">
<li id="switchCatOp1">Parent Cat 1
<ul id="catOp1">
<li>Child Cat 1</li>
<li>Child Cat 2</li>
</ul>
</li>
<li id="switchCatOp2">Parent Cat 2
<ul id="catOp2">
<li>Child Cat 1</li>
<li>Child Cat 2</li>
<li>Child Cat 3</li>
</ul>
</li>
</ul>
It's obvious that there are some missing elements, but I have no clue about a possible solution. I also don't have a clue how to count the parent categories, so that I can toggle the subcategories.
I currently have the following code snippet:
<?php
class ControllerModuleCategory extends Controller {
protected $category_id = 0;
protected $path = array();
protected function index() {
$this->language->load('module/category');
$this->data['heading_title'] = $this->language->get('heading_title');
$this->load->model('catalog/category');
$this->load->model('tool/seo_url');
if (isset($this->request->get['path'])) {
$this->path = explode('_', $this->request->get['path']);
$this->category_id = end($this->path);
}
$this->data['category'] = $this->getCategories(0);
$this->id = 'category';
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
$this->template = $this->config->get('config_template') . '/template/module/category.tpl';
} else {
$this->template = 'default/template/module/category.tpl';
}
$this->render();
}
protected function getCategories($parent_id, $current_path = '') {
$category_id = array_shift($this->path);
$output = '';
$results = $this->model_catalog_category->getCategories($parent_id);
if ($results) {
if ($parent_id == 0) {
$output .= '<li id="switchCatOp1">';
} else {
$output .= '<ul id="catOp1"><li>';
}
}
foreach ($results as $result) {
if (!$current_path) {
$new_path = $result['category_id'];
} else {
$new_path = $current_path . '_' . $result['category_id'];
}
$output .= '';
$children = '';
// if ($category_id == $result['category_id']) {
$children = $this->getCategories($result['category_id'], $new_path);
// }
if ($this->category_id == $result['category_id']) {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path) . '">' . $result['name'] . '</a>';
} else {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path) . '">' . $result['name'] . '</a>';
}
$output .= $children;
$output .= '</li>';
}
if ($results) {
$output .= '</ul>';
}
return $output;
}
}
?>
I really hope someone knows a solution.
You do not need to customize controller. It has already Parent and child category listing, Just open the category module, and past below code.
<div class="box-category">
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<?php echo $category['name']; ?>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php if ($category['children']) { ?>
<ul>
<?php foreach ($category['children'] as $child) { ?>
<li>
<?php if ($child['category_id'] == $child_id) { ?>
<?php echo $child['name']; ?>
<?php } else { ?>
<?php echo $child['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>