I want to make mega dropdown menu that comes from mysql database. Below is my php code. The code is working well. But the problem is I am unable to make mega dropdown menu for the code below.
I need the mega menu like the example here : https://bootsnipp.com/snippets/featured/bootstrap-mega-menu
My problem is how will I make more div with the below php code .
Plz help me the css for the mega drop down menu as shown above.
<?php
$sql = "SELECT id, product, parent_id, category_link FROM category ORDER BY parent_id, id";
$results = mysqli_query($conn,$sql) or die(mysqli_error()) ;
if($results)
{
while($result = mysqli_fetch_array($results))
{
$category['categories'][$result['id']] = $result;
$category['parent_cats'][$result['parent_id']][] = $result['id'];
}
}
function getCategories($parent, $category)
{
$html = "";
if (isset($category['parent_cats'][$parent]))
{
$html .= "<div id='wrapper'>";
$html .= "<ul class='mega-menu'>\n";
foreach ($category['parent_cats'][$parent] as $cat_id)
{
if (!isset($category['parent_cats'][$cat_id]))
{
$html .= "<li class='mega-menu-drop'>\n <a class='mega-menu-content' href='" . $category['categories'][$cat_id]['category_link'] . "'>" . $category['categories'][$cat_id]['product'] . "</a>\n</li> \n";
}
if (isset($category['parent_cats'][$cat_id]))
{
$html .= "<li class='mega-menu-drop'>\n <a class='mega-menu-content' href='" . $category['categories'][$cat_id]['category_link'] . "'>" . $category['categories'][$cat_id]['product'] . "</a> \n";
$html .= getCategories($cat_id, $category);
$html .= "</li> \n";
}
}
$html .= "</ul> \n";
$html .= "</div>";
}
return $html;
}
?>
<?php echo $data['category'] = getCategories(0, $category);?>
as of your comment. i guess you need some CSS.
this will work on mouse hover. but not on click like in your example:
.mega-menu-drop {
display:none
}
.mega-menu:hover .mega-menu-drop {
display:block
}
Related
I have nav menu in html and i want to create a loop everything is fine but the item that have children echo twice! i know where is the problem but i can't figure out how to solve it :(
my sql table named menu
and this is my php:
$db = mysqli_connect('localhost', 'root', 'password', 'aftab');
<?php
$get = mysqli_query($db , "SELECT * from menu where parent_id is NULL");
while ($rowmenu = mysqli_fetch_assoc($get)) {
echo '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-656"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;
$id = $rowmenu['id'] ;
$check = mysqli_query($db , "SELECT * from menu where parent_id = '$id'");
if ( mysqli_num_rows($check) ) {
echo '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-656"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;
echo '<ul class="sub-menu">' ;
while ( $row2 = mysqli_fetch_assoc($check) ) {
echo '<li class="menu-item-302">' . $row2['name'] . '</li>' ;
}
echo '</ul>' ;
} else {
echo '</li>' ;
}
}
?>
and this the result:enter image description here
i know it happen because the father item that hold the sub menu's called in $get once and another time when it need other css class.i tried if , foreach , while and many things.
i need that item that holds submenus should have "menu-item-has-children" class otherwise its not show the sub menus.
When a menu item, have childrens then echo menu with menu-item-has-children class, otherwise echo a simple menu and move on.
<?php
$get = mysqli_query($db , "SELECT * from menu where parent_id is NULL");
while ($rowmenu = mysqli_fetch_assoc($get)) {
$id = $rowmenu['id'] ;
$check = mysqli_query($db , "SELECT * from menu where parent_id = '$id'");
$haveSubMenu = mysqli_num_rows($check);
if($haveSubMenu)
echo '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-656 menu-item-has-children"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;
else
echo '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-656"><a href="'. $rowmenu['link'] . '" >' . $rowmenu['name'] .'</a>' ;
if ($haveSubMenu)
{
echo '<ul class="sub-menu">' ;
while ( $row2 = mysqli_fetch_assoc($check) ) {
echo '<li class="menu-item-302">' . $row2['name'] . '</li>' ;
}
echo '</ul>' ;
} else {
echo '</li>' ;
}
}
?>
Here is see that Our Products from the database is printed twice.This is because you are fetching and echoing $row2['name'] twice but you can handle the first coming array by changing your SQL query.
Change your first SQL query to
$get = mysqli_query($db , "SELECT * from menu where parent_id is NULL AND name != 'OUR PRODUCTS'");
Querying this to database you will get all the NULL values in the array excluding OUR PRODUCTS and thus it will be echoed only once by the second query.
I tried the above to answer all is working only a few level dropdown menu.
If you want to really create your menu for multi-level dropdown as like tree structure just use the bellow function. for more detailing once go through
https://bootstrapfriendly.com/blog/dynamic-multi-level-dropdown-sticky-menu-in-php-mysql-using-bootstrap/
<?php
include_once("connection.php");
$query = "SELECT id, label, link, parent FROM menus ORDER BY sort ASC, label";
$result = mysqli_query($conn, $query) or die("database error:" . mysqli_error($conn));
// Create an array to conatin a list of items and parents
$menus = array(
'items' => array(),
'parents' => array()
);
// Builds the array lists with data from the SQL result
while ($items = mysqli_fetch_assoc($result)) {
// Create current menus item id into array
$menus['items'][$items['id']] = $items;
// Creates list of all items with children
$menus['parents'][$items['parent']][] = $items['id'];
//echo $items;
}
// function to create dynamic treeview menus
function createMenu($parent, $menu)
{
$html = "";
if (isset($menu['parents'][$parent])) {
// $html .= '<ul class="sina-menu sina-menu-right" data-in="fadeInLeft" data-out="fadeInOut">';
foreach ($menu['parents'][$parent] as $itemId) {
if (!isset($menu['parents'][$itemId])) {
$html .= "<li >
<a href='" . $menu['items'][$itemId]['link'] . "'>" . $menu['items'][$itemId]['label'] . "</a>
</li>";
}
if (isset($menu['parents'][$itemId])) {
$html .= "<li class='dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' href='" . $menu['items'][$itemId]['link'] . "'>" . $menu['items'][$itemId]['label'] . "</a>";
$html .= '<ul class="dropdown-menu">';
$html .= createMenu($itemId, $menu);
$html .= '</ul>';
$html .= "</li>";
}
}
// $html .= "</ul>";
}
return $html;
}
I want to make my website menu appear with the following conditions:
If user did not login, the menu will be like this:
Gallery | Contact Us | Login
If the user logged into the website, the menu will appear like this:
Gallery | Contact Us | Logout
This is my coding to call menu in header file:
<?php
$output = '';
$menus1 = MenuPortal::model()->findAll(array('condition' => "type='atas' AND display_status='1' AND parent_id=0 ORDER BY sort ASC"));
foreach ($menus1 as $menu) {
$submenu = MenuPortal::model()->findAll(array('condition' => "parent_id=$menu->id"));
if (sizeof($submenu) > 0) {
foreach ($submenu as $smenu) {
$output .= '<li><a href=' . $menu->url . '&id=' . PortalElement::encrypt_decrypt('encrypt', $smenu->id) . '>' . $menu->title_my . '</a></li>';
break;
}
}
else {
$output .= '<li><a href=' . $menu->url . '&id=' . PortalElement::encrypt_decrypt('encrypt', $menu->id) . '>' . $menu->title_my . '</a></li>';
}
}
echo $output;
?>
What I was thinking is to check if the session is active, then show menu #2. If there is no session, then show menu #1.
After Your foreach loop just apply following condition..
if(Yii::app()->user->isGuest()){
$output. = "<li><a href='login'>Login</a></li>";
}else{
$output. = "<li><a href='login'>Logout</a></li>";
}
I am trying to create a Dropdown Menu on my site, Ive hit a brick wall and cannot think on how to do it.
Basically i need to check 2-3 variables in a database and out put the correct data.
I have at the moment it checking if its an External Link or Not, and if it contains a submenu, but i can't get it to output the correct information.
Basically i want it to check if its a External or Non-External link, and if it has a submenu, if it has a submenu, to display the menu options underneath it. So say i have menu 1, 2 ,3, 4 and 2,4 has a submenu, i need them to list the other links under them. i have put in my database toplink_id (to represent which link this item should be under) sc_order (which will control the order the sublinks display in) also dropdown (which tells me if the menu has a submenu or not.)
Here is the start of my code
$sql = "SELECT label, url, ext, dropdown FROM content_pages WHERE top_nav='1' AND active='1' ORDER by page_order ASC";
$query = mysqli_query($dbc, $sql) or die (mysqli_error($dbc));
$menuDisplay .= '<div class="bg-2"><div class="container_12"><article class="grid_12"><nav><ul class="menu sf-js-enabled">';
while ($row = mysqli_fetch_array($query)) {
$url = $row["url"];
$nav_label = $row["label"];
$drop_down ='<ul><li>' . $nav_label . '</li></ul>';
if ($row["ext"] == 0 && $row["dropdown"] == 1){
$menuDisplay .= '<li>' . $nav_label . '' . $drop_down . '</li>';
}
elseif ($row["ext"] == 1 && $row["dropdown"] == 1){
$menuDisplay .= '<li>' . $nav_label . '' . $drop_down . '</li>';
}
elseif ($row["ext"] == 0){
$menuDisplay .= '<li>' . $nav_label . '</li>';
}
elseif ($row["ext"] == 1)
{
$menuDisplay .= '<li>' . $nav_label . '</li>';
}
}
$menuDisplay .= '</ul></nav></article></div></div></header>';
mysqli_free_result($query);
Best way I found to do this without using jQuery is use multidimensional Arrays.
// Create a multidimensional array to conatin a list of items and parents
$menu = array(
'items' => array(),
'parents' => array(),
);
// Builds the array lists with data from the menu table
while ($items = mysqli_fetch_assoc($query))
{
// Creates entry into items array with current menu item id ie. $menu['items'][1]
$menu['items'][$items['id']] = $items;
// Creates entry into parents array. Parents array contains a list of all items with children
$menu['parents'][$items['parent']][] = $items['id'];
}
// Menu builder function, parentId 0 is the root
function buildMenu($parent, $menu)
{
$html = "\n";
if ( isset($menu['parents'][$parent]) )
{
$html .= "";
foreach ($menu['parents'][$parent] as $itemId)
{
if(!isset($menu['parents'][$itemId]) && $menu['items'][$itemId]['ext'] == 0)
{
$html .= "<li>\n <a href='../pages/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
}
else
if(!isset($menu['parents'][$itemId]) && $menu['items'][$itemId]['ext'] == 1)
{
$html .= "<li>\n <a href='../".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
}
if(isset($menu['parents'][$itemId]))
{
$html .= "<li>\n <a href='../pages/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."<span class='arrow-down'></span></a> \n";
$html .= "<ul style='border-radius: 0px 0px 6px 6px'> \n";
$html .= buildMenu($itemId, $menu);
$html .= "</ul> \n";
$html .= "</li> \n";
}
}
$html .= "\n";
}
$html .= "";
return $html;
}
I'm doing this PHP tutorial on OOP and I'm trying to incorporate the procedural code from the essential training into it, to see if I actually learned anything beyond the basics, and I got stuck, big time.
So, I have these two functions inside the class I named Blog:
public function navigation($selected_subject, $selected_page) {
$output = "<ul class=\"subjects\" id=\"accordion\">";
$subject_set = $this->get_all_subjects($public);
while ($subject = mysqli_fetch_array($subject_set)) {
$output .= "<li";
if ($subject["id"] == $selected_subject['id']) { $output .= " class=\"blogselected\""; }
$output .= ">{$subject["menu_name"]}</li>";
$page_set = $this->get_pages_for_subject($subject["id"], $public);
$output .= "<ul class=\"pages\">";
while ($page = mysqli_fetch_array($page_set)) {
$output .= "<li";
if ($page['id'] == $selected_page['id'] ) { $output .= " class=\"blogselected\""; }
$output .= "><a href=\"../public/blog_index.php?page=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
$output .= "</ul>";
}
$output .= "</ul>";
return $output;
}
public function blog_page($selected_page){
if ($selected_page) {
echo "<h2>" . $selected_page['menu_name'] . "</h2>";
echo "<div class=\"page-content\">";
echo $selected_page['content'];
echo "</div>";
} else {
echo "<h2>Welcome to Widget Corp</h2>";
}
}
and this piece of code on the index page:
<?php
$blog = new Blog;
global $selected_page;
global $selected_subject;
?>
<div class="blog">
<div class="blogpost">
<?php echo $blog->blog_page($selected_page); ?>
</div>
<div class="blognav">
<?php echo $blog->navigation($selected_subject, $selected_page); ?>
</div>
</div>
The code partially works. The navigation with subjects and sub-pages gets displayed fine, when the links are clicked the id "?page=1" is passed to the url, but the problem is that the code is not adding a class "blogselected" to the active links, and it also does not display the content when the links are clicked. The only thing that gets displayed in the content area is the h2 "Welcome to Widget Corp" from else condition in the second function, so I believe the variables $selected_subject and $selected_page might be causing the problem, but I just can't figure it out on my own. I spent countless hours trying to solve this and I'm on the brink of giving up completely. What exactly am I missing here?
I have a link that is basically an 'about' page that has social icons pointing to my personal social media outlets. In PHP, the code is like this:
function wp_about_author_get_socials() {
$socials = array();
$socials['twitter'] = array('title'=>'Twitter', 'link'=>'http://www.twitter.com
/%%username%%', 'icon'=> WPAUTHORURL_URL .'/images/twitter.png');
$socials['facebook'] = array('title'=>'Facebook', 'link'=>'http://www.facebook.com
/%%username%%', 'icon'=> WPAUTHORURL_URL .'/images/facebook.png');
I've fiddled around with things, but just can't seem to implement things so the link opens up in a new tab/page. Currently, the links take the user off the website (not ideal). Any guidance in this regard would be greatly appreciated!
What I have tried was changing the links to this:
$socials['twitter'] = array('title'=>'Twitter', 'link'=>'http://www.twitter.com
/%%username%%', 'target'=>'_blank', 'icon'=> WPAUTHORURL_URL .'/images/twitter.png');
But that didn't work.
This may be the part that prints the links, from what I can tell:
// About Author Social
$wp_about_author_social .= wp_about_author_get_social_links($wp_about_author_settings);
if(isset($wp_about_author_settings['wp_author_social_images']) &&
$wp_about_author_settings['wp_author_social_images']){
$wp_about_author_content .= "<p>" .$wp_about_author_links . "</p>";
if($wp_about_author_social != ""){
$wp_about_author_content .= '<p class="wpa-nomargin">'.apply_filters(
'wp_about_author_follow_me', "Follow Me:").'<br />' . $wp_about_author_social.'</p>';
}
} else {
$wp_about_author_content .= "<p class='wpa-nomargin'>";
$wp_about_author_content .= $wp_about_author_links;
if($wp_about_author_social != ""){
$wp_about_author_content .= apply_filters( 'wp_about_author_separator', " - ") .
$wp_about_author_social;
}
$wp_about_author_content .= "</p>";
}
// Generate social icons
function wp_about_author_get_social_links($wp_about_author_settings){
$content="";
$socials = wp_about_author_get_socials();
foreach($socials as $social_key=>$social){
if (get_the_author_meta($social_key)){
if(isset($wp_about_author_settings['wp_author_social_images']) &&
$wp_about_author_settings['wp_author_social_images']){
$content .= "<a class='wpa-social-icons' href='".str_replace('%%username%%',
get_the_author_meta($social_key), $social['link'])."'><img src='". $social['icon']."'
alt='".$social['title']."'/></a>";
} else {
if($content != "")
$content .= apply_filters( 'wp_about_author_separator', " - ");
$content .= "<a href='".str_replace('%%username%%', get_the_author_meta($social_key),
$social['link'])."'>".$social['title']."</a>";
}
}
}
return $content;
}
Within the code you posted, you can add target="_blank" to the links on lines 28 and 34.
Line 28 becomes:
$content .= "<a class='wpa-social-icons' target='_blank' href='".str_replace('%%username%%',
Line 34 becomes:
$content .= "<a target='_blank' href='".str_replace('%%username%%', get_the_author_meta($social_key),