php+mysql无限级分类的代码

无限级分类的代码,把文件保存为相应的文件名,放在同一目录下即可看效果
有数据库连接的地方,改成你自己的,我只是做测试,没整成把数据连接的地方提出来放到一个地方
另外文件编辑搞中utf-8要不会有乱码
主要是用的递归
class.php
======================================================
<?php
//无限级分类,数据表结构
/**
* CREATE TABLE `new` (
* `id` int(11) NOT NULL auto_increment,
* `parentid` int(11) NOT NULL,
* `name` varchar(20) NOT NULL,
* PRIMARY KEY (`id`)
)
*/
class nolimit{
    function __construct(){
       
    }
   
    /**
     * print_list用于生成一个管理菜单,用于添加,删除子类
     *
     * $parents 父数组
     * $subids 所有arr[父id] => 子id
     * $name 键与名子的对应arr[id]=$name
     *
     * $sql= 'select * from new order by name DESC';
     * mysql_query('set names gbk');
     * $result = mysql_query($sql);
     * while($row = mysql_fetch_object($result)){
     * $subid[$row->parentid][] = $row->id;
     * $name[$row->id] = $row->name;}
     * print_list($subid[0],$subid,$name);
     *
     *   当其用于查找时,代码
     *   echo "<ul>";
     *   echo $name[$parentid];
     *   echo "<a href=/"classadd.php?parentid=$parentid/">[添加]</a>/n";
     *   echo "<a href=/"classdel.php?arentid=$parentid/">[删除]</a>/n";
     *   echo "<a href=/"find.php?parentid=$parentid/">[查找]</a></li>/n";
     *   print_list($subid[$parentid],$subid,$name);
     *   echo "</ul>";
     */
    function print_list($parents,$subids,$name){
        $list = '<ul>';
        foreach ($parents as $parent){
             $list.= "<li>".$name[$parent];
             $list.= "<a href=/"classadd.php?parentid=$parent/">[添加]</a>/n";
             $list.= "<a href=/"classdel.php?parentid=$parent/">[删除]</a>/n";
             $list.= "<a href=/"find.php?parentid=$parent/">[查找]</a></li>/n";
             if(array_key_exists($parent,$subids)){
             $sublist=$this->print_list($subids[$parent],$subids,$name);
             $list.=$sublist;
             }
        }
         $list.= "</ul>/n";
         return $list;
     }
    
     /**
      * print_option用于生成选择列表
      *
      *$parents 同上
      * $subids 同上
      * $name   同上
      * $indent 用于控制缩进
      * print_option($subid[0],$subid,$name);
      */
    
     function print_option($parents,$subids,$name,$indent=0){
        foreach($parents as $parent ){
        $content.= "<option value='$parent'>".str_repeat('&nbsp;&nbsp;',$indent).$name[$parent]."</option>/n";
        if(key_exists($parent,$subids)){
            $subcontent=$this->print_option($subids[$parent],$subids,$name,$indent+1);
            $content.=$subcontent;
            }
        }
        return $content;
    }
    
    /**
      * getsubid用于得到一个父id下的所有id,主要是用于删除
      * $parentid=$_GET['parentid']
      * $a=getsubid($parentid,$subid);
      */

    function getsubid($parentid,$subids){
        $list.=$parentid;
        if(array_key_exists($parentid,$subids)){
         foreach ($subids[$parentid] as $parent) {
              $listsub=", ".$this->getsubid($parent,$subids);   
              $list.=$listsub;  
         }
        }
        return $list;
    }
  
