drop down menu from database - php

I am want to make a navigation menu with a sub menu on hover. I want to get this data from my database (to make it dynamic if data in database changes menu bar changes). In my database I have a table with the the following fields:
ID, Name, Level, Parent_id. Level can be 0 or 1. 0 for main menu 1 for sub menu the id of a main menu is tied to the parent_id field.
So for instance:
ID Name Level Parent_id
1 Test 0
2 Test2 0
3 Test_sub 1 1
4 Test_sub2 2 2
I have managed to get the main menu items from the db but now I am a little bit stuck. This is my code any help would be appriciated.
<?php
$q2= "SELECT * from menu where level = 0 ";
$q2result = $db->query($q2);
while($a2 = $q2result->fetch(PDO::FETCH_ASSOC)){
echo "
<ul>
<li><a href='#' ><span> " . $a2['name'] . " </span></a>
<ul>
<li><a href='#' ><span> test </span></a></li>
</ul>
</ul>
";
}
?>

First load all your datas into variables. Never print/echo while in SQL statement.
<?php
$q2= "SELECT * FROM menu ORDER BY Level ASC";
$q2result = $db->query($q2);
$nodes=array();
while($a2 = $q2result->fetch(PDO::FETCH_ASSOC)){
// assuming one parent node has already been assigned, due do level order into the SQL
if($node['Level']>0 && isset($nodes[$a2['id']])) {
$nodes[$a2['id']]['nodes'][]=$a2;
} else {
// parent node
$nodes[$a2['id']]=$a2;
$nodes[$a2['id']]['nodes']=array();
}
}
print "<ul>";
foreach($nodes as $node) {
print "<li>";
print "<a href='#'>".$node['Name']."</a>";
if(sizeof($node['nodes'])>0) {
print "<ul>";
foreach($node['nodes'] as $subnode) {
print "<li><a href='#'>".$subnode['Name']."</a></li>";
}
print "</ul>";
}
print "</li>";
}
print "</ul>";
?>
This may be much more improved, but with this, you can do what you ask, and improve it.

Related

How can I return data from MySQL as navbar links [duplicate]

This question already has an answer here:
Struggling to output PHP array as unordered HTML list
(1 answer)
Closed 2 years ago.
I want to return some data from my table as navbar links, my table holds this information:
Then I coded this:
<?php
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");
while($row = $result->fetch_object()){
echo "
<li><a href='contact.html'>".$row->name_link."</a></li>
<li><a href='about.html'>".$row->name_link."</a></li>
<li><a href='news.html'>".$row->name_link."</a></li>
<li><a href='news.html'>".$row->name_link."</a></li>
<li><a href='news.html'>".$row->name_link."</a></li>
<li><a href='blog.html'>".$row->name_link."</a></li>
<li class='active'><a href='index.html'>".$row->name_link."</a></li>
";
}
?>
But this is wrong because it prints this as result:
And I want each item to be printed once and then another name_link appears.
So how can I do that?
Assuming the value from name_link column is the same of html file, then:
<?php
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");
while($row = $result->fetch_object()){
echo "<li><a href='".$row->name_link.".html'>".$row->name_link."</a></li>";
}
?>
This would seem to be more like what you need - create one <li> per row returned from your table. (I've assumed href-link is the field in your table which should be used as the URL.)
<?php
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");
while($row = $result->fetch_object()){
echo "<li><a href='".$row->href_link."'>".$row->name_link."</a></li>";
}
?>
What you were doing before is repeating your entire menu for every row in your table, which doesn't make any sense.

how to display data from two tables in php

I have two tables in a database, sight_country and sightseeing. I am inserting the ID of the country field from the sight_country table to s_country field of the table sightseeing. In php I am showing country field values from sight_country in a CSS drop-down menubar.
the code is
<li class="menu-item-has-children">Sightseeing
<ul class="sub-menu">
<?php
$qry_st = "select * from `sight_country` limit 5";
$rec_st = mysql_query($qry_st );
if( mysql_num_rows($rec_st) > 0)
{
while($res_st = mysql_fetch_array($rec_st))
{
echo "<li><a href='sightseeing.php?id=$res_st[id]'>".$res_st['country']."</a></li>";
}
}
?>
</ul>
</li>
When click on link of county value then I am showing all sightseeing data from the table sightseeing in php page.
the code is
$sql = "select * from `sightseeing` where `s_country` ='$id'";
$res = mysql_query($sql);
$rec = mysql_fetch_array($res);
the country may have two or more related sightseeing data, so I am displaying sightseeing titles from the sightseeing table in a sidebar menu in my PHP page.
the code is
<ul class="st_lnks">
<?php
$qry_st = "select * from `sightseeing` where s_country = '$id'";
$rec_st = mysql_query($qry_st );
if( mysql_num_rows($rec_st) > 0)
{
while($res_st = mysql_fetch_array($rec_st))
{
echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";
}
}
?>
</ul>
when I click link of stitle I want to show it's related sightseeing data in same page. How it can be done?
I am assuming that;
The whole script is on one page (sightseeing.php), which varies depending on any GET variables (variables in the URL).
Originally the page just displays the first menu. Then when u click a country, you are sent again to sightseeing.php. Now also with ?id=* which shows also a second list, containing the list of sightseeing relevant to the country selected.
You have a field called 'id' in your sightseeing table that has the unique sightseeing id.
To now additionally show details of the sightseeing selected (clicked by user);
Modify the links in the second list. rather than:
echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";
Write:
echo "<li><a href='sightseeing.php?id=$res_st[s_country]&ss_id=$res_st[id]'>".$res_st['stitle']."</a></li>";
Now when u click one of the links and are sent back to to sightseeing.php you will also have another get variable GET['ss_id'] (which has the id of the sightseeing that you want to view).
You can use this variable to pull the relevant details of the sightseeing.
$sightSeeingId = $_GET['ss_id'];
$sql3 = "select * from `sightseeing` where `id` ='$sightSeeingId' LIMIT 1";
$res3 = mysql_query($sql3);
$sightSeeingData = mysql_fetch_array($res3);
check that it has data and print it out
if(!$res3) die(mysql_error());
if(mysql_num_rows($res3) > 0){
echo "Sight Seeing id:" . $sightSeeingData['id'];
}
As a side note you should be aware that mysql_* functions are outdated and your code is vunerable to sql injection, see here;
GET parameters vulnerable to SQL Injection - PHP
You could give your second link a second parameter. Give the first parameter a unique name
<ul class="sub-menu">
<?php
$qry_st = "select * from `sight_country` limit 5";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
while ($res_st = mysql_fetch_array($rec_st)) {
echo "<li><a href='sightseeing.php?idCountry=$res_st[id]'>".$res_st['country']."</a></li>";
}
}
?>
</ul>
Then add a second parameter when generating the second link:
<ul class="st_lnks">
<?php
$qry_st = "select * from `sightseeing` where s_country = '$id'";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
while($res_st = mysql_fetch_array($rec_st)) {
echo "<li><a href='sightseeing.php?idCountry=$idCountry&idSightseeing=res_st['id']'>".$res_st['stitle']."</a></li>";
}
}
?>
</ul>

