search
Homephp教程php手册php无限分类使用concat如何实现,php无限分类concat

php无限分类使用concat如何实现,php无限分类concat

一、数据库设计

--  
-- 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');  

这里说明下,catpath的-链接符号不是固定的,可以选择,;等特殊符号。

二、PHP代码实现

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

MySQL concat函数可以连接一个或者多个字符串

select concat('颜','培','攀')
select `id`,`name`,concat(`id`,'-',`name`) as iname

以上就是本文的全部内容,介绍了php使用concat实现无线分类,希望对大家的学习有所帮助。

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

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools