1 ----------------menu.php-------------------------
2 <h2>无限分类管理</h2>
3 <a href="index.php">浏览分类信息</a>
4 <a href="add.php">添加分类信息</a>
5 <a href="select.php">下拉菜单框浏览分类信息</a>
6 <a href="index2.php">分层浏览分类信息</a>
7 <hr>
1 ------------------------------dbconfig.php--------------------------------------
2 <?php
3 define("HOST","localhost");
4 define("USER","root");
5 define("PASS","111");
6 define("DBNAME","db_database12");
1 ---------------------------add.php--------------------------------
2 <html>
3 <head>
4 <title>无限分类信息管理</title>
5 </head>
6 <body>
7 <center>
8 <?php
9 include("menu.php");
10 ?>
11 <h3>添加商品类别信息</h3>
12 <form action="action.php?action=add" method="post">
13 <input type="hidden" name="pid" value="<?php echo isset($_GET['pid'])?$_GET['pid']:0 ; ?>"/>
14 <input type="hidden" name="path" value="<?php echo isset($_GET['path'])?$_GET['path']:0 ; ?>"/>
15 <table width="340" border="0">
16 <tr>
17 <td align="right">父类别:</td>
18 <td><?php echo isset($_GET['name'])?$_GET['name']:"根类别" ; ?></td>
19 </tr>
20 <tr>
21 <td align="right">类别名称:</td>
22 <td><input type="text" name="name"/></td>
23 </tr>
24
25 <tr>
26 <td align="right"> </td>
27 <td>
28 <input type="submit" value="添加"/>
29 <input type="reset" value="重置"/>
30 </td>
31 </tr>
32 </table>
33 </form>
34 </center>
35 </body>
36 </html>
-----------------------------------index.php---------------------------------
<html>
<head>
<title>无限分类信息管理</title>
</head>
<body>
<center>
<?php
include("menu.php");
?>
<h3>浏览商品类别信息</h3>
<table width="600" border="1">
<tr>
<th>id号</th>
<th>类别名称</th>
<th>父id</th>
<th>路径</th>
<th>操作</th>
</tr>
<?php
//1. 导入配置信息
require("dbconfig.php");
//2.获取数据库的连接
$link=@mysql_connect(HOST,USER,PASS)or die("数据库连接失败");
mysql_select_db(DBNAME,$link);
mysql_query("set names utf8");
//3.实现数据的查询
$sql="select *from type order by concat(path,id)";
$result=mysql_query($sql,$link);
//4.遍历解析输出内容
while($row=mysql_fetch_array($result)){
echo"<tr>";
echo"<td>{$row['id']}</td>";
echo"<td>{$row['name']}</td>";
echo"<td>{$row['pid']}</td>";
echo"<td>{$row['path']}</td>";
echo"<td><a href='add.php?pid={$row['id']}&name={$row['name']}&path={$row['path']}{$row['id']},'>添加子类</a>
<a href='action.php?action=del&id={$row['id']}'>删除</a>
</td>";
echo"</tr>";
}
//5.释放结果集 关闭数据库连接
mysql_free_result($result);
mysql_close($link);
?>
</center>
</body>
</html>
1 ---------------------------------index2.php----------------------------
2 <?php
3 //1. 导入配置信息
4 require("dbconfig.php");
5 //2.获取数据库的连接
6 $link=@mysql_connect(HOST,USER,PASS)or die("数据库连接失败");
7 mysql_select_db(DBNAME,$link);
8 mysql_query("set names utf8");
9 //获取当前父类别的id
10 $pid=$_GET['pid']+0;
11 //判断是否是在根类别下面
12 if($pid>0){
13 //根据当前父类别id号获取对应的类别信息
14 $sql="select path from type where id={$pid}";
15 $res=mysql_query($sql,$link);
16 $path=mysql_result($res,0,0);
17 //获取当前位置上的所有路径中的类别信息
18 echo $sql="select id ,name from type where id in ({$path}{$pid}) order by id";
19 $typeres=mysql_query($sql,$link);
20 }
21 ?>
22 <html>
23 <head>
24 <title>无限分类信息管理</title>
25 </head>
26 <body>
27 <center>
28 <?php
29 include("menu.php");
30 ?>
31 <h3>分层浏览类别信息</h3>
32 <div style="600;text-align:left;">
33 路径:<a href="index2.php?pid=0">根类别</a>>>
34 <?php
35 //判断并遍历输出路径信息
36 if($typeres &&mysql_num_rows($typeres)>0){
37 while($row=mysql_fetch_assoc($typeres)){
38 echo " <a href="index2.php?pid={$row['id']}">{$row['name']}</a>>> ";
39 }
40 }
41 ?>
42 </div>
43 <table width="600" border="1">
44 <tr>
45 <th>id号</th>
46 <th>类别名称</th>
47 <th>父id</th>
48 <th>路径</th>
49 <th>操作</th>
50 </tr>
51 <?php
52 //3.实现数据的查询
53 $sql="select *from type where pid={$pid} order by concat(path,id) ";
54 $result=mysql_query($sql,$link);
55 //4.遍历解析输出内容
56 while($row=mysql_fetch_array($result)){
57 echo"<tr>";
58 echo"<td>{$row['id']}</td>";
59 echo"<td><a href='index2.php?pid={$row['id']}'>{$row['name']}</a></td>";
60 echo"<td>{$row['pid']}</td>";
61 echo"<td>{$row['path']}</td>";
62 echo"<td><a href='add.php?pid={$row['id']}&name={$row['name']}&path={$row['path']}{$row['id']},'>添加子类</a>
63 <a href='action.php?action=del&id={$row['id']}'>删除</a>
64 </td>";
65 echo"</tr>";
66 }
67 //5.释放结果集 关闭数据库连接
68 mysql_free_result($result);
69 mysql_close($link);
70 ?>
71 </center>
72 </body>
73 </html>
1 ---------------------------action.php-------------------------------
2 <?php
3 //实现无限分类的信息的添加和删除操作
4 //1. 导入配置信息
5 require("dbconfig.php");
6 //2.获取数据库的连接
7 $link=@mysql_connect(HOST,USER,PASS)or die("数据库连接失败");
8 mysql_select_db(DBNAME,$link);
9 mysql_query("set names utf8");
10
11 //3.根据action参数的值,做对应操作
12 switch($_GET["action"]){
13 case "add":
14 //获取要添加的信息
15 $name=$_POST["name"];
16 $pid=$_POST["pid"];
17 $path=$_POST["path"];
18 //执行添加操作
19 $sql="insert into type values(null,'{$name}','{$pid}','{$path}')";
20 mysql_query($sql,$link);
21 //判断是否成功
22 if(mysql_insert_id($link)>0){
23 echo"添加成功";
24 }else{
25 echo"添加失败";
26 }
27 echo"<br><a href='add.php'>继续添加</a>";
28 break;
29 case "del":
30 //获取要删除的id号,拼装sql语句
31 $id=$_GET['id'];
32 $sql="delete from type where id={$id} or path like '%,{$id},%'";
33 //执行删除
34 mysql_query($sql,$link);
35 //输出删除的行数
36 echo"成功删除".mysql_affected_rows($link)."行";
37 break;
38 }
39 //4.关闭连接
40 mysql_close($link);
1 -------------------------select.php--------------------------------
2 <html>
3 <head>
4 <title>无限分类信息管理</title>
5 </head>
6 <body>
7 <center>
8 <?php
9 include("menu.php");
10 ?>
11 <h3>浏览商品类别信息</h3>
12 <select name="typeid">
13 <?php
14 //1. 导入配置信息
15 require("dbconfig.php");
16 //2.获取数据库的连接
17 $link=@mysql_connect(HOST,USER,PASS)or die("数据库连接失败");
18 mysql_select_db(DBNAME,$link);
19 mysql_query("set names utf8");
20 //3.实现数据的查询
21 $sql="select *from type order by concat(path,id)";
22 $result=mysql_query($sql,$link);
23 //4.遍历解析输出内容
24 while($row=mysql_fetch_array($result)){
25 $m=substr_count($row['path'],",")-1;
26 $strpad=str_pad("",$m*6*2," ");
27 if($row['pid']==0){
28 $dbd="disabled";
29 }else{
30 $dbd="";
31 }
32 echo " <option {$dbd} value='{$row['id']}'>{$strpad}{$row['name']}</option>";
33 }
34 //5.释放结果集 关闭数据库连接
35 mysql_free_result($result);
36 mysql_close($link);
37 ?>
38 </select>
39 </center>
40 </body>
41 </html>