Home >Backend Development >PHP Tutorial >PHP implements unlimited classification based on concat

PHP implements unlimited classification based on concat

墨辰丷
墨辰丷Original
2018-06-06 11:52:401565browse

This article mainly introduces how to implement unlimited classification in PHP using concat. Friends who need it can refer to it

1. Database design

--  
-- Table structure for table `category` 
--  
 
CREATE TABLE `category` ( 
 `id` int(11) NOT NULL auto_increment, 
 `catpath` varchar(255) default NULL, 
 `name` varchar(255) default NULL, 
 PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ; 
 
--  
-- Dumping data for table `category` 
--  
 
INSERT INTO `category` VALUES (1, '0', '网站首页'); 
INSERT INTO `category` VALUES (2, '0-1', 'Linux OS'); 
INSERT INTO `category` VALUES (3, '0-1', 'Apache服务器'); 
INSERT INTO `category` VALUES (4, '0-1', 'MySQL数据库'); 
INSERT INTO `category` VALUES (5, '0-1', 'PHP脚本语言'); 
INSERT INTO `category` VALUES (6, '0-1-2', 'Linux 系统教程'); 
INSERT INTO `category` VALUES (7, '0-1-2', 'Linux 网络技术'); 
INSERT INTO `category` VALUES (8, '0-1-2', 'Linux 安全基础'); 
INSERT INTO `category` VALUES (9, '0-1-2-7', 'Linux LAMP'); 
INSERT INTO `category` VALUES (10, '0-1-3-10', 'apache Server');

Let me explain here that the - link symbol of catpath is not fixed, you can choose special symbols such as;

2. PHP code implementation

<? 
$conn = mysql_connect ( &#39;localhost&#39;, &#39;root&#39;, &#39;root&#39; ); 
mysql_select_db ( &#39;test&#39;, $conn ); 
mysql_query ( &#39;set names UTF8&#39; ); 
$sql = "select id,concat(catpath,&#39;-&#39;,id) as abspath,name from category order by abspath"; 
$query = mysql_query ( $sql ); 
while ( $row = mysql_fetch_array ( $query ) )  
{ 
  //第一种展示方法 
  //$space = str_repeat ( &#39;    &#39;, count ( explode ( &#39;-&#39;, $row [&#39;abspath&#39;] ) ) - 1 ); 
  //echo $space . $row [&#39;name&#39;] . &#39;<br>&#39;;*/ 
   
  //第二种展示方法 
  $space = str_repeat ( &#39;    &#39;, count ( explode ( &#39;-&#39;, $row [&#39;abspath&#39;] ) ) - 1 ); 
  $option .= &#39;<option value="&#39; . $row [&#39;id&#39;] . &#39;">&#39; . $space . $row [&#39;name&#39;] . &#39;</option>&#39;; 
} 
echo &#39;<select name="opt">&#39; . $option . &#39;</select>&#39;; 
?>

MySQL concat function can connect one or more strings

select concat(&#39;颜&#39;,&#39;培&#39;,&#39;攀&#39;)
select `id`,`name`,concat(`id`,&#39;-&#39;,`name`) as iname

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php image zoom and cut function

How to write session to database in php

php method to compress zip files online

The above is the detailed content of PHP implements unlimited classification based on concat. For more information, please follow other related articles on the PHP Chinese website!

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