Because it exposes the contents of the menu.php module. Below we will present our own menu development in PHP, which was written from scratch in a notepad.

This code will be especially useful for dynamic sites that have custom engines. I will offer two code options that have minor differences (the differences will be explained later).

To begin with, I will give an approximate structure of the site for which this menu is suitable. The site structure should look like this (classic view):

/index.html /razdel_1/ /razdel_1/articles_1.html /razdel_1/articles_2.html ... /razdel_2/ /razdel_2/articles_1.html /razdel_2/articles_2.html ... ... ... /razdel_N/articles_2 .html

The site may also contain subsections for sections:

/razdel_1/podzaderl_1/ /razdel_1/podzaderl_1/articles_1.html /razdel_1/podzaderl_1/articles_2.html ... /razdel_1/podzaderl_2/articles_1.html /razdel_1/podzaderl_2/articles_2.html

This structure will also work for our menu with only minor differences.

I suggest creating separate file for menu in php. For example, menu.php would be a great name for such a file. To implement the menu, a menu style in CSS is also provided to immediately make it more or less beautiful. Naturally, this style is given for reference only, since the designs of the sites are very different.

Code for menu styling in CSS:

.menu ( height:42px; padding:0 0 0 16px; background:url(images/spacer.png) repeat; ) .menu li ( display:block; float:left; ) .menu li.active ( background: #000011 ; ) .menu a ( color:#FFF; display:block; line-height:42px; text-decoration:none; padding:0 14px; ) .menu a:hover ( background:url(images/spacer.png) repeat ; )

Now, let's look at the first option for implementing a menu in PHP, which is a little simplified.

The first version of the menu code in PHP

\n"; for ($i=0;$i ": "
  • "; echo " ".$array_menu[$i]["name"]."
  • \n"; ) echo ""; ?>

    The menu can be divided into two parts. The first contains the $array_menu information array, which contains the names of our sections with links to sections. There is an option to enter this data into the database mySQL data, but there is no particular point in this, since the sample is very small, so this will not affect the speed of work.

    The second part contains the menu output via for loop. The cycle compares the site address with the address from the $array_menu array. If there is a match, then we display the next menu section with a special active class:

  • , otherwise just
  • . This allows us to highlight with some color the part of the menu in which the user is located. In my opinion, this is a necessary thing for any site, so that the user can understand which section he is in.

    The order in the array will be preserved when the menu is displayed on the site. That is, the array must be filled in the order in which the menu should be displayed.

    Note:
    If the URLs (addresses) of the section headings look like:
    /section_1
    or like this
    /razdel_1/nazvanie_razdela.html
    then you need to write an exact match in array_menu:
    $array_menu[$i]["url"]="/razdel_1"
    or for the second case:
    $array_menu[$i]["url"]="/razdel_1/nazvanie_razdela.html";

    How does the first menu option work?
    It only highlights the menu if you are at the section header address. For example, if the page address is /razdel_1/articles_1.html, then the menu will not be highlighted in any way.

    The second version of the code is a modified version of the first and provides the ability to highlight menus even in articles that are located in sections.

    The second version of the menu code in PHP

    "; for ($i=0;$i ": "
  • "; echo "".$array_menu[$i]["title"]."
  • "; ) else ( echo ($URL) == ($array_menu[$i]["url"]) ? "
  • ": "
  • "; echo "".$array_menu[$i]["title"]."
  • "; ) ) echo ""; ?>

    In this article I will show how you can create multi-level menu in PHP and MySQL. Of course, you can come up with many options for creating it, but judging by the number of your questions on this topic, you need an example. And I will give it in this article. Let me note right away that this article makes sense only for those who know PHP and knows how to work with MySQL. Everyone else needs to go through this first, or read some books on PHP and MySQL.

    First, let's create a table in the database with the following fields:

    • id- unique identificator.
    • title- anchor links in the menu.
    • link- the address to which the menu item will lead.
    • parent_id- parent ID. If there is no parent item, then it will be NULL (or you can also put 0).

    We've sorted out the table, now it's time PHP code. Full PHP code is given below:

    $mysqli = new mysqli("localhost", "root", "", "db"); // Connect to the database
    $result_set = $mysqli->query("SELECT * FROM `menu`"); // Select all records from the table with the menu
    $items = array(); // Array for menu items
    while (($row = $result_set->fetch_assoc()) != false) $items[$row["id"]] = $row; // Fill the array with a sample from the database
    $childrens = array(); // Array for matching child elements to their parents
    foreach ($items as $item) (
    if ($item["parent_id"]) $childrens[$item["id"]] = $item["parent_id"]; // Fill the array
    }
    function printItem($item, $items, $childrens) (
    /* Display the menu item */
    echo "

  • ";
    echo "".$item["title"]."";
    $ul = false; // Were children rendered?
    while (true) (
    /* An infinite loop where we search for all children */
    $key = array_search($item["id"], $childrens); // Looking for a child element
    if (!$key) (
    /* No children found */
    if ($ul) echo ""; // If child elements were displayed, then close the list
    break; // Exit the loop
    }
    unset($childrens[$key]); // Remove the found element (so that it is not displayed again)
    if (!$ul) (
    echo "
      "; // Start the internal list if there are no child elements yet
      $ul = true; // Set the flag
      }
      echo printItem($items[$key], $items, $childrens); // Recursively display all child elements
      }
      echo "";
      }
      ?>

      This code is completely working, however, you must understand that no one writes this way (in particular, output via echo HTML tags). And your task is to take the algorithm from this code, but not the code itself. And then connect this algorithm to your engine. I tried to carefully comment the output code multi-level menu in PHP and MySQL, but, of course, it is not the most transparent and requires quite good initial knowledge. If you still don’t know well PHP and MySQL, then I strongly recommend going through this one first

      This is exactly what we will look at now. And so, the task is to make a vertical menu in the form of a drop-down list.

      How can this be implemented in PHP? Very simple! For example, we have a file index.php, which, depending on the selected section in the drop-down list, should display the corresponding content on the page. This is implemented as follows:

      1. We create files in .html format, which will contain the content necessary for output.

      2. We create (write) in the index.php script the necessary conditions to display the relevant information.

      3. We consider the created script from the point of view of the security of the script being executed.

      Well, it seems like we’re done with theory, let’s move on to practice. First, we create static pages in .html format, which will contain necessary information. You can write whatever you want there :) As a result, we should end up with at least two files first.html and second.html , so then we can make as many of them as we want, once we understand the basic algorithm of how the code works.

      To display a dropdown list in a file index.php create an html form and write below PHP script with the following content:



      Menu PHP


      Dropdown menu in PHP












      if (isset ($_GET ["where" ]))
      {
      if ($_GET ["where" ]==1 )
      $file = "first.html" ;
      if ($_GET ["where" ]==2 )
      $file = "second.html" ;
      include($file);
      }
      ?>


      Now the code for the drop-down menu is ready, and the most interesting thing is that it will function normally, but from a security point of view, it is vulnerable.

      The vulnerability of this script is that the variable $file remains uninitialized, and in this case the value of the automatically created variable goes directly into the function include, and she, in turn, successfully connects it (the variable) and displays it on the screen. And it may not only be the .htaccess configuration file. In order to hack this script, it is enough to pass the where parameter a value not provided for by the code, for example 3. And since this value is not provided for by the script, it is just a variable $file will not undergo initialization. Therefore, it can be given an arbitrary value via the URL string.

      http://localhost/index.php?where=3&file=.htaccess

      But this is so, a slight digression from the topic. 😀

      The solution to this problem is quite simple, variable $file you just need to initialize it before using it, i.e. assign it a default value.

      Here, if the where parameter is passed to the script, then the variable will be initialized correctly, otherwise it will simply be empty. This is how the [safe] drop-down menu turned out in PHP.

      P.S. When creating scenarios, it is necessary to take into account and accordingly exclude all possible attack vectors. This is the only way to create a project that will meet safety rules and be in demand by the customer. See you again!

      Apr 23 2017

      In previous lessons we looked at examples of what arrays are. In this tutorial we will use arrays in practice to create a menu on a website.

      Why are they more convenient than regular HTML tags?

      Well, let's say our site has 100 pages, each of which has the same menu. And suddenly we needed to change menu items. Using only HTML, we will have to make edits on 100 pages of the site, which is a lot. So PHP comes to our aid in such situations. It will be enough to change the menu items just once in one file.

      Now let's go.

      On local hosting in the folder “ domains create a folder called “ array- menu. local”.

      In this folder we create four files: index. php, about. php, contact. php And menu. php.

      !!! The file syntax should bePHP.

      In the index.php file we write a simple HTML framework.




      charset="utf-8" >



      home


      include("menu.php" );
      ?>



      Copy this code into the about.php and contact.php files. We only change the page names in the tag

      .

      We write code for the menu.

      In file menu.php write an associative array.

      $menu = array (
      "index "=> "index.php",
      "about" => "about.php",
      "contacts" => "contact.php"
      );
      ?>

      Below using HTML tags writing a menu.

      In the browser we will see a regular menu in the form of a list.

      By clicking on any menu item, we will go to the corresponding page, and the menu will be on all pages.

      Now let's complicate the task. We delete everything from the file menu.php And...

      We are writing a multidimensional array.




      charset="utf-8" >
      <span>Using an array to display a menu on a website.</span>


      $menu = array (
      array("link" => "Home", "href"=> "index.php"),
      array("link" => "About us", "href"=> "about.php"),
      array("link" => "Contacts", "href"=> "contact.php")
      );
      ?>





      And at the last stage...

      Drawing a menu using a loop foreach.

      Removing the list from the menu.php file

        and write the following code instead.

        echo"

          " ;
          foreach($menu as$item)(
          echo "
        • ($item)
        • " ;
          }

          In the browser we will see the same result.

          To change menu items, just change them in the file menu.php just once.