    /**
     * listurl用于生成导航条
     * $sql="select * from new order by name";
     * mysql_query('set names gbk');
     * $result = mysql_query($sql);
     * while($row = mysql_fetch_object($result)){
     * $parents[$row->id] = $row->parentid;
     * $name[$row->id] = $row->name;}
     * $id=$_GET['id'];
     * $listurl=listurl($id,$parents,$name);
    */
     function listurl($id,$parent,$name){
         $tmp="<a href='find.php?parentid=$id'>$name[$id]</>";
        while($parent[$id]!=0){
             $id=$parent[$id];
             $tmp="<a href='find.php?parentid=$id'>$name[$id]</a>"."&rarr;".$tmp;
       }
       return $tmp;
}

}
?>
======================================================
mysql.php
======================================================
<?php
//此文件用于生成数组
$link = mysql_connect('localhost','root','');
mysql_select_db('nolimit',$link);
$sql= 'select * from new order by name DESC';
mysql_query('set names gbk');
$result = mysql_query($sql);
while($row = mysql_fetch_object($result)){
     $parentid[$row->id] = $row->parentid;//用于导航
     $subid[$row->parentid][] = $row->id;   //用于列出,查找
     $name[$row->id] = $row->name;          //id与名子的对应
}
?>
======================================================
index.php
======================================================
<?php
require_once('mysql.php');
require_once('class.php');
/*
while($row = mysql_fetch_object($result)){
     $parentid[$row->id][] = $row->parentid;//用于导航
     $subid[$row->parentid][] = $row->id;   //用于列出,查找
     $name[$row->id] = $row->name;          //id与名子的对应
}
*/
$nolimit = new nolimit();
//生成管理菜单
echo $nolimit->print_list($subid[0],$subid,$name);
//生成下拉列表
echo '<select>';
echo $nolimit->print_option($subid[0],$subid,$name);
echo '</select>';
?>
======================================================
classadd.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>
</head>

<body>
<form id="form1" name="form1" method="post" action="classaddcfm.php">

<p>输入子类名称:
    <input type="text" name="name">
    <input type="hidden" name="parentid" value="<?=$_GET['parentid']?>" />
</p>
<p>
   
   <input type="submit" name="Submit" value="提交" />
   
</p>
</form>
</body>
</html>
=====================================================
classaddcfm.php
=====================================================
<?php
print_r($_POST);
$link = mysql_connect('localhost','root','');
mysql_select_db('nolimit',$link);
mysql_query('set names gbk');
$name=$_POST['name'];
$parentid=$_POST['parentid'];
$sql="insert new values (NULL,'$parentid','$name')";
$result=mysql_query($sql);
if($result){
alertjs('子类已经添加');
successBack('index.php');
}else{
alertjs('添加失败');
failureBack();  
}

function alertjs($str){
    echo "<script>alert('$str')</script>";
}
function failureBack(){
    echo "<script>history.go(-1)</script>";
    exit;
}
function successBack($url){
    echo "<meta http-equiv=/"refresh/" content=/"1;URL=$url/">";
    exit;
}
?>
==========================================================
classdel.php
==========================================================
<?php
require_once('mysql.php');
/*
while($row = mysql_fetch_object($result)){
     $parentid[$row->id][] = $row->parentid;//用于导航
     $subid[$row->parentid][] = $row->id;   //用于列出,查找
     $name[$row->id] = $row->name;          //id与名子的对应
}
*/
require_once('class.php');
$nolimit=new nolimit();
$id=$_GET['parentid'];
$in = $nolimit->getsubid($id,$subid);
$sql="delete from new where id in ($in)";
$result=mysql_query($sql);

if($result){
alertjs('delete');
successBack('index.php');
}else{
alertjs('not delete');
failureBack();  
}

function alertjs($str){
    echo "<script>alert('$str')</script>";
}
function failureBack(){
    echo "<script>history.go(-1)</script>";
    exit;
}
function successBack($url){
    echo "<meta http-equiv=/"refresh/" content=/"1;URL=$url/">";
    exit;
}
?>
===========================================================
find.php
===========================================================
<?php
/*
while($row = mysql_fetch_object($result)){
     $parentid[$row->id] = $row->parentid;//用于导航
     $subid[$row->parentid][] = $row->id;   //用于列出,查找
     $name[$row->id] = $row->name;          //id与名子的对应
}
*/
require_once('mysql.php');
require_once('class.php');
$nolimit=new nolimit();
$id=$_GET['parentid'];
echo $nolimit->listurl($id,$parentid,$name);
echo '<br />';
echo "<ul>";
echo $name[$id];
echo "<a href=/"classadd.php?parentid=$id/">[添加]</a>/n";
echo "<a href=/"classdel.php?parentid=$id/">[删除]</a>/n";
echo "<a href=/"find.php?parentid=$id/">[查找]</a></li>/n";
echo $nolimit->print_list($subid[$id],$subid,$name);
echo "</ul>";
?>
============================================================
sql
============================================================
--
-- 表的结构 `new`
--

CREATE TABLE `new` (
`id` int(11) NOT NULL auto_increment,
`parentid` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=35 ;

--
-- 导出表中的数据 `new`
--

INSERT INTO `new` (`id`, `parentid`, `name`) VALUES
(1, 0, 'ROOT');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值