Infinite classification,,,but the efficiency is not the best. . . Do you have any faster and more efficient code?
Using recursion to wait until there are many categories will affect efficiency!
Please share!
class.sql
-- -- 表的结构 `class` -- set names utf8; CREATE TABLE `class` ( `id` int(10) NOT NULL auto_increment, `name` varchar(250) character set utf8 default NULL, `classid` int(10) default NULL, `sort` int(10) not null default '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ; -- -- 导出表中的数据 `class` -- INSERT INTO `class` (`id`, `name`, `classid`, `sort`) VALUES (1, '中国', 0, 1), (2, '广西', 1, 1), (3, '桂林', 2, 2), (4, '广东', 1, 2), (5, '北京', 1, 3), (6, '东莞', 4, 10), (7, '南宁', 2, 10), (8, '阳朔', 3, 10), (9, '柳州', 2, 10), (10, '广州', 4, 10);
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无限级分类</title> <style type="text/css"> * { padding:0; margin:0; } body { font: 14px Arial, Helvetica, sans-serif; overflow-x: hidden; overflow-y:auto; color:#444444; } #main { width:600px; margin:20px auto; } #main table{ margin-top:8px; width:100%; border-collapse:collapse; border-spacing:0px; BACKGROUND-COLOR: #EFEFEF; border:0px; } #main table td{ line-height:20px; border:1px solid #FFF; padding:5px; } #main table thead td { background:#C6C6C6; FONT-SIZE:15px; text-align:center; line-height:23px; font-weight:bold; } .input { border-top: 1px inset; border-left: 1px inset; padding:2px 3px; } </style> </head> <body> <div id="main"> 分类列表 添加分类 <?php $mysql = new mysql_Class('localhost','root',''); $mysql -> select_db('test'); switch($_GET['action']){ case 'add': $class_arr=array(); $sql = "select * from `class` order by sort asc, id Desc"; $query = $mysql -> query($sql); while($row = $mysql -> fetch_array($query)){ $class_arr[] = array($row['id'],$row['name'],$row['classid'],$row['sort']); } ?> <?php break; case 'act_add': $sql = "INSERT INTO `class` (`name`,`classid`,`sort`) VALUES('".$_POST['name']; $sql .= "',".$_POST['classid'].",".$_POST['sort'].")"; $mysql -> query($sql); msg('添加成功!','http://blog.csdn.net/phpfenghuo/article/details/18733323?action='); break; case 'edit': $class_arr=array(); $sql = "select * from `class` order by sort asc, id Desc"; $query = $mysql -> query($sql); while($row = $mysql -> fetch_array($query)){ $class_arr[] = array($row['id'],$row['name'],$row['classid'],$row['sort']); } $sql = "select * from `class` where id=".$_GET['id']; $query = $mysql -> query($sql); $row = $mysql -> fetch_array($query); if($row){ ?> <?php }else{ msg('要修改的记录不存在!','http://blog.csdn.net/phpfenghuo/article/details/18733323?action='); } break; case 'act_edit': $sql = "select id from `class` where id=".$_POST['id']; $query = $mysql -> query($sql); $row = $mysql -> fetch_array($query); if($row){ if($row['id']==$_POST['classid']){ msg('修改失败,不能自己是自己的子分类!','http://blog.csdn.net/phpfenghuo/article/details/18733323?action='); }else{ $sql = "update `class` set `name`='".$_POST['name']."',`classid`=".$_POST['classid']; $sql .= ",`sort`=".$_POST['sort']." where `id`=".$_POST['id']; $mysql -> query($sql); msg('修改成功!','http://blog.csdn.net/phpfenghuo/article/details/18733323?action='); } } break; case 'del': $sql = "select * from `class` where id=".$_GET['id']; $query = $mysql -> query($sql); $row = $mysql -> fetch_array($query); if($row){ $mysql -> query("delete from `class` where id=".$_GET['id']); msg('删除成功!','http://blog.csdn.net/phpfenghuo/article/details/18733323?action='); }else{ msg('记录不存在!','http://blog.csdn.net/phpfenghuo/article/details/18733323?action='); } break; case '': $class_arr=array(); $sql = "select * from `class` order by sort asc, id Desc"; $query = $mysql -> query($sql); while($row = $mysql -> fetch_array($query)){ $class_arr[] = array($row['id'],$row['name'],$row['classid'],$row['sort']); } ?> <table class="table"> <thead> <tr> <td >分类名称</td> <td width="60"><div align="center">排序</div></td> <td width="80"><div align="center">操作</div></td> </tr> </thead> <?php infinite_arr(0,0);?> </table> <?php break; } ?> </div> </body> </html> <?php function msg($msg,$url) { echo "<script type=\"text/javascript\">alert('$msg');window.location.href='$url';</script>"; } function infinite_arr($m,$id) { global $class_arr; global $classid; global $mysql; if($id=="") $id=0; $n = str_pad('',$m,'-',STR_PAD_RIGHT); $n = str_replace("-"," ",$n); for($i=0;$i<count($class_arr);$i++){ if($class_arr[$i][2]==$id){ echo "<tr>\n"; echo "<td>".$n."|----".$class_arr[$i][1]."</td>\n"; echo " <td><div align=\"center\">".$class_arr[$i][3]."</div></td>\n"; echo " <td><div align=\"center\">修改"; echo " 删除"; echo "</div></td>\n"; echo " </tr>\n"; infinite_arr($m+1,$class_arr[$i][0]); } } } function infinite_select($m,$id,$index) { global $class_arr; $n = str_pad('',$m,'-',STR_PAD_RIGHT); $n = str_replace("-"," ",$n); for($i=0;$i<count($class_arr);$i++){ if($class_arr[$i][2]==$id){ if($class_arr[$i][0]==$index){ echo "<option value=\"".$class_arr[$i][0]."\" selected=\"selected\">".$n."|----".$class_arr[$i][1]."</option>\n"; }else{ echo "<option value=\"".$class_arr[$i][0]."\">".$n."|----".$class_arr[$i][1]."</option>\n"; } infinite_select($m+1,$class_arr[$i][0],$index); } } } /** *-------------------------数据库操作类-----------------------------* */ class mySql_Class { function __construct($host, $user, $pass) { @mysql_connect($host,$user,$pass) or die("数据库连接失败!"); mysql_query("SET NAMES 'utf8'"); } function select_db($db)//连接表 { return @mysql_select_db($db); } function query($sql)//执行SQL语句 { return @mysql_query($sql); } function fetch_array($fetch_array) { return @mysql_fetch_array($fetch_array, MYSQL_ASSOC); } function close() //关闭数据库 { return @mysql_close(); } function insert($table,$arr) //添加记录 { $sql = $this -> query("INSERT INTO `$table` (`".implode('`,`', array_keys($arr))."`) VALUES('".implode("','", $arr)."')"); return $sql; } } ?>

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Alipay PHP...


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

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),

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

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.