<?php
/**************************************
Page:
Author: Boss Hui
Function: Define database operations and generate menu list classes
**************************************/
class menu{
//Create a constructor, function: connect the database and select the corresponding database
public function __construct(){
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "7529639";
$dbname = "menu";
mysql_connect($dbhost,$dbuser,$dbpassword) or die("error!");
mysql_query("SET NAMES 'GBK'");
mysql_select_db($dbname);
}
//Execute SQL statement functions
private function query($sql){
return mysql_query($sql);
}
//Get the result set array function
private function loop_query($result){
return mysql_fetch_array($result);
}
//List menu list function
public function menulist(){
$sql="select * from list order by path";
$result=$this->query($sql);
while($rows=$this->loop_query($result)){
if(substr_count($rows['path'],',')>2){
for($i=0;$i<(substr_count($rows['path'],',')-2);$i++)
echo ' ';
}
echo $rows['name'].'<br>';
}
}
//Create a destructor, function: close the database connection
public function __destruct(){
return mysql_close();
}
}
$db=new menu();//Generate an instance
$db->menulist();//Calling the method to generate menu
?>