無限級分類、、、但是效率不是最好的。。。大家有沒有更快更好的效率的代碼呢?
用遞歸等到類別多的時候就會影響效率了!
求分享!
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
無限級分類 分類列表 添加分類 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']); } ?> 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){ ?> 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']); } ?> 分類名稱 排序 操作 ".$n."|----".$class_arr[$i][1]."\n"; }else{ echo "\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; } } ?>