I am trying to create a hoverable mega menu, all works fine except the active one.
I checked in source codes and active class is added, but it doesn't show up the content until mouseenter on it.
see image please :
And this is when I go on it with mouse, it show up with no problem.
I gues I need to set main (FINANCE is main menu in pic) menu active, but dont know how to pass it to submenu.
Here is my full code :
$stmt_menu = $pdo->prepare("SELECT * FROM categories WHERE parentId = ? LIMIT 5");
$stmt_menu->execute([0]);
if ($stmt_menu->rowCount() > 0) {
$query = $stmt_menu->fetchAll();
foreach ($query as $row) {
if ($row['cat_type'] == 'mega') {
?>
<li class="dropdown megamenu-fw mega-li-<?php echo $row['catId']; ?>">
<?php echo $row['catName']; ?><span class="caret"></span>
<ul class="dropdown-menu megamenu-content dropdown-top" role="menu" aria-expanded="true" data-mega-ul="<?php echo $row['catId']; ?>">
<li>
<?php
$stmt_menu = $pdo->prepare("SELECT * FROM categories WHERE parentId = ? LIMIT 5");
$stmt_menu->execute([$row['catId']]);
if ($stmt_menu->rowCount() > 0) {
$subquery = $stmt_menu->fetchAll();
?>
<div class="sub-menu-left">
<ul class="nav-sub-categories">
<?php
$i = 0;
foreach ($subquery as $subq) {
$actives = '';
if($i == 0){
$actives = ' active';
}
?>
<li data-category-filter="<?php echo $subq['cat_seo_url']; ?>-<?php echo $subq['catId']; ?>" class="li-sub-category<?php echo $actives; ?>"><?php echo $subq['catName']; ?></li>
<?php
$i++;
}
?>
</ul>
</div>
<div class="sub-menu-right">
<?php
$i = 0;
foreach ($subquery as $subcats) {
$actives = '';
if($i == 0){
$actives = ' active';
}
?>
<div class="sub-menu-inner filter-<?php echo $subcats['cat_seo_url']; ?>-<?php echo $subcats['catId']; ?><?php echo $actives; ?>">
<div class="row row-menu-right">
<div class="col-sm-3 menu-post-item">
<div class="post-item-image">
<a href="is-allowance-instantly-strangers-applauded">
<img src="assets/img/img_bg_md.png" data-src="uploads/images/202203/image_380x226_624239afd7295.jpg" alt="Is allowance instantly strangers applauded" class="lazyload img-responsive img-post" width="1" height="1"/>
</a>
</div>
<h3 class="title"><?php echo $subcats['cat_seo_url']; ?> Is allowance instantly strangers applauded</h3>
<p class="post-meta">
admin
<span>Mar 28, 2022</span>
<span><i class="icon-comment"></i>2</span>
<span class="m-r-0"><i class="icon-eye"></i>894</span>
</p>
</div>
</div>
</div>
<?php
$i++;
}
?>
</div>
<?php
}
?>
</li>
</ul>
</li>
<?php
} elseif ($row['cat_type'] == 'dropdown') {
echo ' <li class="nav-item"><a class="nav-link" href="' . $row["cat_seo_url"] . '">test</a></li>';
} else {
echo ' <li class="nav-item"><a class="nav-link" href="' . $row["cat_seo_url"] . '">' . $row["catName"] . '</a></li>';
}
}
}
I'm trying to help a friend with a website. I usually don't work with PHP, jquery.
The dropdown has 4 levels. The first level has 4 points. The 4 points have their own sub-levels, different for each other.
I'm trying to find them and then make them display in a dropdown, directly from the database.
And I'm stuck at the second level, with this error
Notice: Trying to get property 'subcategorie' of non-object in .....
What I've managed to do, until now:
<?php $categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategorie = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach($categorii as $categorie) {
?>
<ul>
<li>
<a <?php if(isset($_GET['categorie']) && $categorie->categorie==$_GET['categorie']){ echo "btn-success";}else{ echo "btn-info";};?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode($categorie->categorie); ?>"> <i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie ; ?> </a>
<ul class="">
<li> <?php echo $subcategorie->subcategorie; ?>
</li>
</ul>
</li>
</ul>
</a>
Hi You need to do a foreach loop for $subcategorieas you did for $categorii. Because you are getting an array and not a object as you got for $categorii. Here I set it around the li because it seems to be the best.
<?php
$categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategories = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach( $categorii as $categorie ) {
?>
<ul>
<li>
<a
<?php
#### What are these? Are they classnames? ####
if( isset( $_GET['categorie'] ) && $categorie->categorie==$_GET['categorie'] ){
echo "btn-success";
}else{
echo "btn-info";
};
?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode( $categorie->categorie ); ?>">
<i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie; ?>
</a>
<ul class="">
<?php foreach( $subcategories as $subcategorie ) { ?>
<li>
<a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode( $_GET['categorie'] ); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>" class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> ">
<?php echo $subcategorie->subcategorie; ?>
</a>
</li>
<?php } ?>
</ul>
</li>
</ul>
Not intended as a solution to your error " Trying to get property 'subcategorie' of non-object" ~ just to illustrate the invalid markup. If you use proper code indentation you would probably have been able to spot this!
<?php
$categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategorie = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach( $categorii as $categorie ) {
?>
<ul>
<li>
<a
<?php
#### What are these? Are they classnames? ####
if( isset( $_GET['categorie'] ) && $categorie->categorie==$_GET['categorie'] ){
echo "btn-success";
}else{
echo "btn-info";
};
?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode( $categorie->categorie ); ?>">
<i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie; ?>
</a>
<ul class="">
<!-- ### Where is the opening LI? ### -->
<a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode( $_GET['categorie'] ); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>" class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> ">
<?php echo $subcategorie->subcategorie; ?>
</a>
</li>
</ul>
</li>
</ul>
</a><!-- ### what is this closing? ### -->
Sorry if this have been asked before but I couldn't find what I wanted and I am not strong in PHP.
Right now I have this code, which is supposed to return result for different levels:
<div class="swiper-slide">
<img src="img/B1.jpg" alt="" />
<div class="content_container">
<?php
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='B1'");
while($row = mysqli_fetch_array($result))
{
?>
<h1><?php echo $row['categories']; ?></h1>
<ul class="shop_listing clearfix">
<li class="float_left"><?php echo $row['name']; ?></li>
<li class="float_right"><?php echo $row['unit_number']; ?></li>
</ul>
<?php
}
?>
</div>
</div>
<div class="swiper-slide">
<img src="img/L1.jpg" alt="" />
<div class="content_container">
<?php
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='L1'");
while($row = mysqli_fetch_array($result))
{
?>
<h1><?php echo $row['categories']; ?></h1>
<ul class="shop_listing clearfix">
<li class="float_left"><?php echo $row['name']; ?></li>
<li class="float_right"><?php echo $row['unit_number']; ?></li>
</ul>
<?php
}
?>
</div>
</div> and so on...
Right now I can only duplicate it in order to fulfil the displaying of result for each individual levels. If let's say the building have 10 levels, is there a way to simplified the coding?
Hope you guys understand. Thanks in advance! =)
Try this:
<?php
$levelArray=array('L1','B1','L2','B2');
foreach ($levelArray as $i=>$level) {
$data='';
$img = "img/".$levelArray[$i];
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$levelArray[$i]'");
while($row = mysqli_fetch_array($result)){
$data .= '<h1>'.$row['categories'].'</h1>
<ul class="shop_listing clearfix">
<li class="float_left">'.$row['name'].'</li>
<li class="float_right">'.$row['unit_number'].'</li>
</ul>';
}
echo '<div class="swiper-slide">
<img src="'.$img.'" alt="" />
<div class="content_container">'.$data.'</div>
</div>'
}
?>
My brain doesn't seem to want to work this morning.. i have the following array:
private $nav_pages = [['0', 'Home', 'home'],
['0', 'About Us', 'about'],
['0', 'Our Services', 'services'],
['1', 'PROCURE TO PAY (P2P)', 'procure'],
['1', 'ORDER TO CASH (02C)', 'cash'],
['1', 'GENERAL ACCOUNTING', 'general'],
['2', 'Charting of Accounts', 'charting'],
['2', 'General Ledger Maintenance', 'maintenance'],
['2', 'Accounts Receivable Services', 'receivable'],
['2', 'Accounts Payable Services', 'payable'],
['2', 'Reconciliation of Bank Accounts and Credit Cards', 'reconciliation'],
['2', 'Preparing Financial Statements', 'statements'],
['2', 'Payroll Processing', 'payroll'],
['1', 'CLOSING & REPORTING', 'closing'],
['0', 'How It Works', 'how-it-works'],
['0', 'Contact Us','contact'],
['0', 'Careers', 'careers']];
This is then fed to a php page which is meant to lay out a multi-layered pure css drop down menu.. the php page code is:
<ul class="<?php echo $ul_class; ?>">
<?php $this_layer = 0;
// place array content into variables
foreach ($links as $link) {
$item_layer = $link[0];
$item_string = $link[1];
$item_link = $link[2];
if ($item_layer > $this_layer) { ?>
<ul>
<?php $this_layer++; }
elseif ($item_layer == $this_layer) { ?>
<li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
</li>
<?php }
elseif ($item_layer < $this_layer) { ?>
</li></ul>
<?php $this_layer--; } ?>
<?php } ?>
The output is not correct however as the above code always closes the list item containing the second layer before the second layer is ready to be closed. if that makes sense..
<ul class="pages_nav">
<li class="home">
<a class="home"
href="/home">
Home
</a>
</li>
<li class="about">
<a class="about"
href="/about">
About Us
</a>
</li>
<li class="services">
<a class="services"
href="/services">
Our Services
</a>
</li>
<ul>
<li class="cash">
<a class="cash"
href="/cash">
ORDER TO CASH (02C)
</a>
</li>
<li class="general">
<a class="general"
href="/general">
GENERAL ACCOUNTING
</a>
</li>
<ul>
<li class="maintenance">
<a class="maintenance"
href="/maintenance">
General Ledger Maintenance
</a>
</li>
<li class="receivable">
<a class="receivable"
href="/receivable">
Accounts Receivable Services
</a>
</li>
<li class="payable">
<a class="payable"
href="/payable">
Accounts Payable Services
</a>
</li>
<li class="reconciliation">
<a class="reconciliation"
href="/reconciliation">
Reconciliation of Bank Accounts and Credit Cards
</a>
</li>
<li class="statements">
<a class="statements"
href="/statements">
Preparing Financial Statements
</a>
</li>
<li class="payroll">
<a class="payroll"
href="/payroll">
Payroll Processing
</a>
</li>
</li></ul>
</li></ul>
<li class="contact">
<a class="contact"
href="/contact">
Contact Us
</a>
</li>
<li class="careers">
<a class="careers"
href="/careers">
Careers
</a>
</li>
SOLUTION:
<ul class="<?php echo $ul_class; ?>">
<?php $this_layer = 0;
// place array content into variables
foreach ($links as $link) {
$item_layer = $link[0];
$item_string = $link[1];
$item_link = $link[2];
// check if link needs to be manipulated
switch($do) {
case 'strtolower':
$item_string = strtolower($item_string);
break;
case 'strtoupper':
$item_string = strtoupper($item_string);
break;
} ?>
<?php if ($item_layer > $this_layer) { ?>
<ul>
<li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php $this_layer++; }
elseif ($item_layer == $this_layer) { ?>
</li><li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php }
elseif ($item_layer < $this_layer) { ?>
</li></ul></li><li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php $this_layer--; } ?>
<?php } ?>
Never close the the li tag, close it when you add something
foreach ($links as $link) {
if ($item_layer > $this_layer) {
echo '<ul><li> ......';
this_layer++;
}elseif ($item_layer == $this_layer) {
echo '</li><li>......';
}elseif ($item_layer < $this_layer) {
echo '</li></ul><li>......';
$this_layer--;
}
}
Last you probably need to add a echo '</li></ul>' outside of the foreach to close everything
Had to rework the code a little, usually i organize the php menu structure in tree form makes it easier to parse into html menus, but here it is:
<ul class="<?php echo $ul_class; ?>">
<?php
$this_layer = 0;
$closeit = false;
// place array content into variables
foreach ($nav_pages as $link) {
$item_layer = $link[0];
$item_string = $link[1];
$item_link = $link[2];
if ($item_layer > $this_layer) {
// Changing level, dont close the previous li
?><ul><?php
$closeit = false;
$this_layer++;
}
if ($item_layer == $this_layer) {
// Same level, check if you need to close previous li
if ($closeit) {
?></li><?php
}
// Render the li
?>
<li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php
// Close it on next loop
$closeit = true;
}
elseif ($item_layer < $this_layer) {
// Changing layer back down, close the previous li and the current level ul
?>
</li>
</ul>
<?php
$this_layer--;
}
}
// Finished loop, there should still be a li waiting to be closed
if ($closeit) {
?></li><?php
}
// Close menu
?>
</ul>
Should be working, let me know if it isnt
Suggested php menu structure:
$menu = array(
array(
"url"=>"/place.php",
"text"=>"Go to place"
),
array(
"url"=>"/place2.php", // not necessary
"text"=>"with submenu",
"children"=>array(
array(
"url"=>"/placewithinplace2.php",
"text"=>"submenu item"
)
)
)
);
Im trying to validate my output data from a php site that Im calling with
<?php include_once('nav.inc.php');
?>
The thing is when Im using this code
<ul>
<li>GÄSTBOK</li>
<li>OM MIG</li>
<li>PORTFOLIO</li>
<li>CURRICULUM VITAE</li>
</ul>
I wont get any errors, But when Im trying with the other code I get alot of errors. Is there any way to write the code so It validates ?
This is the code thas bugging me
<?php
$index = 'menu';
$gb = 'menu';
$portfolio = 'menu';
$cv = 'menu';
$menuLinkid=basename($_SERVER['PHP_SELF'],'.php');
if($menuLinkid=='index'){
$index = 'myButtons';
}else if($menuLinkid=='gb'){
$gb ='myButtons';
}else if($menuLinkid=='portfolio'){
$portfolio ='myButtons';
}else if($menuLinkid=='cv'){
$cv ='myButtons';
}
?>
<div id="fronticon">
<a href="contact.php"><img src="images/em.png" alt="Email" title="Email"/>
</a>
</div>
<ul>
<li><a class="<?php echo $index; ?> "href="index.php">OM MIG</a></li>
<li><a class="<?php echo $gb; ?> "href="gb.php">GÄSTBOK</a></li>
<li><a class="<?php echo $portfolio;?> " href="portfolio.php">PORTFOLIO</a></li>
<li><a class="<?php echo $cv; ?> "href="cv.php">CURRICULUM VITAE</a></li>
</ul>
Thanks
You need spaces between the ?> " and the href. You can't have them next to each other with no space.
Should be:
<li><a class="<?php echo $index; ?> " href="index.php">OM MIG</a></li>