Infinite classification program code in php_PHP tutorial
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);
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