无限分类就像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();//$getID即为最后一条记录的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
- //$sql = "select * from `class` order by sort asc, id Desc";
- //$sql="select * from class where pid=0";
- //$result =mysql_query($sql);
- //while($row = mysql_fetch_array($result)){
- //$class_arr[] = array($row['id'],$row['name'],$row['classid'],$row['sort']);
- // echo $row['name'].$row['id'].'---'.$row['pid'].'<br>';
- sorttree(0);
- //}
- function sorttree($id){
- //if()
- $sql="select * from class where pid=".$id;
- $result =mysql_query($sql);
- //$a=mysql_fetch_array($result);
- //$row = mysql_fetch_array($result);
- while($row = mysql_fetch_array($result)){
- $rid=explode(",",$row['depth']);
- $i=count($rid);
- $n = str_pad('',$i,'-',STR_PAD_RIGHT);
- $n = str_replace("-"," ",$n);
- //print_r($row).'<br>';
- if($row['pid']==0){
- echo $n.'|-----'.$row['name'].'-----|<br>';
- }else{
- echo $n.' <font color="#FF0000">|-</font>'.$row['name'].'<br>';
- }
- sorttree($row['id']);
- }
- }
- function sort_s($id){
- //if()
- $sql="select * from class where pid=".$id;
- $result =mysql_query($sql);
- //$a=mysql_fetch_array($result);
- //$row = mysql_fetch_array($result);
- while($row = mysql_fetch_array($result)){
- $rid=explode(",",$row['depth']);
- $i=count($rid);
- $n = str_pad('',$i,'-',STR_PAD_RIGHT);
- $n = str_replace("-"," ",$n);
- //print_r($row).'<br>';
- if($row['pid']==0){
- echo "<option value="".$row['id']."" style='background:#ccc' >".$n.'|------'.$row['name']."-----|</option>rn";
- }else{
- echo "<option value="".$row['id']."">".$n.' |- '.$row['name']."</option>rn";
- }
- sort_s($row['id']);
- }
- }
- ?>
总结原理:如何把它的各个分类一一列出来呢?
首先我们假设有这样的一个三级分类,新闻→PHP新闻→PHP6.0出来了。
如果我们要查找“PHP6.0出来了”这条新闻,我们先点击新闻,然后再点击PHP新闻
新闻热点
疑难解答