Home  >  Article  >  Backend Development  >  Infinite classification program code in php_PHP tutorial

Infinite classification program code in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:09914browse

The article is an original article by the author about the infinite classification implemented in php. Friends who need to know more can take a look.

Infinite classification is like creating a new folder under Windows. You can create a new folder under the newly created folder. This will continue in an infinite loop. The same is true for infinite classification. The parent category can be divided into subcategories, and the subcategories can be divided into subcategories. Separate its subcategories and continue in an infinite loop

The code is as follows Copy code

--
-- Database: `tree`
--
CREATE DATABASE `tree` DEFAULT CHARACTER SET gb2312 COLLATE gb2312_chinese_ci;
USE `tree`;


--
-- Table structure `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 ;


$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");
    }
}
?>


 
   
     
     
   
   
     
     
   
   
     
     
   
 
名称
     
栏目
 









//$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'].'
';
        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).'
';
    if($row['pid']==0){
       echo $n.'|-----'.$row['name'].'-----|
';
    }else{
       echo $n.'    |-'.$row['name'].'
';
       }
    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).'
';
    if($row['pid']==0){
        echo "rn";
    }else{
        echo "rn";
    }
    sort_s($row['id']);
 }
}
?>

总结原理

如何把它的各个分类一一列出来呢?
首先我们假设有这样的一个三级分类,新闻→PHP新闻→PHP6.0出来了。
如果我们要查找“PHP6.0出来了”这条新闻,我们先点击新闻,然后再点击PHP新闻

就可以查出来了,也就是说我们可以通过祖父类一级一级地往下找,反过来我们只要

知道一个子类的父类,就可以把它查找出来了。这样我们在设计数据库时就可以多设

计一个父类id的字段就可以实现无限分类的功能了

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631632.htmlTechArticle文章是作者原创的一篇关于在php中实现的无限级分类,有需要了解的朋友可以看看。 无限分类就像windows下新建一个文件夹,在新建的文件夹...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn