PHP unlimited classification example program_PHP tutorial
The principle of infinite classification: Just like creating a new folder under Windows, you can create a new folder under the newly created folder, and this will continue in an infinite loop. The same is true for infinite classification. The parent class can be divided into subclasses, and the subclasses can be divided into subclasses. Separate its subclasses and continue in an infinite loop
Example 1
The code is as follows | Copy code | ||||
1 => array('id'=>'1 ','parentid'=>0,'name'=>'First-level column one'), 2 => array('id'=>'2','parentid'=> 0,'name'=>'First-level column two'), 3 => array('id'=>'3','parentid'=>1,'name'=> 'Second-level column one'), 4 => array('id'=>'4','parentid'=>1,'name'=>'Second-level column two'), 5 => array('id'=>'5','parentid'=>2,'name'=>'Second-level column three'), 6 => array('id'=>'6','parentid'=>3,'name'=>'Third-level column one'), 7 => array('id'=> '7','parentid'=>3,'name'=>'Third-level column two'), 8 => array('id'=>'8','parentid'= >2,'name'=>'Second-level column three'), ); /** * Get the sub-ID of the current id * @param array $data original array * @param int $id current id * @param int $layer current level >*/ function genCate($data, $pid = 0, $level = 0) { if($level == 10) break; $l = str_repeat(" ", $ level); $l = $l.'└'; static $arrcat = array(); $arrcat = empty($level) ? array() : $arrcat; foreach ($ data as $ k = & gt; $ row) { /** * If the parent ID is the currently passed in id * / if ($ row ['paintid'] == $ pid ) 'level'] = $level; $arrcat[] = $row; ], $level+1);//Recursive call ); echo ""; |
Note: Because it is an infinite call, I added a judgment to let it jump out when the level $level=10. No normal website would have more than 10 levels of
directory structure.
After executing the static variable, determine the current level. If the level is 0, it means that this is the highest level menu. You need to clear the $arrcate data and re-declare it
Example 2
The code is as follows | Copy code | ||||||||
CREATE TABLE `class` ( `id` int(11) NOT NULL auto_increment COMMENT 'classification id',
|
The code is as follows | Copy code |
< ?php <🎜><🎜 >header("Content-type:text/html;charset=utf-8"); <🎜><🎜>$db=new mysqli("localhost","root","","news_php100") ; <🎜 >//Instantiate a database connection. Before using this, be sure to load the mysqli class library, <🎜> or use mysql_connect to connect. <🎜><🎜>if(mysqli_connect_errno()){<🎜><🎜>echo "Link failed:".mysqli_connect_error();<🎜><🎜>exit(); } <🎜><🎜>$db- >query("set names utf8");$result=$db->query("select name from class where f_id=0"); //Find the category of f_id=0 , that is, search for each major category. while($row=$result->fetch_assoc()){echo $row['name']."< br>"; //This way, each The major categories are cycled out. }//Similarly we can loop out the subcategories of news. $result=$db->query("select * from class where f_id=1"); //Find the category of f_id=1, that is, find the subcategory of 'news'. while($row=$result->fetch_assoc()){echo $row['name'].""; //This will The subcategory of 'News' is cycled out. Note: only subcategories, excluding grandchild categories. } |
//Writing here, we will find a problem. If this classification is a 10-level classification, do we have to write
10 loops to cycle out each of its subcategories? If there are more levels of classification, it is obviously unrealistic to write like this.
//Then what’s the solution? We can write a recursive function, passing in f_id as a parameter, and
continuously looping through each f_id value, that is to say, looping out the subclasses of each f_id value.
//First we save the values of each category in a two-dimensional array, which is useful in the following recursive function.
The code is as follows | Copy code | ||||
|
The code is as follows | Copy Code |
/** /** /** /** / ** /** /**
$select>$spacer$name" ("$nstr = "$str";"); $selected = $this->have($sid,$id) ? 'selected' : ' '; function have($list, $item){ |
Note: There is no platform restriction, you only need to tell the id, parentid, name
The three unlimited classification codes summarized above have no platform restrictions, but only It can be used in php. We only need to understand the relationship between id, parentid and name.
http://www.bkjia.com/PHPjc/444632.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

The way to update ByBit exchanges varies by platform and device: Mobile: Check for updates and install in the app store. Desktop Client: Check for updates in the Help menu and install automatically. Web page: You need to manually access the official website for updates. Failure to update the exchange can lead to security vulnerabilities, functional limitations, compatibility issues and reduced transaction execution efficiency.

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

The official website entrance of the Coinsuper Exchange: https://www.coinsuper.com. The client download channels are: Windows client, macOS client, and mobile (iOS/Android). Registration requires an email, mobile phone number and password, and you need to complete real-name authentication before you can trade. The platform provides a variety of digital asset transactions, including Bitcoin, Ethereum, etc., with the transaction fee rate of 0.1% for both orders and acceptors. Security safeguards include cold wallet storage, dual-factor verification, anti-money laundering and anti-terrorism financing measures, and with security public
