无限级分类、、、但是效率不是最好的。。。大家有没有更快更好的效率的代码呢?
用递归等到类别多的时候就会影响效率了!
求分享!
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"> <a href="?action=">分类列表</a> <a href="?action=add">添加分类</a>
<?php
$mysql = new mysql_Class('localhost','root','');
$mysql -> select_db('test');
switch($_GET['action']){
case 'add':
$class_arr=array();
$sql = "