php中无限级分类程序代码
无限分类就像windows下新建一个文件夹,在新建的文件夹下又可以新建一个文件夹,这样无限循环下去,无限分类也是这样,父类可以分出它子类,子类又可以分出它的子类,这样一直无限循环下去
- --
- -- 数据库: `tree`
- --
- CREATE DATABASE `tree` DEFAULT CHARACTER SET gb2312 COLLATE gb2312_chinese_ci;
- USE `tree`;
-
-
- --
- -- 表的结构 `class`
- --
-
- CREATE TABLE `class` (
- `id` int(11) NOT NULL auto_increment,
- `name` varchar(10) NOT NULL,
- `pid` int(11) NOT NULL,
- `depth` varchar(100) default '0',
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=30 ;
-
-
- <?php
- $link =mysql_connect('localhost','root','root');
- mysql_select_db('tree');
- mysql_query ( 'set names GBK' );
- if($_GET['act']=='add'){
- $name=$_POST['name'];
- if($name=='') exit('name not null');
- $pid=$_POST['pid'];
- if($pid !=0){
- $sql="select * from class where id=".$pid;
- $result =mysql_query($sql);
- $row = mysql_fetch_array($result);
- $depth=$row['depth'].','.mysql_insert_id();
- }else{
- $depth=0;
- }
- $sql="INSERT INTO class(name,pid,depth) VALUES('".$name."','".$pid."','".$depth."')";
- $result =mysql_query($sql);
- if(!$result){
- exit("shibai $sql");
- }else{
- exit("chenggong");
- }
- }
- ?>
- <form id="form1" name="form1" method="post" action="?act=add">
- <table width="327" border="1" cellpadding="0" cellspacing="0">
- <tr>
- <td width="97" height="27">名称</td>
- <td width="224"><label for="name"></label>
- <input type="text" name="name" id="name" /></td>
- </tr>
- <tr>
- <td height="30">栏目</td>
- <td><select name="pid" id="pid">
- <option value="0">-----顶级分类-----</option>
- <?php
- sort_s(0);
- ?>
- </select></td>
- </tr>
- <tr>
- <td height="35"> </td>
- <td><input type="submit" name="button" id="button" value="提交" /></td>
- </tr>
- </table>
- </form>
- <br />
- <br />
- <br />
- <br />
- <?php
-
-
-
-
-
-
- sorttree(0);
-
-
-