i have a folder 'server' and inside of server i have anothers folders 'computer1' and 'computer2' and inside of computer1 i have more folder and inside of computer2 i have more folders too
So i have a sidebar and until now i put this to show the computer1 and computer2
<ul class="nav side-menu">
<?php
foreach (glob('server/sandro/*', GLOB_ONLYDIR)as$subfolder) {
echo '<li><a><i class="fa fa-home"></i>'. basename($subfolder) .'<span class="fa fa-chevron-down"></span></a>';
echo '<ul class="nav child_menu">';
echo '</ul>';
echo '</li>';
}
?>
</ul>
and well..it's result, but now i want to add inside of
echo '<ul class="nav child_menu">';
echo '</ul>';
the rest of subfolders that are inside of the computer1 and computer2
please, help-me.
Hi you have to use function and call function in itself
<ul class="nav side-menu">
<?php
function foldersList($folderName = NULL) {
$return = '';
foreach (glob('./server/sandro/' . $folderName . '*', GLOB_ONLYDIR) as $subfolder) {
// call function to check subfolders - don't forget write `/`
$subFolders = foldersList(basename($subfolder). '/');
$return .= '<li><a><i class="fa fa-home"></i>' . basename($subfolder) . '<span class="fa fa-chevron-down"></span></a>';
$return .= '<ul class="nav child_menu">';
// if subfolder exist add to return variable
$return .= $subFolders != '' ? $subFolders : '';
$return .= '</ul>';
$return .= '</li>';
}
return $return;
}
echo foldersList();
?>
</ul>
Related
I use these few lines of code to list folders and subfolders. It works pretty well but I don't understand why I end up with :
<ul></ul>.
This is my PHP code :
<?php
function listFolder($dir){
$files = preg_grep('/^([^.])/', scandir($dir));
// prevent empty ordered elements
if (count($files) < 1)
return;
echo '<ul>';
foreach($files as $file){
if(is_dir($dir.'/'.$file)) {
echo '<li><a href="view.php?m='. $dir.'/'.$file.'" class="folder">'.$file;
echo '</a>';
listFolder($dir.'/'.$file);
echo '</li>';
}
}
echo '</ul>';
}
echo '<ul class="menu">';
listFolder('mails');
echo '</ul>';
?>
and the HTML result
<ul class="menu">
<ul>
<li>
2020
<ul>
<li>
Janvier
<ul>
<li>semaine1</li>
<li>semaine2</li>
<li>semaine3</li>
</ul>
</li>
<li>
Fevrier
<ul></ul>
</li>
<li>
Mars
<ul></ul>
</li>
<li>
Avril
<ul></ul>
</li>
<li>
Mai
<ul></ul>
</li>
</ul>
</li>
</ul>
</ul>
If anyone has any idea what the problem is...
Thank you very much for the help you can give me.
The location of ul is incorrect, see below
function listFolder($location)
{
if (is_file($location)) {
echo '<li> ' . basename($location) . '</li>';
return;
}
if (!is_dir($location)) {
return;
}
$dirs = scandir($location);
foreach ($dirs as $dir) {
if ($dir == '.' || $dir == '..') {
continue;
}
$path = $location . '/' . $dir;
if (is_file($path)) {
echo '<li> ' . $dir . '</li>';
} else if (is_dir($path)) {
echo '<li><ul>';
listFolder($path);
echo '</ul></li>';
}
}
}
and to call
echo '<ul class="menu">';
listFolder('/your/path');
echo '</ul>';
Edit: as per your comment if you only wants to list folders, I would go with glob method, make sure to provide path without a slash
function listFolder($dir, $parent){
// $files = preg_grep('/^([^.])/', scandir($dir));
$files = glob($dir.'/*', GLOB_ONLYDIR);
if (count($files) < 1)
return;
if(!$parent) echo '<ul>';
foreach($files as $file){
if ($file == '.' || $file == '..') {
continue;
}
if(is_dir($file)) {
echo '<li><a href="view.php?m='.$file.'" class="folder">'.basename($file);
echo '</a>';
listFolder($file,false);
echo '</li>';
}
}
if(!$parent) echo '</ul>';
}
which you need to call
echo '<ul class="menu">';
listFolder('/your/location',true);
echo '</ul>';
My PHP code gets ignored or I get unexpected error on the page. I just can't find the way to include a line of PHP between HTML code and then assign that HTML code to the variable later on so I could return it in my custom made taxonomy. I know this is a noob question, but other examples was not working for me neither. I'm trying to assign the HTML with some PHP code to $out variable. Here is my code:
add_shortcode('Use_Case', 'use_case_shortcode_query');
function use_case_shortcode_query($atts, $content){
global $use_case_categories;
extract(shortcode_atts(array( // a few default values
'posts_per_page' => '11',
'post_type' => 'use_cases',
)
, $atts));
global $post;
$posts = new WP_Query(array( // a few default values
'posts_per_page' => '11',
'post_type' => 'use_cases'
));
//print_r($posts);
$output = '';
$out = '';
$out .= '<ul class="nav nav-tabs nav-justified">
<li class="active">
<a data-toggle="tab" href="#all">All</a>
</li>
<?php foreach($use_case_categories as $use_case_category) { ?>
<li>
<?php return $use_case_category->name ?>
</li>
<?php } ?>
</ul>';
?>
Be careful with your php open end close tags. To make it more readable and easier to check, you can do it this way :
$out .= '<ul class="nav nav-tabs nav-justified">';
$out .= '<li class="active">';
$out .= '<a data-toggle="tab" href="#all">All</a>';
$out .= '</li>';
foreach($use_case_categories as $use_case_category):
$out .= '<li>';
$out .= '' . $use_case_category->name . '';
$out .= '</li>';
endforeach;
$out .= '</ul>';
And if you want to compact it :
$out .= '<ul class="nav nav-tabs nav-justified"><li class="active"><a data-toggle="tab" href="#all">All</a></li>';
foreach($use_case_categories as $use_case_category):
$out .= '<li>' . $use_case_category->name . '</li>';
endforeach;
$out .= '</ul>';
im generating a bootstrap navbar using codeigniter, its working fine but i want the admin link to be aligned to the right. here is my code:
function get_menu ($array, $child = FALSE)
{
$CI =& get_instance();
$str = '';
if (count($array)) {
$str .= $child == FALSE ? '<ul class="nav navbar-left navbar-custom">' . PHP_EOL : '</ul>' . PHP_EOL;
foreach ($array as $item) {
$active = $CI->uri->segment(1) == $item['slug'] ? TRUE : FALSE;
if (isset($item['children']) && count($item['children'])) {
$str .= $active ? '<li class="dropdown active">' : '<li class="dropdown">';
$str .= '<a class="dropdown-toggle" data-toggle="dropdown" href="' . site_url(e($item['slug'])) . '">' . e($item['title']);
$str .= '<b class="caret"></b></a>' . PHP_EOL;
$str .= get_menu($item['children'], TRUE);
}
else {
$str .= $active ? '<li class="active">' : '<li>';
$str .= '' . e($item['title']) . '';
}
// Closing tags
$str .= '</li>' ;
}
//add dashboard link to the right of menu for login
$str .= '<li>' . '<p class="navbar-text navbar-right"><a
`href="admin/dashboard" class="navbar-link"> ADMIN</a></p></li>' . PHP_EOL;
$str .= '</ul>' . PHP_EOL;
}
im getting a navbar with required links but its all inline. any help is aappreciated. Cheers
This is because of where you are adding the navbar-right class. You have to add it to an ul element. Change your second to last string:
$str .= '<li>' . '<p class="navbar-text navbar-right"><a `href="admin/dashboard" class="navbar-link"> ADMIN</a></p></li>' . PHP_EOL;
To something along these lines:
<ul class="nav navbar-nav navbar-right">
<li> ADMIN</li>
</ul>
And do not add it inside of your current ul element, that will also not work; you need to close the navbar-left class then add the navbar-right.
Looking something like this:
UPDATE
I think I got it, you do not have the nav element around your navbar, and that is probably what is causing the issues with alignments...
<?php
function get_menu ($array, $child = FALSE)
{
$CI =& get_instance();
$str = '';
if (count($array)) {
$str .= $child == FALSE ? '<nav class="navbar navbar-default"> <ul class="nav navbar-nav navbar-custom">' . PHP_EOL; // create the <nav> element
foreach ($array as $item) {
$active = $CI->uri->segment(1) == $item['slug'] ? TRUE : FALSE;
if (isset($item['children']) && count($item['children'])) {
$str .= $active ? '<li class="dropdown active">' : '<li class="dropdown">';
$str .= '<a class="dropdown-toggle" data-toggle="dropdown" href="' . site_url(e($item['slug'])) . '">' . e($item['title']);
$str .= '<b class="caret"></b></a>' . PHP_EOL;
$str .= get_menu($item['children'], TRUE);
}
else {
$str .= $active ? '<li class="active">' : '<li>';
$str .= '' . e($item['title']) . '';
}
// Closing tags
$str .= '</li>' ;
}
$str .= '</ul>' . PHP_EOL;
//add dashboard link to the right of menu for login
$str .= '<ul class="nav navbar-nav navbar-right">
<li> ADMIN</li>
</ul>' . PHP_EOL;
// add the nav closing tag
$str .= '</nav>' . PHP_EOL;
}
?>
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 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>