Home  >  Article  >  Backend Development  >  Unlimited Classification-Summary_PHP Tutorial

Unlimited Classification-Summary_PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:01937browse

PHP code part:

//Unlimited classification-use path
//Principle: Split through the path to get all the pids and ids you want. The path is a field when designing the table, including a series of ids from the ancestor id to the parent id
//This method, regardless of viewing or deletion or other operations, only needs to execute the SQL statement once to achieve the desired effect
//This method is also what I prefer to use, because my principle is that if I can do without recursion, I will definitely not use recursion
$link=mysql_connect('localhost','username,'password');
if(mysql_errno()){
echo 'Database connection failed:'.mysql_error();
}
mysql_select_db('db_kind');
mysql_set_charset('utf8');
//concat(), concatenate strings into a new string
//The following code completes an infinite classification of reality
$sql="select concat(path,'-',id) as conpath,id,name,path from wx_kind order by conpath";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
$arr=array();
//Here you can choose to split the string on the path field or the newly formed conpath field
                                //One thing to note about the explode() function, no matter whether the string to be split contains characters using parameters, it will return an array containing at least one element
$count=count(explode('-',$row['path']))-1;
//str_repeat(), repeats the string, mainly to make the format clearer
$str=str_repeat(' ',$count);
echo $str.$id.'=>'.$row['name'].'
';
}
/***********************************
If you need to delete it now, use the same path
For example, now we want to delete the Thunder category
First find the path based on its id
**************************************/
/* $sql1="select id,path from wx_kind where id=20";
$result1=mysql_query($sql1);
$row=mysql_fetch_assoc($result1);
$id=$row['id'];
$path=$row['path'];
//Construct all paths with this category as the parent or ancestor category
$new_path=$path.'-'.$id;
//The deletion action can be performed below
$sql2="delete from wx_kind where id={$id} or path like '{$new_path}%'";
$result2=mysql_query($sql2);
if($result2 && mysql_affected_rows()){
echo 'Delete successfully! ';
}else{
echo 'Deletion failed! ';
} */
/*****************************************************/
//The method of using recursive functions, I won’t explain so much, just look at the code, the main thing is that the search method is different
function display_classify($pid=0,$num=0){
$sql="select id,name from wx_kind where pid={$pid}";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
$id=$row['id'];
$str=str_repeat(' ',$num);
echo $str.$id.'=>'.$row['name'].'
';
                    display_classify($id,$num+1,$sid);
      }
}
//display_classify();
/*****************************************************/
//The following is the deletion method. Take your time and take a look. The main thing is the order of deletion mentioned below, and pay attention to where you should delete it.
function del_classify($id){
                                                                                                                                                                                                               
$sql="select id,name from wx_kind where pid={$id}";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
$id=$row['id'];
del_classify($id);
      }
//Delete operation is executed outside the loop
$sql1="delete from wx_kind where id={$id}";
//Delete directly without any prompt
$result=mysql_query($sql1);
if(!($result && mysql_affected_rows())){
$bool=false;
      }else{
$bool=true;
      }
return $bool;
}
//del_classify(5);
The following is the database code:
--
-- Database: `db_kind`
--
-------------------------------------------------- ----------
--
-- Table structure `wx_kind_dump`
--
CREATE TABLE `wx_kind_dump` (
`id` int(11) NOT NULL auto_increment,
`pid` int(11) NOT NULL,
`name` char(40) NOT NULL,
`path` char(40) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=26;
--
-- Export the data in the table `wx_kind_dump`
--
INSERT INTO `wx_kind_dump` VALUES (1, 0, 'News', '0');
INSERT INTO `wx_kind_dump` VALUES (2, 0, 'Video', '0');
INSERT INTO `wx_kind_dump` VALUES (3, 0, 'picture', '0');
INSERT INTO `wx_kind_dump` VALUES (4, 0, 'Reading', '0');
INSERT INTO `wx_kind_dump` VALUES (5, 1, 'Political News', '0-1');
INSERT INTO `wx_kind_dump` VALUES (6, 1, 'Financial News', '0-1');
INSERT INTO `wx_kind_dump` VALUES (7, 1, 'Entertainment News', '0-1');
INSERT INTO `wx_kind_dump` VALUES (8, 1, 'Sports News', '0-1');
INSERT INTO `wx_kind_dump` VALUES (9, 8, 'Basketball', '0-1-8');
INSERT INTO `wx_kind_dump` VALUES (10, 8, 'football', '0-1-8');
    INSERT INTO `wx_kind_dump` VALUES (11, 8, 'F1', '0-1-8');
    INSERT INTO `wx_kind_dump` VALUES (12, 8, '网球', '0-1-8');
    INSERT INTO `wx_kind_dump` VALUES (13, 9, '国际篮球', '0-1-8-9');
    INSERT INTO `wx_kind_dump` VALUES (14, 9, 'CBA', '0-1-8-9');
    INSERT INTO `wx_kind_dump` VALUES (15, 9, 'CUBA', '0-1-8-9');
    INSERT INTO `wx_kind_dump` VALUES (16, 9, 'NBA', '0-1-8-9');
    INSERT INTO `wx_kind_dump` VALUES (17, 9, 'NCAA', '0-1-8-9');
    INSERT INTO `wx_kind_dump` VALUES (18, 16, '热火', '0-1-8-9-16');
    INSERT INTO `wx_kind_dump` VALUES (19, 16, '湖人', '0-1-8-9-16');
    INSERT INTO `wx_kind_dump` VALUES (20, 16, '雷霆', '0-1-8-9-16');
    INSERT INTO `wx_kind_dump` VALUES (21, 16, '凯尔特人', '0-1-8-9-16');
    INSERT INTO `wx_kind_dump` VALUES (22, 18, 'James', '0-1-8-9-16-18');
    INSERT INTO `wx_kind_dump` VALUES (23, 18, 'Wade', '0-1-8-9-16-18');
    INSERT INTO `wx_kind_dump` VALUES (24, 20, 'KD', '0-1-8-9-16-20');
    INSERT INTO `wx_kind_dump` VALUES (25, 20, 'WS', '0-1-8-9-16-20');

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477826.htmlTechArticlePHP代码部分: //无限分类-使用路径 //原理:通过路径来进行拆分得到所有想得到的pid以及id,路径是在设计表时的一个字段,包含着从祖先...
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