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>' ;
}
}
}
?>
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP , PHP , HTML , PDF , Excel 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 Powerful php/mysql Pagination for up to 6 URL Params Categories : PHP , PHP Classes , Databases , MySQL , 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 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 GonxTabs : Create elegant HTML tabs based interface Categories : Navigation , HTML , HTML and PHP , PHP 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 XML Menu Categories : PHP , PHP Classes , Navigation , XML , XSL Database resultset navigation Categories : PHP , HTML and PHP , Databases , MySQL , Navigation Record Set Paging with PHP (RSP) Categories : PHP , MySQL , Navigation , Databases , HTML and PHP This is a database wrapper for PostgreSQL, but can be simply modified for any other database type. Categories : Databases , PostgreSQL , PHP Authorize Me! An authentication script. Categories : MySQL , Databases , Authentication , PHP Different Call User Functions Categories : PHP , Functions , Beginner Guides How to thread a list of messages in database
and show it in a treelike structure Categories : PHP , MySQL , Databases Alert in JavaScript and Trace in Flash Action script are two commands that I find very much useful for tracking and debugging errors in my scripts. Unfortunately, there is no such option in PHP. Categories : PHP , Java Script , Debugging