WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP Nested Menu Example - Shows how to make a single level nested menu
Categories : PHP, Navigation Update Picture
John Harrison
Date : Jul 27th 2002
Grade : 3 of 5 (graded 14 times)
Viewed : 10468
File : No file for this code example.
Images : No Images for this code example.
Search : More code by John Harrison
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Used to create a single level nested menu.
simply call it like this: menu(get_page());
use this sql query to set up the table:

CREATE TABLE menu_data (
  id int(11) unsigned NOT NULL auto_increment,
  pid int(11) unsigned NOT NULL default '0',
  title varchar(100) NOT NULL default '',
  page_name varchar(100) NOT NULL default '',
  PRIMARY KEY  (id),
  KEY pid (pid),
  KEY page_name (page_name)
) TYPE=MyISAM;


I posted this because there arn't many nest menu examples availible on the net for php.

You will need to open a DB connection before using this. It should be easy to adapt
to other DBs like postgreSQL or MS-SQL

<?php
function get_page(){
 
$fn = explode("/", $_SERVER['PHP_SELF']);
 
$num_of_s = count($fn) - 1;
 
$fn = "$fn[$num_of_s]";
 
$query = "SELECT id, pid, title, page_name FROM menu_data WHERE page_name = '$fn'";
 
$result = mysql_query($query) or die("Query failed: $query<br>" . mysql_error());
 
$num_results = mysql_num_rows($result);
 
$row = mysql_fetch_array($result);
   
  return array(
$row['id'], $row['pid'], $row['title']);
}

function
print_child($id, $sid){
   
$peer_query = "select * from menu_data where pid = $sid";
   
$peers = mysql_query($peer_query) or die("Query failed: $query<br>" . mysql_error());
    while (
$prow = mysql_fetch_array($peers)){
        if(
$id == $prow['id']){
            echo
'<img alt="CO2Busters menu tree" src="images/tree.gif" height="15" width="20">'.$prow['title'].'<br>';
        }else{
            echo
'<img alt="CO2Busters menu tree" src="images/tree.gif" height="15" width="20"><a href="'.$prow['page_name'].'">'.$prow['title'].'</a><br>';
      }   
    }
}

function
menu($menu_info) {
 
$query = "select * from menu_data order by id";
 
$result = mysql_query($query) or die("Query failed: $query<br>" . mysql_error());;
 
$num_results = mysql_num_rows($result);

  for (
$i=0; $i < $num_results; $i++){
   
$row = mysql_fetch_array($result);
        if(
$menu_info[0] == $row['id'] && $row['pid'] == 0){
            echo
$row['title'].'<br>';
           
print_child($menu_info[0], $menu_info[0]);
        }elseif(
$menu_info[0] == $row['id'] && $row['pid'] == $menu_info[1]){
           
print_child($menu_info[0], $menu_info[1]);
        }elseif(
$row['pid'] == '0') {
            echo
'<a href="'.$row['page_name'].'">'.$row['title'].'</a><br>';
        }
    }
}
?>



A simple class with some HTML output functions that would come in handy for consistent page layout etc.
Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
Record Set Paging with PHP (RSP)
Categories : PHP, MySQL, Navigation, Databases, HTML and PHP
GonxTabs : Create elegant HTML tabs based interface
Categories : Navigation, HTML, HTML and PHP, PHP
XML Menu
Categories : PHP, PHP Classes, Navigation, XML, XSL
Javascript/DHTML menus without all the extra javascipt.... server side browser detection + dynamically generated javascript (build menus on fly using php)
Categories : PHP, Java Script, Navigation
Gonx URLs - This class is meant to generate URLs for accessing application dynamically generated pages based on parameters passed in the URL.
Categories : PHP, PHP Classes, Navigation, URLs
Database resultset navigation
Categories : PHP, HTML and PHP, Databases, MySQL, Navigation
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
TreeView - Finally a working tree view function to be used as you want. Simple create the Table using the code provided and you will be able to have a tree view in your project. Download the zip to get the images.
Categories : PHP, HTML and PHP, Navigation
Retrieve text from table and email to your e- address in pipe delimited format.
Categories : PHP, MySQL
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
A PHP function to encrypt and decrypt a number or string or a combination of the two.
Categories : PHP, Encryption, Security
Using $PHP_AUTH_USER and $PHP_AUTH_PW to authenticate.
Categories : Authentication, PHP
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services