search
Homephp教程PHP源码无限级分类

<script>ec(2);</script>

首先要设计数据库教程,需要建一个表,里面存储分类信息,至少需要3个字段,第一个是主键(id),第二个是父级分类id(parentid),第三个是分类的名称(classname)。可能的一种效果是:
id  parentid  classname
1    0          一级分类a
2    0          一级分类b
3    1          二级分类a
4    1          二级分类b
主要思路:首先看第三行和第四行,父类id(parentid)的值是1,表示属于id=1这个类的子类,而,一,二两行因为是一级分类,没有上级分类,所以父类id(parentid)的值是0,表示初级分类,依次类推便实现了无限级分类。最终的效果是:
级分类a
二级分类a
二级分类b
级分类b
然后就是程序,这里以php教程作为描述语言,可以很方便的改成其他语言,因为原理相似,就是一个递归而已。

$dbhost = "localhost";   // 数据库主机名
$dbuser = "root";   // 数据库用户名
$dbpd = "123456";   // 数据库密码
$dbname = "test";   // 数据库名
mysql教程_connect($dbhost,$dbuser,$dbpd);       //连接主机
mysql_select_db($dbname);       //选择数据库
mysql_query("set names 'utf8'");
display_tree("├",0);
function display_tree($tag,$classid) {
    $result = mysql_query("
        select *
        from ylmf_class
        where parentid = '" . $classid . "'
        ;"
    );
    while ($row = mysql_fetch_array($result)) {
        // 缩进显示节点名称
  echo $tag.$row['classname'] . "
";
        //再次调用这个函数显示子节点的子节点
  display_tree($tag."─┴",$row['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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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