» Menu en base de donnée
Voici un menu dont les items sont en base de données. Il est entièrement récursif.
article_17.php
<?php
// ------------------------------------------------------------------------- //
// Menu en base de donnée //
// ------------------------------------------------------------------------- //
// 04/06/03 16:39:30 par Thomas Harding (thomas.harding@laposte.net) //
// ------------------------------------------------------------------------- //
// http://www.gphp.net/articles,17.php //
// ------------------------------------------------------------------------- //
//-------------------------------------------------------------------------- //
// HDG_PHP_MENU V. 0.8 //
//-------------------------------------------------------------------------- //
// Dynamic PHP manu / Menu dynamique PHP //
// ------------------------------------------------------------------------- //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//
// ------------------------------------------------------------------------- //
// Todo : close the menu when clicked again //
////////////////////
//
// constants
//
define ('LOGIN','guest');
//define ('PASSWD','');
//include ('passwd.php');
define ('SERVER','localhost');
define ('DATABASE','menu');
define ('MENU_TABLE','menu');
define ('SCRIPT_PATH',$PHP_SELF);
//define ('MENU_STYLE','li');
define ('MENU_STYLE','div');
//define ('PAGE_TYPE','noframes');
define ('PAGE_TYPE','frames');
////////////////////
//
// How to use
//
/*
Create a table :
+--------+--------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------+------+-----+---------+----------------+
| ID | int(6) | | PRI | NULL | auto_increment |
| parent | int(6) | YES | | NULL | |
| nom | text | YES | | NULL | |
| action | text | YES | | NULL | |
+--------+--------+------+-----+---------+----------------+
Fill table :
+----+--------+-------+--------+
| ID | parent | nom | action |
+----+--------+-------+--------+
| 1 | 0 | menu1 | |
| 2 | 0 | menu2 | |
| 3 | 0 | menu3 | |
| 4 | 1 | menu4 | |
| 5 | 4 | menu5 | test5 |
| 6 | 4 | menu6 | test6 |
| 7 | 0 | menu7 | |
+----+--------+-------+--------+
if you use frames, replace test5 and test6 by URLs.
+----+--------+-------+-------------------------+
| ID | parent | nom | action |
+----+--------+-------+-------------------------+
| 1 | 0 | menu1 | |
| 2 | 0 | menu2 | |
| 3 | 0 | menu3 | |
| 4 | 1 | menu4 | |
| 5 | 4 | menu5 | ./menu.php?menuactive=5 |
| 6 | 4 | menu6 | ./menu.php?menuactive=6 |
| 7 | 0 | menu7 | |
+----+--------+-------+-------------------------+
define constants in menu.php file (constants are self-explanatory)
write your code instead of sample use one
N.B.: "div" or "li" classes are "menuN" with "N" = menu level.
use it...
*/////////////////////
//
//sample use follows
//
$menuactive = $_GET['menuactive'];
$action = $_GET['action'];
if (empty($action)) $action = $menuactive;
echo <<<FIN
<?xml version="1.0" encoding="iso-8859-15"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<meta name="generator" content="menu.php" />
<style>
<!--
div.menu { position: absolute ; left: 1em ; right: 90%; top: 5em;}
div.menu0 { background: white; }
div.menu1 { background: white ; margin-left: 1em}
div.menu2 { background: white ; margin-left: 2em}
div.action { position: absolute;
left: 50%;
top:50%;
font-size: 150%;
font-weight: bold;}
-->
</style>
</head>
<body>
<div class="menu">\n
FIN;
$menu = new MENU (MENU_TABLE,$menuactive);
echo <<<FIN
</div>
<div class='action'>ACTION : $action</div>
</div>
</body>/////////////////////////////////////////////////////////////////////////////
// code
//
//
//database access
//
class DB
{
// ---- Private part : properties
var $connexion;
// class builder
function DB ($login, $password, $database, $server)
{
// Connexion to server
$this->connexion = mysql_pconnect ($server, $login, $password);
// Connexion to database
mysql_select_db ($database, $this->connexion);
}
// ---- public part
//execute a request
function execRequest ($requete)
{
$result = mysql_query ($requete, $this->connexion);
return $result;
}
// get next row, object form
function nextObject ($result)
{ return mysql_fetch_object ($result); }
// deconnexion
function quit ()
{ @mysql_close ($this->connexion); }
//
//menu itself
//
class MENU
{
function MENU ($table,$menuactive='')
{
$level = 0;
$parent = 1;
$menu = array();
$hierarchy = array();
$initialMenu = $menuactive;
if (empty($menuactive)) $menuactive=0;
$db = new DB (LOGIN,PASSWD,DATABASE,SERVER);
// search parents
while (!empty($parent))
{
$menu[$level] = $menuactive;
$request = "SELECT * FROM $table WHERE ID = '$menuactive'";
$result = $db->execRequest ($request);
$res = $db->nextObject ($result);
$parent = $res->parent;
if (empty($parent)) $parent = 0;
$menuactive = $parent;
$nbrLevels = $level ;
$level ++;
}
//load menu hierarchy into an array
while ($level >= 0)
{
$parent = $menu[$level];
$request = "SELECT * FROM $table WHERE parent = '$parent'";
$result = $db->execRequest ($request);
$i = 0;
$level --;
while ($res = $db->nextObject ($result))
{
$hierarchy[$level][$i]['ID'] = $res->ID;
$hierarchy[$level][$i]['name'] = $res->nom;
if (PAGE_TYPE == 'frames') $hierarchy[$level][$i]['action'] = $res->action;
else $hierarchy[$level][$i]['action'] =
"?menuactive=".$res->ID."&action=".$res->action;
if (empty($hierarchy[$level][$i]['action']))
$hierarchy[$level][$i]['action'] =
SCRIPT_PATH."?menuactive=".$hierarchy[$level][$i]['ID'];
$i++;
}
}
$level = $nbrLevels;
$i = 0;
$j = array();
if (MENU_STYLE == 'li') echo "<ul>\n";
//determine menu
while ($level <= $nbrLevels)
{
if (!empty($hierarchy[$level][$i]['name']))
{
$name = $hierarchy[$level][$i]['name'];
$action = $hierarchy[$level][$i]['action'];
$menuLevel = $nbrLevels - $level;
if (MENU_STYLE == 'li')
$this->display_menu_li ($name,$action,$menuLevel,$changeLevel);
if (MENU_STYLE == 'div')
$this->display_menu_div ($name,$action,$menuLevel,$changeLevel);
}
$i ++;
if (($hierarchy[$level][$i - 1]['ID'] == $menu[$level]) and ($level != -1))
{
$j[$level] = $i;
$i = 0;
$level --;
if (MENU_STYLE == 'li') echo "<ul>\n";
}
elseif (empty($hierarchy[$level][$i]['name']))
{
$level ++;
$i = $j[$level];
if ((MENU_STYLE == 'li') and ($level != ($nbrLevels + 1))) echo "</ul>\n";
}
}
//display menu as divs
function display_menu_div ($name,$action,$level,$changeLevel)
{
echo "<div class='menu$level'>"
. "<a href='"
. $action
. "'>"
. $name
. "</a>"
. "</div>\n";
}
//display menu as unordered lists
function display_menu_li ($name,$action,$level,$changeLevel)
{
echo "<li class='menu$level'>"
. "<a href='"
. $action
. "'>"
. $name
. "</a>"
. "</li>\n";
}
}
?>
if (MENU_STYLE == 'li') echo "</ul>\n";
}
// end of class
}
</html>
FIN;
?>
article_17.sql
## ------------------------------------------------------------------------- ##
## Menu en base de donnée ##
## ------------------------------------------------------------------------- ##
## Le 04/06/03 16:39:30 par Thomas Harding (thomas.harding@laposte.net) ##
## ------------------------------------------------------------------------- ##
## http://www.gphp.net/articles,17.php ##
## ------------------------------------------------------------------------- ##
CREATE TABLE menu (
ID int(6) NOT NULL auto_increment,
parent int(6) default NULL,
nom text,
action text,
PRIMARY KEY (ID)
) TYPE=MyISAM;
INSERT INTO menu VALUES (1,0,'menu1','');
INSERT INTO menu VALUES (2,0,'menu2','');
INSERT INTO menu VALUES (3,0,'menu3','');
INSERT INTO menu VALUES (4,1,'menu4','');
INSERT INTO menu VALUES (5,4,'menu5','test5');
INSERT INTO menu VALUES (6,4,'menu6','test6');
INSERT INTO menu VALUES (7,0,'menu7','');
» Commentaires
Pas de commentaires.
Vous devez être connecté en tant que Membre pour pouvoir poster un commentaire, Inscrivez vous ici !