php+mysql无限级分类
用递归等到类别多的时候就会影响效率了!
求分享!
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']); } ?>
分类名称 | 排序 |
操作 |
".$class_arr[$i][3]."
修改"; echo " 删除"; echo "
下一篇: 七牛云存储官方接口PHP版本