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>';
}
}
}
?>
Related
I want print wordpress nav menu in custom html .
This function print my favorite out put , but the problem is printing the additional tags in output
I don't want use wp_nav_menu function
my favorite output is like :
<div class="menu">
<ul>
<li>
<a href="" ></a>
<div class="menu">
<ul>
.....
</ul>
</div>
</li>
</ul>
</div>
and my function is :
function sh_mobileMenuRender($menu_id)
{
$menu_items = wp_get_nav_menu_items($menu_id);
$html = '<div class="position-fixed mobileMenu-continer"
id="mobileMenu-continer" data-open="' . $opening_from . '"><div id="mobileMenu" class="menu"><ul>';
$is_frist_menu = true;
//این آرایه مقدار تورفتگی ها را محاسبه میکند
$y = array();
for ($x = 0; $x < $c; $x++) {
if ($menu_items[$x]->menu_item_parent == 0) {
//تو رفتگی رو صفر کن
$y[$x] = 0;
if ($is_frist_menu) {
$html .= '<li>بستن<i aria-hidden="true" class="fas fa-arrow-right"></i></li>';
$html .= '<li><a href="#">' . $menu_items[$x]->title;
$is_frist_menu = false;
} else {
$html .= '</a>';
if ($y[$x] < $y[$x - 1]) {
$dif = $y[$x - 1] - $y[$x];
for ($v = 1; $v <= $dif; $v++) {
$html .= '</li></ul></div>';
}
}
$html .= '</li><li><a href="#">' . $menu_items[$x]->title;
}
continue;
}
//find parent menu form privous menu
for ($b = $x - 1; $b >= 0; $b--) {
if ($menu_items[$x]->menu_item_parent == $menu_items[$b]->ID) {
//محاسبه تورفتگی
$y[$x] = $y[$b] + 1;
if ($b == $x - 1) {
$html .= '<i aria-hidden="true" class="fas fa-arrow-left"></i></a>';
$html .= '<div class="menu"><ul class="submenu">';
$html .= '<li>بازگشت<i aria-hidden="true" class="fas fa-arrow-right"></i></li>';
$html .= '<li><a href="#">' . $menu_items[$x]->title;
} else {
$html .= '</a>';
if ($y[$x] < $y[$x - 1]) {
$dif = $y[$x - 1] - $y[$x];
for ($v = 1; $v <= $dif; $v++) {
$html .= '</li></ul></div>';
}
}
$html .= '</li><li><a href="#">' . $menu_items[$x]->title;
}
break;
}
}
}
$html .= '</ul></div></div>';
echo $html;
}
I want print wordpress nav menu in custom html .
This function print my favorate out put , but the problem is printing the additional tags in output
I don't want use wp_nav_menu function
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>';
I want to display <li class = "active"> in my current URL when I click any menu.
Here is what I have tried:
<?php
$uri_string = $this->uri->uri_string();
$id_user = $usr->id;
$get_group = $this->db->get_where('users_groups', array('user_id'=> $id_user));
$hasil = $get_group->result();
foreach($hasil as $h)
if(isset($h->group_id)){
$in_group = $this->ion_auth->in_group($h->group_id);
if(isset($in_group)){
$get_menu = $this->db->get_where('menu',array('parent_menu' => 0, 'menu_users_groups' => $h->group_id));
$menu = $get_menu->result();
foreach($menu as $m){
$cekSub = $this->db->get_where('menu',array('parent_menu' => $m->id));
if($cekSub->num_rows() > 0){
echo '<li>';
echo '
<a href="javascript:void(0);" class="menu-toggle">
<i class="material-icons">'.$m->icon.'</i> <span>'.$m->menu_name.'</span>
</a>
<ul class="ml-menu">
<li>';
foreach($cekSub->result() as $c)
echo anchor(''.$c->controller_link.'','<i class="material-icons">'.$c->icon.'</i><span> '.$c->menu_name.'</span>');
echo '</li>
</ul>
</li>';
} else {
echo '<li>';
echo anchor(''.$m->controller_link.'','<i class="material-icons">'.$m->icon.'</i><span> '.$m->menu_name.'</span>');
echo '</li>';
}
}
}
}
?>
The problem is, that if not in the curent URL class, active is hidden - just <li> is displayed.
How to fix this?
it's solved with :
<?php
$getUri = $this->uri->uri_string();
$id_user = $usr->id;
$get_group = $this->db->get_where('users_groups', array('user_id'=> $id_user));
$hasil = $get_group->result();
foreach($hasil as $h)
if(isset($h->group_id)){
$in_group = $this->ion_auth->in_group($h->group_id);
if(isset($in_group)){
$get_menu = $this->db->get_where('menu',array('parent_menu' => 0, 'menu_users_groups' => $h->group_id));
$menu = $get_menu->result();
foreach($menu as $m){
$cekSub = $this->db->get_where('menu',array('parent_menu' => $m->id));
$getSub = $cekSub->result();
if($cekSub->num_rows() > 0){
echo '<li';
foreach($getSub as $c)
if($c->controller_link==$getUri){echo ' class="active"';}
echo '>
<a href="javascript:void(0);" class="menu-toggle">
<i class="material-icons">'.$m->icon.'</i> <span>'.$m->menu_name.'</span>
</a>
<ul class="ml-menu">
<li>';
foreach($getSub as $c)
echo anchor(''.$c->controller_link.'','<i class="material-icons">'.$c->icon.'</i><span> '.$c->menu_name.'</span>');
echo '</li>
</ul>
</li>';
} else {
echo '<li';
if($m->controller_link==$getUri){echo ' class="active"';}
echo '>';
echo anchor(''.$m->controller_link.'','<i class="material-icons">'.$m->icon.'</i><span> '.$m->menu_name.'</span>');
echo '</li>';
}
}
}
}
?>
I have a banner that used from bootstrap and, the first slider on this banner should have class='item active' the rest of the sliders should have class='item' I am getting my sliders from my database
so far that what I try to do.
<?php
$getBanner = $db->prepare("SELECT * FROM banner_english");
if ($getBanner->execute()) {
$results = $getBanner->get_result();
while ($b = $results->fetch_array()) {
$bannerImages = array($b['image']);
foreach ($bannerImages as $image) {
if ($image[0]) {
echo '<div class="item active">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
} else {
echo '<div class="item">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
var_dump($bannerImages);
}
}
}
}
?>
still not working as expected
var_dump
array (size=1)
0 => string '06.jpg' (length=6)
array (size=1)
0 => string '03.jpg' (length=6)
I see that the var_dump is 0 for all items what did I do wrong here?
This is a fix to the old code:
<?php
$getBanner = $db->prepare("SELECT image FROM banner_english");
if ($getBanner->execute()) {
$results = $getBanner->get_result();
$is_first = true;
while ($b = $results->fetch_array()) {
if ($is_first) {
echo '<div class="item active">
<img src="../images/en_banner/' . $b[0] . '" alt="Koueider">
</div>';
$is_first = false;
} else {
echo '<div class="item">
<img src="../images/en_banner/' . $b[0] . '" alt="Koueider">
</div>';
var_dump($bannerImages);
}
}
}
?>
Change the code like this:
<?php
$getBanner = $db->prepare("SELECT * FROM banner_english");
if ($getBanner->execute()) {
$results = $getBanner->get_result();
$rows = $results->fetch_all(MYSQLI_ASSOC);
$ind = 0;
foreach ($rows as $image) {
if ($ind == 0) {
echo '<div class="item active">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
$ind++;
} else {
echo '<div class="item">
<img src="../images/en_banner/' . $image . '" alt="Koueider">
</div>';
var_dump($bannerImages);
}
}
}
?>
I want to detect virtuemart category page in mod_breadcrumbs.
Breadcrumbs is loading all page and i just wanted write a message in category page.
My breadcrumbs code:
<div class="breadcrumbs<?php echo $moduleclass_sfx; ?>">
<?php
echo '<ul>';
for ($i = 0; $i < $count; $i ++) {
// If not the last item in the breadcrumbs add the separator
if ($i < $count -1) {
if (!empty($list[$i]->link)) echo '<li>'.$list[$i]->name.'</li>';
else echo '<li class="pathway">' . $list[$i]->name . '</li>';
if($i < $count -2) echo ' <li class="pathway separator">></li> ';
} else if ($params->get('showLast', 1)) { // when $i == $count -1 and 'showLast' is true
if($i > 0) echo ' <li class="pathway separator">></li> ';
echo '<li class="pathway">' . $list[$i]->name . '</li>';
}
}
echo '</ul>';
?>
</div>
Here's how you can check if you're on a category view:
$appInput = Jfactory::getApplication()->input;
if($appInput->getCmd('option')=='com_content' && $appInput->getCmd('view')=='category' ){
//add your code here
}