search
HomeBackend DevelopmentPHP TutorialInfinite classification program code in php_PHP tutorial

Infinite classification program code in php_PHP tutorial

Jul 13, 2016 pm 04:56 PM
phpcodeauthoraboutClassificationOriginalexistaccomplisharticleunlimitedyeshaveofprogram

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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor