Create menus in php

How to create Dynamic menu in php and mysql

So this shows how you can easily create dynimic dropdowns without a database and even without server side code (PHP). Now, that said, I’m not saying you are better off without a database, just that it’s not strictly needed for dynamic dropdowns.

How to create Dynamic menu in php and mysql

I have created a menu in php where category is one of those options but i want such that when i click on category related products should get open from database. If somebody have idea please share.

You can try this. No need to do ajax.

In your css hide the inner ul by default and on hover of outer ul > li > a just show the inner ul. By this you will be able to display related products to that category.

Use this in html

* < margin:0; padding:0; >#nav < list-style:none; height:2em; >#nav li < position:relative; float:left; width:192px; background:#999; text-align:center; >#nav li:hover < background:#777; >#nav a < display:block; color:#000; text-decoration:none; line-height:2em; >/* --------- Drop Down -------- */ #nav ul < position:absolute; left:-999em; top:2em; list-style:none; >#nav li:hover ul

Here is a JSfiddle which you can refer.

Читайте также:  Wordpress css and javascript

How dynamic menus are managed in quickadmin Code, PHP queries related to “how dynamic menus are managed in quickadmin” laravel is active route; how dynamic menus are managed in quickadmin; laravel add active class to menu; active route get in laravel; laravel set active route angular; route active class laravel * laravel 5.8 active link; laravel route active class

Dynamic Menu In PHP & MySQL: Grouping Menu

In this playlist you will learn how to create dynamic menu or dynamic navigation menu in PHP programming and MySQL. This tutorial is very important …

PHP and MySQL — How To Create A Dynamic Menu (PHP

NOTICE: I used multiple files for organisational purposes, it’s very useful when working on a big project

Simple Dynamic Menu With PHP MYSQL

This short tutorial will walk through how to create a simple dynamic navigation menu with PHP MYSQL.— CODE BOXX EBOOK STORE — …

How can I create dynamic menus and pages driven from database?

I am a newbie here and for php too. I am not new to stackoverflow though 🙂

I am creating a simple reporting system and I want the menues and pages to be generated from the database.

I saw this video on YouTube and managed to create a menu with the following code.

I have a database table called Reports and columns called rep_id , rep_date , rep_ledit_date , rep_by , department , position , report , and rep_to .

So based on the above method, I created a menu using this code.

So I created a page called reports.php to see the details of the content in the database. What I wanted was to see the following rep_by (rep_date) as a heading and report as a content.

I might want to use other columns in the content too. So what kind of code the menu and reports.php should have to achieve what I want. What I did was the following and it only outputs the first row when all the menu links are clicked.

 require ("includes/db.php"); mysqli_select_db($con, '$db_name'); $sql= "SELECT * FROM reports"; $result = mysqli_query($con, $sql) or die (mysqli_error($con)); $represult=mysqli_fetch_assoc($result);

prepare($sql); $id = $_GET[‘rep_id’]; $stmt->bind_param(‘i’, $id); $stmt->execute(); $stmt->bind_result($rep_id, $rep_date, $rep_ledit_date, $rep_by, $department, $position, $report, $rep_to); $stmt->fetch(); ?>

Your code is a bit confusing due to the mix of PHP tags and HTML so I have reworked it as 100% PHP like this.

The main problem I see in your original code is you are doing this call twice:

$represult=mysqli_fetch_assoc($result); 

One right near $result = mysqli_query(…) and the other as the tail end of the do / while loop. Also, unsure of the value of that do / while loop so I refactored it as a simple while loop. And finally with the restructuring to be 100% PHP I did some formatted concatenation to make it easier to read & see the structure of your overall logic.

EDIT: The original poster added additional code saying the problem is it was only returning the first result. The issue—again—is the lack of a while loop. Here is my refactored version of that code:

require ("includes/db.php"); mysqli_select_db($con, '$db_name'); $sql= "SELECT * FROM reports"; $result = mysqli_query($con, $sql) or die (mysqli_error($con)); while ($represult = mysqli_fetch_assoc($result)) < echo '

' . $represult['rep_by'] . ' (' . $represult['rep_date'] . ')' . '

' . $represult['report'] ; >

How to create Dynamic menu in php and mysql, how to create Dynamic menu in php and mysql. Ask Question Asked 4 years, 5 months ago. Modified 4 years, 5 months ago. I have created a menu …

Dynamic Dropdown Menus

Is it compulsory that all dynamic dropdown menus must be called from the database? I want to include one for my latest project, although I dont know how to do it, but after searching from Google, I found that almost everyone was talking about MySQL. I was thinking would be enough.

Well, there are obviously alternatives to this, you can use PHP to read a file (XML is popular for configuration files) and then use the data to generate the menu or just generate the dropdown menu based on an array and so on. What you should do obviously depends on what you are working on. Basically the point is that you have data stored somewhere (an array, file, database. ) and then you retrieve it and build the menu.

EDIT: To specify, I’m not saying you should use XML file etc. for dynamic content.

A database is definitely useful, but not strictly required.

I have a jQuery javascript I often use for dynamic drop-downs. It updates the drop-downs from a javascript variable (json object). Most often I generate this from a database, but it could be hard coded if the site doesn’t use a database.

Edit (adding explaination)

I’ve updated the sample code to be somewhat more related to your application (only knowing it relates to classes and sessions. The example has dynamic dropdowns as follows:

  • Select course
  • Select the semester
    • Depending on the course some course may have multiple options or only run in one particular semester. Also a ‘full year project’ for which semester is not applicable.
    • This may only be applicable to some courses

    (A few levels of dynamic, just to cover the script capabilities)

    Now the implementation. We include a script at the top of the page (+ jQuery, which the script depends on). Then we have the HTML, we only need empty select fields with names and ids. The dynamic options are handled by the javascript.

    The onload is the only javascript that needs to be modified for the application. We have an array of ‘option’ objects with their id, parent_id, and display.

    And the JQuery onload function adds the options into the dropdown and if course changes then semester dropdown should update, and if semester dropdown then time dropdown is updated.

    The scripts has one additional property in that it hides the field and it’s label if there are no related options for a given selection.

    A complete html page combining these parts can be viewed here: http://snipt.org/Uul0 (Just sav to an html file to demo it)

    So this shows how you can easily create dynimic dropdowns without a database and even without server side code (PHP). This is purely JQuery and HTML). Now, that said, I’m not saying you are better off without a database, just that it’s not strictly needed for dynamic dropdowns.

    Sorry this is already a long essay. That’s all I have to offer here.

    The included script

    function loadOptions(jquery_identifier, options, parent_id) < var $select = jQuery(jquery_identifier), i, option; $select.children().remove(); if (typeof(options)=='object'&&(options instanceof Array)) < var toAppend = []; var toAppendIndex = 0; for (i = 0; i < options.length; i++) < option = options[i]; if (option.parent_id == parent_id) < // repeatedly appending to select was too slow, instead // appending to array and appending to select once at the end using toAppend.join toAppend[toAppendIndex++] = ""; > > if (toAppendIndex > 0) < $select.append("").append(toAppend.join('')); $select.parent('.field').show(); > else < $select.parent('.field').hide(); >> $select.change(); > 

    Sample usage

    Course Semester Time  The onload 
    var dynamic_options = [ , , , , , , , , ]; jQuery(function()< // initialise course dropdown with the choices with no parent (parent_id = 0) loadOptions('#course', dynamic_options, 0); // if course changes update semester dropdown with the appropriate child options jQuery('#course').change(function() < loadOptions('#semester', dynamic_options, jQuery(this).val()); >).change(); // if level 2 changes update level 3 with the appropriate child options jQuery('#semester').change(function() < loadOptions('#time', dynamic_options, jQuery(this).val()); >).change(); >); 

    Something interesting to take a look is Google Fusion Tables.

    Anyway, at some point you’ll have to rely on a DB.

    MySQL is a good choice for many PHP applications, because they work well together. Also, extremely many hosting providers host MySQL. Add to that you own Google experience and you can see why many people use it. Plus, MySQL is a very good database system, which is free to boot.

    However, you can build your drop down menus any way you want. However, sooner or later you’ll want to have some way of storing all your site’s data, and then turn those into drop down items somehow, and using a database system is a good way to go. The reason is that a database handles annoying file opening and searching code for you, so you can focus on your data itself.

    EDIT: a good database system will make your data juggling life very easy. It’s easy to get all the people in the class with an average grade between a B and a C with a single statement. Or one statement to find out whether girls really are smarter than boys, based on their grade. Or to get a list of the number of students by age, by grade, or both. In pure PHP this takes a relatively large amount of code, compared to the single query that MySQL needs.

    Dynamic navigation in php, i agree with the first solution even if its a bit simply. For me you have to split your website into some script where you include your navigation script …

    Источник

    3 Steps Simple Dynamic Menu With PHP MYSQL

    Welcome to a quick tutorial on how to create a dynamic navigation menu with PHP and MySQL. So you are looking for ways to dynamically generate a menu, and not have to update the HTML every time?

    We can create a simple dynamic navigation menu with PHP MySQL in just a few steps:

    1. Create a database table to store the menu items.
    2. Create a PHP script to fetch the menu items from the database.
    3. Finally, draw the menu in HTML.

    But just how is this done? Let us walk through an example in this guide – Read on!

    TLDR – QUICK SLIDES

    Dynamic Menu With PHP MYSQL

    TABLE OF CONTENTS

    DYNAMIC MENU IN PHP MYSQL

    All right, let us now get into the example of creating a dynamic menu with PHP and MYSQL.

    STEP 1) MENU DATABASE TABLE

    1A) MENU ITEMS TABLE

    CREATE TABLE `menu_items` ( `item_id` bigint(20) NOT NULL, `parent_id` bigint(20) NOT NULL DEFAULT 0, `item_text` varchar(255) NOT NULL, `item_link` varchar(255) NOT NULL, `item_target` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ALTER TABLE `menu_items` ADD PRIMARY KEY (`item_id`), ADD KEY (`parent_id`); ALTER TABLE `menu_items` MODIFY `item_id` bigint(20) NOT NULL AUTO_INCREMENT; 

    1B) DUMMY MENU ITEMS

    INSERT INTO `menu_items` (`item_id`, `parent_id`, `item_text`, `item_link`, `item_target`) VALUES (1, 0, 'Home', '/', NULL), (2, 0, 'Blog', 'blog/', NULL), (3, 0, 'Reviews', 'reviews/', NULL), (4, 0, 'Shop', 'shop/', '_BLANK'), (5, 2, 'How To', 'blog/how/', NULL), (6, 2, 'Technology', 'blog/tech/', NULL), (7, 2, 'Hacks', 'blog/hacks/', NULL);

    These should be pretty self-explanatory. The first level items are home, blog, reviews, and shop. Only “blog” has second-level items – How to, technology, and hacks. Of course, feel free to nest deeper, for example, (8, 7, ‘Windows Hacks’, ‘blog/hacks/windows/’, NULL) . But I will advise against that – The design and HTML become very difficult with deeply nested items.

    STEP 2) GET MENU ITEMS FROM DATABASE

     PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]); // (B) DRILL DOWN GET MENU ITEMS // ARRANGE BY [PARENT ID] => [MENU ITEMS] $menu = []; $next = [0]; while (true) < $stmt = $pdo->prepare(sprintf( "SELECT * FROM `menu_items` WHERE `parent_id` IN (%s)", implode(",", $next) )); $stmt->execute(); $next = []; while ($r = $stmt->fetch()) < if (!isset($menu[$r["parent_id"]])) < $menu[$r["parent_id"]] = []; >$menu[$r["parent_id"]][$r["item_id"]] = $r; $next[] = $r["item_id"]; > if (count($next) == 0) < break; >> // (C) CLOSE DATABASE CONNECTION $stmt = null; $pdo = null;

    Yikes! This looks pretty confusing, but keep calm and look carefully:

    1. Connect to the database. Remember to change the settings to your own.
    2. Use a while loop to automatically drill down to get the menu items. The menu items are in the arrangement of $menu[PARENT ID][ITEM ID] = MENU ITEM .
    3. Close the database connection.

    I can hear angry trolls screaming “so stupid and dangerous to use an infinite while loop”. I agree, the more deeply nested the menu is, the slower and more complex this becomes. There is also a risk of getting into an infinite loop.

    But this is also one of the better ways to satisfy everyone who wants to do deeply nested menus. There is no perfect system, I cannot please everyone – So feel free to change this part to a recursive function if you are not happy with it.

    STEP 3) DRAW HTML MENU

     
else < draw($i); >> ?>

Источник

Оцените статью