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>';
Related
I am display data from the database. Currently, I have 6 records in my database and I am getting my output like
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>
Now what I am doing is, I have to close the </ul> tag after 4th li tag and then start new ul after 4th li.
My expected output is
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
</ul>
<ul>
<li>Records5</li>
<li>Records6</li>
</ul>
is it possible?
I am using below code
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check=0;
$first4=0;
while ( $tyler_query->have_posts() ) {
$tyler_query->the_post();
if ($index < 4) {
if ($first4==0){?>
<ul>
<?php $first4=1;}?>
<li>
<!--output here-->
</li>
<?php if ($first4==4){?>
</ul>
<?php }?>
<?php }
else {
if ($check==0){?>
<ul>
<?php $check=1;}?>
<li>
<!--output here-->
</li>
<?php } $index++;}?>
</ul>
<?php }?>
You can just insert a closing tag followed by an opening tag, whenever it meets your condition. In the following after every third item:
<?php
$items = [
'Syd',
'Roger',
'Nick',
'David',
'Richard'
];
$i = 0;
echo '<ul>';
foreach($items as $item) {
if($i++%3 == 0)
echo '</ul><ul>';
echo '<li>' . $item . '</li>';
}
echo '</ul>';
Output:
<ul><li>Syd</li><li>Roger</li><li>Nick</li></ul><ul><li>David</li><li>Richard</li></ul>
It's quick example. Hope help you.
if ($tyler_query->have_posts()) {
$index = 0;
$check = 6;
?>
<ul>
<?php while ($tyler_query->have_posts()) {
$tyler_query->the_post(); ?>
<li><?php echo 'some_value' ?></li>
<?php if ($index % $check === 0 ) { ?>
</ul><ul>
<?php }
$index++;
} ?>
</ul>
<?php } ?>
your code would work if you echo the HTML tag/output you want to see in the browser.
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check = 0;
$first4 = 0;
while ($tyler_query->have_posts()) {
$tyler_query->the_post();
$output = "whatever the output object is";
if ($index < 4) {
if ($first4 == 0) {
echo '<ul>';
$first4 = 1; // increment so that the this if block wont trigger again
}
echo '<li>' . $output . '</li>';
// increment so that the next if block trigger once
if ($first4 == 4) {
echo '</ul>';
}
$first4++;
}
if ($index >= 4){
if ($check == 0) {
echo '<ul>';
$check = 1;
}
// assuming you want to have the rest of the data in this block.
// data 5 and above
else {
echo '<li>' . $output . '</li>';
}
}
$index++;
}
echo '</ul>';
}
?>
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>
I want to remove last separator after Login Menu.
Here is my code and output.
<?php
if (!empty($topmenu) && !empty($menulist)) {
foreach ($topmenu as $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
echo '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span>';
echo '</li>';
echo '<li> | </li>';
} else {
echo '<li>' . $arry['name'] . '</li>';
echo '<li> | </li>';
}
}
}
?>
Result of this code is
Home | Register | Login |
I want to Remove last Separator after Login menu.
I want result like this.
Home | Register | Login
Try This
<?php
if (!empty($topmenu) && !empty($menulist)) {
$count = count($topmenu);
$i = 1;
foreach ($topmenu as $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
echo '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span>';
echo '</li>';
if($count != $i)
echo '<li> | </li>';
} else {
echo '<li>' . $arry['name'] . '</li>';
if($count != $i)
echo '<li> | </li>';
}
$i++;
}
}
?>
You can follow this example:
<?
$array = array('One','Two','Three'); // your array
$count = count($array); // check the array count
$i = 1; // use incremental
foreach ($array as $value) {
$separator = ($i == $count ? '' : '|'); // compare if last index use empty else separator
echo $value. $separator; // print separator with value
$i++; // +1 in every iteration.
}
?>
Result:
One|Two|Three
UPDATE 1:
Example with your code
<?
if (!empty($topmenu) && !empty($menulist)) {
$count = count($topmenu);
$i = 1;
foreach ($topmenu as $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
echo '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span>';
echo '</li>';
} else {
echo '<li>' . $arry['name'] . '</li>';
//echo '<li> | </li>';
}
if($i == $count ? '' : '<li> | </li>');
$i++;
}
}
?>
Side Note:
I am not sure about the $topmenu check count($topmenu); if you get the count it will work. Try it.
You can collect your list items and concat them using implode method:
<?php
if (!empty($topmenu) && !empty($menulist)) {
$listItems = array();
foreach ($topmenu as $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
$listItems[] = '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span></li>';
} else {
$listItems[] = '<li>' . $arry['name'] . '</li>';
}
}
echo implode('<li> | </li>', $listItems);
}
?>
You can do something like this
<?php
if (!empty($topmenu) && !empty($menulist)) {
foreach ($topmenu as $key => $mainparent) {
$arry = getmenuvalue($mainparent->id, $menulist, MAINURL);
if (isset($mainparent->children) && !empty($mainparent->children)) {
echo '<li class="dropdown"> ' . $arry['name'] . '<span class="caret"> </span>';
echo '</li>';
//echo '<li> | </li>';
} else {
echo '<li>' . $arry['name'] . '</li>';
//echo '<li> | </li>';
}
end($topmenu);
if ($key !== key($topmenu)) {
echo '<li> | </li>';
}
}
}
?>
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>