display multilevel database driven menu in php

I want to display a database driven Multilevel menu and below is what I have tried so far, but I am not getting the result as desired. Can any one help me to fix this. the output I am getting is only the main menu with parent id as 0, but not the sub menu items.
<?php
include('system/connection.php');
?>
<?php
//select all rows from the main_menu table
$q = "SELECT * FROM catelogue WHERE cat_visibility = '1'";
$r = mysqli_query($dbc, $q);
echo "<ul class='dropdown'>";
while ($rows = mysqli_fetch_assoc($r)) {
if ($rows['cat_parentid'] == 0) {
echo "<li><a href='#'>" . $rows['cat_name'] . "</a>";
echo "<ul>";
foreach ($rows as $item) {
if ($item['cat_parentid'] == $rows['cat_id']) {
echo "<li><a href='#'>" . $item['cat_name'] . "</a>";
echo "</li>";
}
}
echo "</ul>";
echo "</li>";
}
}
echo "</ul>";
?>
My Database structure is
-----------------------------------------
| cat_id | cat_name | cat_parentid |
-----------------------------------------
| 1 | Home | 0 |
| 2 | About | 0 |
| 3 | Contact | 0 |
| 4 | History | 2 |
| 5 | Services | 2 |
-----------------------------------------
Desired Output I want:
<ul class="dropdown">
<li><a href='#'>Home</a></li>
<li><a href='#'>About</a>
<ul>
<li><a href='#'>History</a></li>
<li><a href='#'>Services</a></li>
</ul>
</li>
<li><a href='#'>Contact</a></li>
</ul>
Here is a recursive solution.
The code is fully commented.
There are two useful checks in the processMenuEntry routine that can conveniently be done so that you can decide if you want different actions to happen.
Check whether the 'current' 'entry' is the 'root' node.
$isRoot = $currentEntry['cat_id'] == 0; // do 'First time' processing
Check whether the current 'entry' has a 'submenu'.
if (!empty($subMenu)) { ...
Q29910284-display-multilevel-database-driven-menu.php
The code:
Database connection:
$DB_HOST = "localhost";
$DB_USER = "test";
$DB_PASSWORD = "test";
$DB_TO_USE = "testmysql";
$dbc = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_TO_USE);
Submenu Query:
/** -----------------------------------------------------------------
* Select all the menu entries for a given entry Id
*
* Note: it is safe to do 'parameter substitution' rather than using
* 'prepared queries' with placeholders as this 'entry Id' never
* comes from an external source.
*
* #param mysqli $dbc
* #param integer $currentEntryId
* #return array of menu entries
*/
function selectSubMenu($currentEntryId, $dbc)
{
$sql = "SELECT cat_id, cat_name, cat_parent_id
FROM catalogue
WHERE cat_parent_id = {$currentEntryId}
ORDER BY cat_id";
$resultSet = mysqli_query($dbc, $sql);
return $resultSet->fetch_all(MYSQLI_ASSOC);
}
Process Current Menu Entry:
/** --------------------------------------------------------------------------
* Process the current menu enty - it will return a complete menu as HTML
*
* These needs to know whether the current entry has a submenu and
* will therefore need to generate an 'unordered list' for the current entry.
*
* Alas, it also has to not display the 'list item text' for the 'root' entry
* but process the 'submenu'.
* This complicates the <li> current entry text generation slightly.
*
* #param array $currentEntry - one row
* #param array $subMenu - many rows - may be empty
* #param mysqli $dbc - database connection
* #return string - the HTML for the menu
*/
function processMenuEntry($currentEntry, array $subMenu, $dbc) {
$outMenu = '';
$isRoot = $currentEntry['cat_id'] == 0; // do 'First time' processing
// display the current entry text as a 'list item'
$outMenu .= !$isRoot ? "<li><a href='#'>" . $currentEntry['cat_name'] . "</a>" : '';
// does it have a submenu...
if (!empty($subMenu)) { // we need to add a complete submenu of <ul> ... </ul>
// Start of the submenu as an unordered list -- decide what is required
if ($isRoot) {
$outMenu .= '<ul class="dropdown">';
}
else {
$outMenu .= '<ul>';
}
// Display each entry of the submenu as a 'list item'
foreach ($subMenu as $submenuEntry) {
$outMenu .= processMenuEntry($submenuEntry,
selectSubMenu($submenuEntry['cat_id'], $dbc),
$dbc);
}
// close the current submenu - terminate the unordered list
$outMenu .= '</ul>';
}
// terminate the current list item
$outMenu .= !$isRoot ? '</li>' : '';
return $outMenu;
};
Process all the menu entries:
/* -------------------------------------------------------------------
* Process all the menu entries
*
* We need a complete menu 'tree' that includes a 'root' which is not provided
* in the database. I think it should be. Whatever, i need one.
*
* Initializing a 'tree walk' i always find 'interesting' ;-/
*/
$root = array('cat_id' => 0, 'cat_name' => '', 'cat_parent_id' => 0);
// build the output menu
$outMenu = processMenuEntry($root,
selectSubMenu($root['cat_id'], $dbc),
$dbc);
// wrap it in a <div>
$htmlMenu = '<div style="border: 1px solid red">'
. $outMenu
.'</div>';
?>
Output the generated HTML:
<!DOCTYPE html>
<html>
<head>
<title>Test Recursive Menu Builder</title>
</head>
<body>
<?= $htmlMenu ?>
</body>
</html>
The generated HTML
<!DOCTYPE html>
<html>
<head>
<title>Test Recursive Menu Builder
</title>
</head>
<body>
<div style="border: 1px solid red">
<ul class="dropdown">
<li>
<a href='#'>Home</a>
</li>
<li>
<a href='#'>About</a>
<ul>
<li>
<a href='#'>History</a>
</li>
<li>
<a href='#'>Services</a>
</li>
</ul>
</li>
<li>
<a href='#'>Contact</a>
</li>
</ul>
</div>
</body>
</html>
What you call $rows is actually one row. Then, in the foreach ($rows as $item) loop, it iterates through the columns of this row. So, there is no $item['cat_parentid']. Try to see the output of the $rows and $item with var_dump().
A draft idea of one possible solution that comes to my mind is to first iterate through the result rows and save the sub items in a parent item (note: some array initializations would have to be added here):
while ($row = mysqli_fetch_assoc($r)) {
$menuItems[$row['cat_id']] = $row;
$parentId = $row['cat_parentid'];
$menuItems[$parentId]['sub_items'][] = $row['cat_id'];
}
And then iterate through the $menuItems array generating output, recursion would be great for this.
Also, ordering the sql results would be beneficial, to be sure that top menu items come first:
"SELECT * FROM catelogue WHERE cat_visibility = '1' ORDER BY cat_parentid ASC";

