search
Homephp教程php手册PHP 无限分类三种方式 非函数的递归调用!

PHP 无限分类三种方式 非函数的递归调用!

Jun 13, 2016 pm 12:06 PM
phpfunctionClassificationdatabaseWayunlimitedofset uptransferrecursionpassNo

php无限分类大致有三种方式,

  1、数据库通过设置父类ID来进行唯一索引,然后使用函数的递归调用实现无限分类;

  2、数据库设计通过特定格式进行排列,然后使用mysql查询关键函数:concat。程序实现比较简单;

  3、第三种不是太了解, 好像要使用到算法和数据结构进行排列。

今天我主要分享下第二种方式,一开始也是找了很多资料,确实比较难理解。不过最终还是给搞明白了,因此记下随笔,希望通过这篇文章能够帮助到大家。

一、数据库设计:  


复制代码 代码如下:


--
-- Table structure for table `category`
--
CREATE TABLE IF NOT EXISTS `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` (`id`, `catpath`, `name`) VALUES
(1, '0', '网站首页'),
(2, '0-1', 'Linux OS'),
(3, '0-1', 'Apache服务器'),
(4, '0-1', 'MySQL数据库'),
(5, '0-1', 'PHP脚本语言'),
(6, '0-1-2', 'Linux 系统教程'),
(7, '0-1-2', 'Linux 网络技术'),
(8, '0-1-2', 'Linux 安全基础'),
(9, '0-1-2-7', 'Linux LAMP'),
(10, '0-1-3-10', 'apache Server');


这里说明下,catpath的-链接符号不是固定的,可以选择,;等特殊符号。
二、 PHP代码实现:

复制代码 代码如下:


$conn = mysql_connect ( 'localhost', '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'] . '
';*/
/**
* 第二种展示方法
*/
$space = str_repeat ( '    ', count ( explode ( '-', $row ['abspath'] ) ) - 1 );
$option .= '';
}
echo '';


上效果图:

    

这里有几个关键的地方需要注意下:
 1、在数据库查询字段是用了concat函数,不了解的地方可以google下。 
 2、第二个地方主要是用到了php中的str_repeat巧妙的设置了空格。
有错误之处,望mail: chenghuiyong1987@gmail.com或者留言

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.