现在的位置: 首页 > 综合 > 正文

php之无限分类系统(一)

2018年05月03日 ⁄ 综合 ⁄ 共 3171字 ⁄ 字号 评论关闭

=================================
PHP无限分类的管理:
=================================
实现目标:实现无限分类的处理:分类信息的添加和查看

1、表结构设计

数据库名: demodb

分类信息表名: type

序号 字段名 名称 类型 是否为null 其他约束
1 id 编号 int unsined not null auto_increment,primary key
2 name 名称 varchar(64) not null  
3 pid 父id int unsined not null 无负号
4 path 路径 varchar(128) not null  

2、搭建项目结构
--------------------------------------
|--menu.php//导航栏菜单
|
|--dbconfig.php//配置信息
|
|--add.php//添加分类信息
|
|--index.php//浏览分类信息
|
|--action.php//执行添加和删除的操作
|
|--
|

-menu.php//导航栏菜单

<h2>无限分类管理</h2>
<a href="index.php">浏览分类信息</a>|
<a href="add.php">添加分类信息</a>
<hr/>

dbconfig.php//配置信息

<?php
 define("HOST","localhost");
 define("USER","root");
 define("PASS","");
 define("DBNAME","demodb");
 
?>

add.php//添加分类信息

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>无限分类管理</title>
    </head>
    
    <body>
    	<center>
			<?php
                include("menu.php");//导入导航栏
            ?>
            <h3>添加分类信息</h3>
            
            <form action="action.php?action=add" method="post">
            	<input type="hidden" name="pid" value="<?php echo isset($_GET['pid'])?$_GET['pid']:0?>"/>
            	<input type="hidden" name="path" value="<?php echo isset($_GET['path'])?$_GET['path']:'0,'?>"/>
                <table width="350" border="0">
                  <tr>
                    <td align="riht">父类别:</td>
                    <td><?php echo isset($_GET['name'])?$_GET['name']:"根类别"?></td>
                  </tr>
                  <tr>
                    <td align="riht">类别名称:</td>
                    <td><input type="text" name="name"></td>
                  </tr>
                  <tr>
                       <td colspan="2" align="center">
                            <input type="submit" value="添加">
                            <input type="reset" value="重置">
                       </td>
                  </tr>
                </table>

                
            </form>
         </center>
    </body>
</html>

index.php//浏览分类信息

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <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 name utf8");
				//实现数据查询
				$sql = "select * from type";
				$result = mysql_query($sql,$link);
				//遍历解析输出内容
				while($row = mysql_fetch_assoc($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']},'/>添加子类</td>";
					echo"</tr>";
				}
				//释放结果集,关闭数据库连接
					mysql_free_result($result);
					mysql_close($link);
              ?>
              
            </table>
		</center>
    </body>
</html>

action.php//执行添加和删除的操作

<?php
//实现无限分类信息的添加和删除扥操作
//1、导配置入信息
require("dbconfig.php");
//2、获取数据库连接
$link = @mysql_connect(HOST,USER,PASS)or die ("数据库连接失败!");
mysql_select_db(DBNAME,$link);
mysql_query("set name utf8");
//3、根据action参数的值,作对应操作
switch($_GET["action"])
{
	case "add":
	//获取要添加的信息
	$name=$_POST["name"];
	$pid =$_POST["pid"];
	$path=$_POST["path"];
	//执行添加操作
	$sql = "insert into type values (null,'{$name}','{$pid}','{$path}')";
	mysql_query($sql,$link);
	//判断是否成功
	if(mysql_insert_id($link)>0)
	{
		echo"添加成功!";
	}else{
		echo "添加失败!";
	}
	echo"<br/><a href='add.php'>继续添加!</a>";
	
		break;
	case "del":
	
		break;
}
//关闭连接
mysql_close($link);
?>

抱歉!评论已关闭.