


Share a PHP code to implement infinite classification program_PHP tutorial
Infinite classification is a problem encountered in all program development. Now I will introduce an infinite classification program implemented by php+mysql. Friends in need can refer to it.
Let me show you my database structure: The name of the database is: fa_category
Field | Type | Comment |
cid | int(11) | 分类id |
catename | varchar(255) | 分类名字 |
catetype | int(1) | 分类类型,1为单页面,2为普通分类 |
catdir | varchar(255) | 英文目录 |
display | int(1) | 1为显示,2为不显示 |
keywords | varchar(255) | 栏目关键字 |
description | text | 栏目描述 |
ctime | int(11) | 创建时间 |
parentid | int(11) | 父节点id,最高节点父节点为0 |
We use a parentid field to record the id of the parent node. If parentid is 0, it is root. Without further ado, let’s see how to write the code. The function we want to achieve is as shown in the picture:
How to display it like this? I have been thinking about this question for a long time. Let’s take a look at this SQL statement first,
The code is as follows | Copy code |
SELECT c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, c.show_in_nav, c.grade ,c.sort_order, COUNT( s.cat_id ) AS has_children |
Use a left join to connect a table and return a field has_children. This field records how many child nodes there are.
Let’s take a look at the code,
代码如下 | 复制代码 |
public function getCategory($catid=0,$re_type = true,$selected=0) { $db = new Public_DataBase(); $sql = 'select c.cid,c.catename,c.catetype,c.ctime,c.parentid,count(s.cid) as has_children from '. __MYSQL_PRE.'category as c left join '. __MYSQL_PRE.'category as s on s.parentid=c.cid group by c.cid order by c.parentid asc'; $res = $db->selectTable($sql); $cateInfo = self::getChildTree($catid,$res); if($re_type==true) { $select = ''; foreach($cateInfo as $val) { $select .= ' $select .= ($selected == $val['cid']) ? "selected='ture'" : ''; $select .= '>'; if($val['level']>0) { $select .= str_repeat(' ', $val['level'] * 4); } $select .= htmlspecialchars(addslashes($val['catename']), ENT_QUOTES) . ''; } return $select; } else { foreach($cateInfo as $key=>$val) { if($val['level']>0) { $cateInfo[$key]['catename'] = "|".str_repeat(' ', $val['level'] * 8)."└─".$val['catename']; } } return $cateInfo; } } /** * 通过父ID递归得到所有子节点树 * @param int $catid 上级分类 * @param array $arr 含有所有分类的数组 * @return array */ public function getChildTree($catid,$arr) { $level = $last_cat_id = 0; while (!empty($arr)) { foreach($arr as $key=>$value) { $cid = $value['cid']; if ($level == 0 && $last_cat_id == 0) { if ($value['parentid'] > 0) { break; } $options[$cid] = $value; $options[$cid]['level'] = $level; $options[$cid]['id'] = $cid; $options[$cid]['name'] = $value['catename']; unset($arr[$key]); if ($value['has_children'] == 0) { continue; } $last_cat_id = $cid; $cat_id_array = array($cid); $level_array[$last_cat_id] = ++$level; continue; } if ($value['parentid'] == $last_cat_id) { $options[$cid] = $value; $options[$cid]['level'] = $level; $options[$cid]['id'] = $cid; $options[$cid]['name'] = $value['catename']; unset($arr[$key]); if ($value['has_children'] > 0) { if (end($cat_id_array) != $last_cat_id) { $cat_id_array[] = $last_cat_id; } $last_cat_id = $cid; $cat_id_array[] = $cid; $level_array[$last_cat_id] = ++$level; } } elseif ($value['parentid'] > $last_cat_id) { break; } } $count = count($cat_id_array); if ($count > 1) { $last_cat_id = array_pop($cat_id_array); } elseif ($count == 1) { if ($last_cat_id != end($cat_id_array)) { $last_cat_id = end($cat_id_array); } else { $level = 0; $last_cat_id = 0; $cat_id_array = array(); continue; } } if ($last_cat_id && isset($level_array[$last_cat_id])) { $level = $level_array[$last_cat_id]; } else { $level = 0; } } return $options; |
}Use a smarty loop to display it. The effect is the same as the picture above! If you have any questions, please leave me a message.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

Article discusses type hinting in PHP, a feature for specifying expected data types in functions. Main issue is improving code quality and readability through type enforcement.

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

Article discusses creating and securing PHP APIs, detailing steps from endpoint definition to performance optimization using frameworks like Laravel and best security practices.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

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.

Atom editor mac version download
The most popular open source editor