Multilevel menu from database records

I need some help with PHP. I have a multilevel css menu that works fine, but now I want to generate according to the records from a database.
The menu Code:
<div id="page-wrap">
<ul class="dropdown">
<li>Link 1 </li>
<li>Link 2 </li>
<li>Link 3 </li>
<li>Link 4
<ul class="sub_menu">
<li>Link 4 - 1</li>
<li>Link 4 - 2</li>
<li>Link 4 - 3</li>
</ul>
</li>
<li>Link 5</li>
<li>Link 6 </li>
</ul>
</div>
In the database every record has a field called main_manu which makes it a main link if the value of this field is Yes, and if the value is No then it has another field (sub_menu) which says the parent menu in which this link should be placed.! See the screenshot.
DB Fields
SO, now I don't know how to do the php piece that will get the records and if the value of the main_menu is yes, it will create a top level menu, and if the value of main_menu is no, it will create a sub level menu according to the value of sub_menu field.
Thanks a lot
UPDATE
This is the code I have so far and it works. It would be nice if I could use a single query instead of multiple nested queries.
<div id='page-wrap'>
<ul class='dropdown'>
<?
$sql_menu = "SELECT * FROM content WHERE visible = '1' and main_menu='yes' ";
$result_menu = mysql_query($sql_menu);
while($row = mysql_fetch_assoc($result_menu))
{
$id=$row['id'];
$menu_top=$row['menu_name'];
$menu_url=$row['menu_url'];
print "<li><a href='$menu_url'>$menu_top</a>";
$sql_sub = "SELECT * FROM content WHERE sub_menu = '$menu_url' ";
$result_sub = mysql_query($sql_sub);
$num_rows = mysql_num_rows($result_sub);
while($row = mysql_fetch_assoc($result_sub))
{
$id=$row['id'];
$menu_sub=$row['menu_name'];
$sub_url=$row['menu_url'];
If ($num_rows != 0)
{
print "<ul class='sub_menu'>
<li><a href='$sub_url'>$menu_sub</a></li>
</ul>";
}
}
print "</li>";
}
?>
</ul>
</div>
The code for this would be something like the following (This will need to be changed for whatever way you interact with the database etc.):
// Here we do a query to get all the rows from the table
$db_result = db_execute_query('SELECT * FROM `menu_table` ORDER BY `order_no`');
// Here we take the rows and put it into a structured array
$items = array();
$hierarchy = array('' => array());
while ($row = db_fetch_row($db_result)) {
$items[$row['menu_name']] = $row['menu_name_en'];
if ($row['main_menu'] == 'yes') {
$hierarchy[''][] = $row['menu_name'];
} else {
if (!isset($hierarchy[$row['sub_menu']]) {
$hierarchy[$row['sub_menu']] = array();
}
$hierarchy[$row['sub_menu']][] = $row['menu_name'];
}
}
// Here we define a recursive function to run through our $hierarchy array;
function show_menu($name = '') {
if (isset($hierarchy[$name])) {
if ($name == '') {
echo '<ul class="dropdown">';
} else {
echo '<ul class="sub_menu">';
}
foreach ($hierarchy[$name] as $sub) {
echo '<li>' . $items[$sub] . '';
show_menu($sub);
echo '</li>';
}
echo '</ul>';
}
}
// Here we execute the recursive function on the main menu
show_menu('');
Try to understand what I'm doing here instead of just implementing it verbatim. Once you get to know recursive functions, a whole new world can open for you.
Also note that your db table could be changed to make this code simpler
I think these link will help you out. if not please let me know i will give you the code
http://board.phpbuilder.com/showthread.php?10337808-how-to-create-menu-submenu-subsubmenu-using-php-mysql
http://www.sitepoint.com/forums/showthread.php?718237-how-to-create-dynamic-menu-and-submenu-using-php-and-mysql

Can't manage to run 2 while loops that create main links and sub drop down links in PHP

So I have one query which displays the main categories, and one query which displays the sub categories. I can't manage to display all the sub categories, in fact, it will only display the second database input for subcategories.
Here's the code
<?php
$mainlinks = mysql_query("select * from ns_categories");
while ($catlinks = mysql_fetch_array($mainlinks)) {
$catlinkname = $catlinks['name'];
$catlinkid = $catlinks['id'];
$sublinks = mysql_query("select * from ns_subcategories where mainid='$catlinkid'");
echo "<ul>";
echo "<li><a href='products.php?mc=$catlinkid'>$catlinkname</a></li>";
while ($catsublinks = mysql_fetch_array($sublinks)) {
$sublinkname = $catsublinks['name'];
$sublinkid = $catsublinks['id'];
echo "<ul>";
echo "<li><a href='products.php?sc=$sublinkid>$sublinkname</a></li>";
echo "</ul>";
echo "</ul>"
}
}
?>
Also, when looking at the output from the subcategories get variables, the link leads me to
products.php?sc=3>Themes</a></li></ul></ul><ul><li><a href=
Any ideas? Thanks!
The last echo "</ul>" should be outside the inner while loop.
I don't see a closing ' on the line that generates your link.

Categories