初始化的数据
//初始化原始数据(id=>编号,name=>显示名称,pid=>父级目录id,sort=>排序顺序)$data[1] = array('id'=>'1','name'=>'一级目录A','pid'=>'0','sort'=>'1')$data[2] = array('id'=>'2','name'=>'一级目录B','pid'=>'0','sort'=>'2')
$data[3] = array('id'=>'3','name'=>'一级目录C','pid'=>'0','sort'=>'3')
$data[4] = array('id'=>'4','name'=>'一级目录D','pid'=>'0','sort'=>'4')
$data[5] = array('id'=>'5','name'=>'二级目录A-1','pid'=>'1','sort'=>'1')
$data[6] = array('id'=>'6','name'=>'二级目录A-2','pid'=>'1','sort'=>'2')
$data[7] = array('id'=>'7','name'=>'二级目录A-3','pid'=>'1','sort'=>'3')
$data[8] = array('id'=>'8','name'=>'二级目录B-1','pid'=>'2','sort'=>'1')
$data[9] = array('id'=>'9','name'=>'二级目录B-2','pid'=>'2','sort'=>'2')
$data[10] = array('id'=>'10','name'=>'二级目录B-3','pid'=>'2','sort'=>'3')
$data[11] = array('id'=>'11','name'=>'二级目录C-1','pid'=>'3','sort'=>'2')
$data[12] = array('id'=>'12','name'=>'二级目录D-1','pid'=>'4','sort'=>'1')
$data[13] = array('id'=>'13','name'=>'二级目录D-2','pid'=>'4','sort'=>'2')
$data[14] = array('id'=>'14','name'=>'三级目录A-2-1','pid'=>'6','sort'=>'1')
$data[15] = array('id'=>'15','name'=>'三级目录A-2-2','pid'=>'6','sort'=>'2')
$data[16] = array('id'=>'16','name'=>'三级目录C-1-1','pid'=>'11','sort'=>'1')
$data[17] = array('id'=>'17','name'=>'三级目录B-2-1','pid'=>'9','sort'=>'2')
显示树结构
function printTree($data,$level=0){ foreach($data as $key=>$value){ for($i=0$i<=$level$i++){ echo '&emsp&emsp'} echo $value['name'] echo '<br>' if(!empty($value['children'])){
printTree($value['children'],$level+1)
}
}
}
function getNodeTree(&$list,&$tree,$pid=0){ foreach($list as $key=>$value){ if($pid == $value['pid']){$tree[$value['id']]=$value unset($list[$key])
getNodeTree($list,$tree[$value['id']]['children'],$value['id'])
}
}
}
使用引用-无限极分类
function createNodeTree(&$list,&$tree){ foreach($list as $key=>$node){ if(isset($list[$node['pid']])){$list[$node['pid']]['children'][] = &$list[$key]
}else{
$tree[] = &$list[$node['id']]
}
}
}
调用
//递归-无限极分类调用getNodeTree($data,$tree)printTree($tree)//使用引用-无限极分类调用createNodeTree($data,$tree)
printTree($tree)
/*** 递归实现无限极分类
* @param $array 分类数据
* @param $pid 父ID
* @param $level 分类级别
* @return $list 分好类的数组 直接遍历即可 $level可以用来遍历缩进
*/
function getTree($array, $pid =0, $level = 0){
//声明静态数组,避免递归调用时,多次声明导致数组覆盖
static $list = [] foreach ($array as $key => $value){ //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
if ($value['pid'] == $pid){ //父节点为根节点的节点,级别为0,也就是第一级
$value['level'] = $level //把数组放到list中
$list[] = $value //把这个节点从数组中移除,减少后续递归消耗
unset($array[$key]) //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
getTree($array, $value['id'], $level+1)
}
}
<?php/*
reader: 这是自己写的无限极分类实现方法 里面的编辑方法只是对分类名进行了编辑
没有进行移动 *** 作 小弟能力有限忘大家多多包涵啊
第一种方法:CREATE TABLE `types` (
`type_id` int(11) NOT NULL AUTO_INCREMENT,
`type_name` varchar(20) NOT NULL,
`type_p_id` varchar(64) NOT NULL DEFAULT '-',
PRIMARY KEY (`type_id`),
KEY `type_name` (`type_name`),
KEY `tname` (`type_name`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
注:
这里没做字段有效验证;
type_id 表示主键自增
type_name 表示分类名
type_p_id 表示分类路径 这里的分类路径是 上层父类的分类路径加上此类的主键id 用逗号隔开
*/
error_reporting(7)
header("Content:text/htmlcharset=utf-8")
//这里先进行 *** 作的判断
$arr = array('list','add','del','edit','addok','edit_ok')
$act= in_array($_GET['ac'],$arr)?$_GET['ac']:$arr[0]
//连接数据库
$conn = mysql_connect("localhost","root","root")or die('数据库连接失败')
mysql_select_db("study",$conn)
mysql_query("set names utf8")
//根据分类层进行排序
$sql = "select * from types order by type_p_id"
$sql1 = mysql_query($sql)
//添加分类
if($act == "addok"){
$type_id = $_POST['type_id']
$type_name = $_POST['type_name']
//如果等于0表示是跟目录
if($type_id=="0"){
$sql = "insert into types set type_name = '{$type_name}'"
$res = mysql_query($sql)
$id = mysql_insert_id()
$sql = "update types set type_p_id = '$id,' where type_id=$id"
$res = mysql_query($sql)
if( mysql_affected_rows ()>0){
echo "<script>location.href='ok.php?act=list'</script>"
}else{
echo "<script>alert('添加失败')</script>"
}
}//如果不等于0
else{
//根据typeid 找到 该id下的type_p_id
$sql = "select type_p_id from `types` where type_id = $type_id"
$res = mysql_query($sql)
$res = mysql_fetch_assoc($res)
$type_id = $res['type_p_id']
//先将名称插入进去
$sql = "insert into types set type_name = '{$type_name}'"
$res = mysql_query($sql)
//获取最后执行的id 然后进行数据更新 主要更新 type_p_id
$id = mysql_insert_id()
$sql = "update types set type_p_id = '$type_id$id,' where type_id=$id"
$res = mysql_query($sql)
if($res){
echo "<script>location.href='ok.php?act=list'</script>"
}else{
echo "<script>alert('添加失败')</script>"
}
}
}elseif($act=="add"){
?>
<form method="post" action="?ac=addok">
新分类名称:<input type="text" name="type_name"><br/>
当前分类:<select name="type_id" >
<option value="0">顶级分类</option>
<?php
//循环遍历分类
while($res = mysql_fetch_assoc($sql1)){
//查找 ","号出现的次数
$m=substr_count($res['type_p_id'],",")
//使用空格替换 "空格"
$strpad = str_pad("",$m*8*2," ")
if($m==1){
$sele = "disabled"
}else{
$sele = ""
}
?>
<option value="<?php echo $res['type_id']?>"><?php echo $strpad.$res['type_name']?></option>
<?php
}
?>
</select><br />
<input type="submit" value="添加"/>
</form>
<?php
}elseif($act == "list"){
//获取列表根据 分类层进行排序
$sql = "select * from types order by type_p_id"
$res = mysql_query($sql)
?>
<table width="50%" style="font-size:12pxmargin-left:20%">
<tr><td>id</td><td>分类名</td><td>path路径</td><td> *** 作</td></tr>
<?php
while($arr = mysql_fetch_assoc($res)){?>
<tr>
<td><?php echo $arr['type_id']?></td>
<td><?php echo $arr['type_name']?></td>
<td><?php echo $arr['type_p_id']?></td>
<td ><a href="?ac=edit&type_id=<?php echo $arr['type_id']?>">编辑</a>|
<a href="?ac=del&type_id=<?php echo $arr['type_id']?>">删除</a></td>
</tr>
<?php
}
?>
</table>
<input type="button" value="添加" onclick="(location.href='?ac=add')">
<?php
}elseif($act == "edit"){
$id = $_GET['type_id']
$sql = "select *from `types` where type_id=$id "
$res = mysql_query($sql)
echo "<form method='post' action='?ac=edit_ok'>"
while($arr = mysql_fetch_assoc($res)){
echo "当前名称:{$arr['type_name']}"."<br />"
echo "新名称:<input type='text' name='n_type_name' >"."<br />"
echo "<input type='hidden' value={$arr['type_id']} name='id'/>"
}
echo "<input type='submit' value='更新'>"
echo "</form>"
}elseif($act == "edit_ok"){
$name = trim($_POST['n_type_name'])
$id = $_POST['id']
if(!empty($name)){
$sql = "update `types` set type_name='$name' where type_id=$id"
mysql_query($sql)
echo "<script>location.href('ok.php?act=list')</script>"
}else{
echo "<script>location.href('ok.php?act=list')</script>"
}
}elseif($act == 'del'){
//这里的删除是要把当前分类 和当前的子分类都要删除 所以用到$id%
$id = $_GET['type_id']
$sql ="delete from `types` where type_p_id like '$id%' "
$res = mysql_query($sql)
if($res){
echo "<script>location.href('ok.php?act=list')</script>"
}else{
echo "<script>alert('删除失败')</script>"
}
}
?>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)