As for functions such as adding and deleting, I won’t write much about them! If you look carefully, you will know how to use them. The difficulty is the display I hope the experts can expand it! This is the class
The code is as follows:
<?php /*======================================================== 类名:catalog 功能:无限分级类 方法: 树形显示分类 catalog_show($id) //参数$id 递归调用 流程:找到父分类为0所有根分类-> 一直递归取得所有分类并显示 添加分类 catalog_add($uid,$name) //$uid 父id //$name 分类名 流程:依据$uid,在此id下添加一个新子id 删除分类 catalog_del($uid)//参数 $uid 数要删除的分类 修改分类 catalog_set($id,$name) //参数 $id 要修改的分类 //参数 $name 新的分类名 变量: $config //数据库信息-> host,user,pass,dbname $catalog_dbname //分类数据库名 数据库: catalog_id //分类的自然序号 catalog_uid //分类的父分类 catalog_name //分类名 catalog_path_number //亲缘树数字形式 0:1:2 catalog_path_char //亲缘树字符形式 分类1:分类1.1:分类1.1.1 ========================================================*/ class catalog{ var $config; var $catalog_dbname; var $links; private function connect(){ $this->links = mysql_connect($this->config['host'],$this->config['user'],$this->config['pass']) or die("错误: 第".LINE."行<br>".mysql_error()); mysql_select_db($this->config['dbname'],$this->links); mysql_query("SET NAMES gb2312"); } function catalog_show($uid = 0){ $this->connect(); $sql = "Select * FROM ".$this->catalog_dbname. " Where catalog_uid = ". $uid ." orDER BY catalog_id "; $result = mysql_query($sql,$this->links) or die("错误: 第".LINE."行<br>".mysql_error()); if(mysql_num_rows($result) > 0){ while ($row = mysql_fetch_assoc($result)){ if($this->sun_catalog($row['catalog_id'])){//判断有没有子分类 $cata_img = "<img id = 'img".$row['catalog_id']."' src='./img/last_node.jpg' ōnclick='click_catalog(".$row['catalog_id'].")'/ alt="PHP infinite classification and sorting method using recursion" >"; }else{ $cata_img = "<img src='./img/sp.jpg'/ alt="PHP infinite classification and sorting method using recursion" >"; } $path = explode(":",$row['catalog_path_number']); if(count($path) > 1){ for($i=1;$i<count($path);$i++){ $path_img .= "<img src='./img/sp.jpg'/ alt="PHP infinite classification and sorting method using recursion" >"; } } echo $path_img.$cata_img; echo "<a class='menu' href = 'javascrīpt:send_id(".$row['catalog_id'].")'>"; echo $row['catalog_name']."</a><br>"; $path_img = ""; if($this->sun_catalog($row['catalog_id'])){ $hidden_p = "style='display:none'"; echo "<p id = 'p".$row['catalog_id']."' ".$hidden_p.">"; $this->catalog_show($row['catalog_id']); echo "</p>"; } } } } private function sun_catalog($uid){//判断是否有子分类 $sql = "Select * FROM ".$this->catalog_dbname. " Where catalog_uid = ". $uid ." orDER BY catalog_id "; $result = mysql_query($sql,$this->links) or die("错误: 第".LINE."行<br>".mysql_error()); if(mysql_num_rows($result) > 0){ return true; }else{ return false; } } function catalog_add($uid,$name){ //获取父id的亲缘树 $this->connect(); $sql = "Select * FROM ".$this->catalog_dbname." Where catalog_id = '".$uid."'"; $result = mysql_query($sql,$this->links) or die("错误: 第".LINE."行<br>".mysql_error()); $row = mysql_fetch_assoc($result); $fid_path_number = $row['catalog_path_number'];//id的数字亲缘树 $fid_path_char = $row['catalog_path_char'];//id的字符亲缘树 //插入数据 先插入行->再找到最新插入的id, 在依据这个id进行修改 $sql = "Insert INTO ".$this->catalog_dbname."(catalog_uid,catalog_name) VALUES(".$uid.",'".$name."')"; $result = mysql_query($sql,$this->links) or die("错误: 第".LINE."行<br>".mysql_error()); $catalog_id = mysql_insert_id();//获取自己的id $catalog_path_number = $fid_path_number.":".$catalog_id;//得到自己的数字亲缘数 $catalog_path_char = $fid_path_char.":".$name;//得到自己的字符亲缘数 $sql = "Update '".$this->catalog_dbname."' SET catalog_path_number = '".$catalog_path_number."', catalog_path_char = '".$catalog_path_char."' Where catalog_id = ".$catalog_id; mysql_query($sql,$this->links) or die("错误: 第".LINE."行<br>".mysql_error()); } function catalog_del($id){ $this->connect(); $sql = "Delete FROM ".$this->catalog_dbname." Where catalog_id = ".$id; mysql_query($sql,$this->links) or die("错误: 第".LINE."行<br>".mysql_error()); } function catalog_set($id,$name){ $this->connect(); $sql = "Update ".$this->catalog_dbname." SET catalog_name = '".$name."' Where catalog_id = ".$id; mysql_query($sql,$this->links) or die("错误: 第".LINE."行<br>".mysql_error()); } } ?>
The following mainly introduces the implementation method of infinite recursive classification and sorting in PHP. It is a very practical technique to realize infinite recursive classification and sorting through a simple recursive function. Friends in need can refer to
function order ($array,$pid=0){ $arr = array(); foreach($array as $v){ if($v['pid']==$pid){ $arr[] = $v; $arr = array_merge($arr,order($array,$v['id'])); } } return $arr; }
The above is the detailed content of PHP infinite classification and sorting method using recursion. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
