|
|
|
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 | |
|
|
|