php分类管理,打造PHP的无限分级类(完整代码及注释)

这是一个PHP实现的商品类别无限级分类管理系统,包含添加、编辑、删除和保存操作。系统使用MySQL数据库,支持对分类的增删改查,并能处理复杂的分类层级关系,如限制最多20级分类,同时在编辑时检查所属类别合法性,避免错误操作导致的数据混乱。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WHB_PHP无限级分类

.STYLE1 {color: #FF0000}

body,td,th {

font-family: 宋体;

font-size: 12px;

}

.STYLE2 {

color: #FFFFFF;

font-size: 14px;

font-weight: bold;

}

-->

ini_set("error_reporting","E_ALL & ~E_NOTICE");//忽略所有警告信息

require_once("conn.php");//嵌入连接文件

@$ToDo=$_GET["ToDo"];

switch($ToDo)

{

case"add":

echoadd();

break;

case"edit":

echoedit();

break;

case"saveadd":

echosaveadd();

break;

case"saveedit":

echosaveedit();

break;

case"del":

echodel();

break;

default:

?>

商品类别设置 添加一个新类别

ID

类别名称

排序

操作

$query=mysql_query("select * from b_mtype order by rootid,orders");

while($arr=mysql_fetch_array($query))

{

?>

0) {

for($i=1;$i

}?>

0) {echo"+";}else{echo"-";}?>

";}?>

0) {?>()

">添加子类

| ">编辑类别

|   "onClick="{if(confirm('删除类别时,该类别下的所有产品将一并被删除,确定删除吗?')){return true;}return false;}">删除类别删除类别 

}

?>

break;

}

?>

functionadd(){

@$editid=$_REQUEST["editid"];

?>

创建新的类别

类别名称:

所属类别

做为主类别

$query=mysql_query("select * from b_mtype order by rootid,orders");

while($arr=mysql_fetch_array($query)){ ?>

">

0) {

for($i=1;$i

{

echo"-";

}

}?>

}

?>

functionsaveadd(){

$query=mysql_query("select typeid from b_mtype order by typeid desc limit 1");

while($arr=mysql_fetch_array($query)){

if(!$arr["typeid"]){

$postnum=1;

}else{

$postnum=$arr["typeid"]+1;

}

}

if(!$postnum)$postnum=1;

$ntid=$postnum;

$tn=$_REQUEST["typename"];

$btype=$_REQUEST["btype"];

//echo $btype;

if($ntid==""or$tn=="")

{

echo"";

echo"alert('参数有误,请重新填写.!');";

echo"location.href='?';";

echo"";

die();

}

if($btype!=0){

$result=mysql_query("select rootid,typeid,depth,orders,parentstr from b_mtype where typeid='$btype'");

$aa=mysql_fetch_array($result);

$rootid=$aa['rootid'];

//echo "aaaaaaaaaaa";

$parentid=$aa['typeid'];

$depth=$aa['depth'];

$orders=$aa['orders'];

$parentstr=$aa['parentstr'];

//echo $rootid;

if(($aa["depth"]+1)>20){die("本分类限制最多只能有20级分类"); }

}

if($ntid==$btype)

{

echo"";

echo"alert('您所指定的typeid值重复!');";

echo"location.href='?';";

echo"";

die();

}

if($btype!=0){

$depth=$depth+1;

$rootid=$rootid;

$orders=$ntid;

$parentid=$btype;

//$child = $child;

if($parentstr=="0"){

$parentstr=$btype;

}else{

$parentstr=$parentstr.",".$btype;

}

}else{

$depth=0;

$rootid=$ntid;

$orders=1;

$parentid=0;

$child=0;

$parentstr=0;

}

//插入类别

$query=mysql_query("insert into b_mtype values('$ntid','$tn','$parentid','$parentstr','$depth','$rootid','','$orders')") ;

//用于调试 echo "insert into b_mtype values('$ntid','$tn','$parentid','$parentstr','$depth','$rootid','','$orders')";

if($btype!=0)

{

if($depth>0)

{

//当上级分类深度大于0的时候要更新其父类(或父类的父类)的版面数和相关排序

for($i=1;$i

//更新其父类版面数

if($parentid!=""){

$query=mysql_query("update b_mtype set child=child+1 where typeid='$parentid'");

}

//得到其父类的父类的版面ID

$result=mysql_query("select parentid from b_mtype where typeid='$parentid'");

$par=mysql_fetch_array($result);

if($par['parentid']!=""){

$parentid=$par['parentid'];

}

//当循环次数大于1并且运行到最后一次循环的时候直接进行更新

if($i==$depth&&$parentid!=""){

$query=mysql_query("update b_mtype set child=child+1 where typeid='$parentid'");

}

}//for循环结束

//更新该版面排序以及大于本需要和同在本分类下的版面排序序号

$query=mysql_query("update b_mtype set orders=orders+1 where rootid='$rootid' and orders>'$orders'");

//$orders1=$orders+1;

//echo "orders1=".$orders1;

$query=mysql_query("update b_mtype set orders='$orders'+1 where typeid='$ntid'");

}else{//对应if ($depth>0),当上级分类深度为0的时候只要更新上级分类版面数和该版面排序序号即可

$query=mysql_query("update b_mtype set child=child+1 where typeid='$btype'");

$result=mysql_query("select max(orders) from b_mtype where typeid='$ntid'");

$ord=mysql_fetch_array($result);

$query=mysql_query("update b_mtype set orders='$ord[0]'+1 where typeid='$ntid'");

}

}

echo"";

echo"alert('类别添加成功!');";

echo"location.href='?';";

echo"";

}

?>

functionedit(){

//global $db,$editid,$tn,$arr;

$editid=$_REQUEST["editid"];

$result=mysql_query("select * from b_mtype where typeid='$editid'");

$tn=mysql_fetch_array($result);

?>

">

编辑类别:

类别名称

">

所属类别

    特别提示:

所属类别不能指定当前类别(即自己)为父类别

所属类别不能指定当前类别的子类别为父类别

做为主类别

$query=mysql_query("select * from b_mtype order by rootid,orders");

while($arr=mysql_fetch_array($query))

{ ?>

">0) {?>

}

?>

}

?>

functionsaveedit(){

//global $db,$aa,$bb,$cc,$dd,$ee,$ff,$gg,$ii,$jj,$kk,$ll,$mm,$nn,$qq,$rr;

$editid=$_REQUEST["editid"];

$btype=$_REQUEST["class"];

$tn=$_REQUEST["typename"];

if($editid==$btype){

echo"";

echo"alert('所属类别不能指定当前类别(即自己)为父类别!');";

echo"location.href='?';";

echo"";

die();

}

$result=mysql_query("select * from b_mtype where typeid='$editid'");

$aa=mysql_fetch_array($result);

$newtypeid=$aa["typeid"];

$typename=$aa["typename"];

$parentid=$aa["parentid"];

$iparentid=$aa["parentid"];

$parentstr=$aa["parentstr"];

$depth=$aa["depth"];

$rootid=$aa["rootid"];

$child=$aa["child"];

$orders=$aa["orders"];

////判断所指定的类别是否其下属类别

if($parentid==0){

if($btype!= 0) {

$result=mysql_query("select rootid from b_mtype where typeid='$btype'");

$b=mysql_fetch_array($result);

if($rootid==$bb['rootid']) {

echo"";

echo"alert('所属类别不能指定当前类别的子类别为父类别!');";

echo"location.href='?';";

echo"";

die();

}

}

}else{

$result=mysql_query("select typeid from b_mtype where parentstr like '%$parentstr%' and typeid='$btype'");

$cc=mysql_fetch_array($result);

if($cc[0]){

echo"";

echo"alert('所属类别不能指定当前类别的子类别为父类别!');";

echo"location.href='?';";

echo"";

die();

}

}

if($parentid==0){

$parentid=$editid;

$iparentid=0;

}

mysql_query("update b_mtype set typename='$tn',parentid='$btype' where typeid='$editid'");

$result1=mysql_query("select max(rootid) from b_mtype");

$ss=mysql_fetch_array($result1);

$maxrootid=$ss["rootid"]+1;

if(!$maxrootid){$maxrootid=1;}

//假如更改了所属类别

//需要更新其原来所属版面信息,包括深度、父级ID、版面数、排序、继承版主等数据

//需要更新当前所属版面信息

//继承版主数据需要另写函数进行更新--取消,在前台可用typeid in parentstr来获得

if($parentid!=$btype&& !($iparentid==0 &&$btype==0)) {

//如果原来不是一级分类改成一级分类

//echo "ggg";

if($iparentid>0 &&$btype==0)

{

//更新当前版面数据

mysql_query("update b_mtype set depth=0,orders=0,rootid='$editid',parentid=0,parentstr='0' where typeid='$newtypeid'");

$parentstr=$parentstr.",";

$result=mysql_query("select count(*) from b_mtype where parentstr like '%$parentstr%'");

$dd=mysql_fetch_array($result);

$postcount=$dd[0];

if(emptyempty($postcount))

{

$postcount=1;

}else{

$postcount=$postcount+1;

}

//更新其原来所属类别版面数

mysql_query("update b_mtype set child=child-'$postcount' where typeid='$iparentid'");

//更新其原来所属类别数据,排序相当于剪枝而不需考虑

for($i=1;$i

{

////得到其父类的父类的版面ID

$result2=mysql_query("select parentid from b_mtype where typeid='$iparentid'");

$ee=mysql_fetch_array($result2);

if(!$ee[0]){

$iparentid=$ee[0];

mysql_query("update b_mtype set child=child-'$postcount' where typeid='$iparentid'");

}

}//for end

if($child>0){//m1

//更新其下属类别数据

//有下属类别,排序不需考虑,更新下属类别深度和一级排序ID(rootid)数据

//更新当前版面数据

$i=0;

$query=mysql_query("select * from b_mtype where parentstr like '%$parentstr%'");

while($arr=mysql_fetch_array($query)){

$i++;

$mParentStr=strtr($arr['parentstr'],$parentstr," ");

mysql_query("update b_mtype set depth=depth-'$depth',rootid='$maxrootid',parentstr='$mParentStr' where typeid='$arr[typeid]");

}

}//m1 end

}elseif($iparentid> 0 &&$btype>0) {

//将一个分类别移动到其他分类别下

//获得所指定的类别的相关信息

$result=mysql_query("select * from b_mtype where typeid='$btype'");

$gg=mysql_fetch_array($result);

//得到其下属版面数

$parentstr=$parentstr.",";

$iparentstr=$parentstr.$editid;

echo$iparentstr;

$result1=mysql_query("select count(*) from b_mtype where parentstr like '%$iparentstr%'");

$ii=mysql_fetch_array($result1);

$postcount=$ii[0];

if(emptyempty($postcount)){$postcount=1; }

//在获得移动过来的版面数后更新排序在指定类别之后的类别排序数据

$query=mysql_query("update b_mtype set orders=orders+'$postcount'+1 where rootid='$gg[rootid]' and orders>'$gg[orders]'");

//更新当前版面数据

If($gg[parentstr]=="0") {

// $idepth=$gg[depth]+1;

// $iorders=$gg[orders]+1;

mysql_query("update b_mtype set depth='$gg[depth]'+1,orders='$gg[orders]'+1,rootid='$gg[rootid]',parentid='$btype',parentstr='$gg[typeid]' where typeid='$newtypeid'");

}Else{

$aparentstr=$gg['parentstr'].",".$gg['typeid'];

$idepth=$gg['depth']+1;

$iorders=$gg['orders']+1;

mysql_query("update b_mtype set depth='$idepth',orders='$iorders',rootid='$gg[rootid]',parentid='$btype',parentstr='$aparentstr' where typeid='$editid'");

}

$i=1;

//如果有则更新下属版面数据

//深度为原有深度加上当前所属类别的深度

$iparentstr=$parentstr.$newtypeid;

$query=mysql_query("select * from b_mtype where parentstr like '%$iparentstr%' order by orders");

while($arr=mysql_fetch_array($query)){// m2

$i++;

If ($gg['parentstr']=="0") {

$iParentStr=$gg['typeid'].",".strtr($arr['parentstr'],$parentstr," ");

}Else{

$iParentStr=$gg["parentstr"] .",".$gg["typeid"] .",".strtr($arr['parentstr'],$parentstr," ");

}

$query=mysql_query("update b_mtype set depth=depth+'$gg[depth]'-'$depth'+1,orders='$gg[orders]'+'$i',rootid='$gg[rootid]',parentstr='$iParentStr' where typeid='$arr[typeid]'");

}///m2 end

$parentid=$btype;

if($rootid==$gg['rootid']) {///m3

//在同一分类下移动

//更新所指向的上级类别版面数,i为本次移动过来的版面数

//更新其父类版面数

$query=mysql_query("update b_mtype set child=child+'$i' where (! parentid=0) and typeid='$parentid'");

for($k=1;$k

//得到其父类的父类的版面ID

$result=mysql_query("select parentid from b_mtype where (! parentid=0) and typeid='$parentid'");

$vv=mysql_fetch_array($result);

if($vv[0]){

$parentid=$vv[0];

//更新其父类的父类版面数

mysql_query("update b_mtype set child=child+'$i' where (! parentid=0) and   typeid='$parentid'");

}

}// for end

//更新其原父类版面数

mysql_query("update b_mtype set child=child-'$i' where (! parentid=0) and typeid='$iparentid'");

//更新其原来所属类别数据

for($k=1;$k

//得到其原父类的父类的版面ID

$result1=mysql_query("select parentid from b_mtype where (! parentid=0) and typeid='$iparentid'");

$zz=mysql_fetch_array($result1);

if($zz[0]){

$iparentid=$zz[0];

//更新其原父类的父类版面数

mysql_query("update b_mtype set child=child-'$i' where (! parentid=0) and   typeid='$iparentid'");

}

}//for end

}else{////m3 end

//更新所指向的上级类别版面数,i为本次移动过来的版面数

//更新其父类版面数

mysql_query("update b_mtype set child=child+'$i' where typeid='$parentid'");

for($k=1;$k

//得到其父类的父类的版面ID

$result2=mysql_query("select parentid from b_mtype where typeid='$parentid'");

$yy=mysql_fetch_array($result2);

if($yy[0]){

$parentid=$yy[0];

//更新其父类的父类版面数

mysql_query("update b_mtype set child=child+'$i' where typeid='$parentid'");

}

}//for end

//更新其原父类版面数

mysql_query("update b_mtype set child=child-'$i' where typeid='$iparentid'");

//更新其原来所属类别数据

for($k=1;$k

//得到其原父类的父类的版面ID

$query=mysql_query("select parentid from b_mtype where typeid='$iparentid'");

while($arr=mysql_fetch_array($query))

{

if($arr[0]){

$iparentid=$arr[0];

//更新其原父类的父类版面数

mysql_query("update b_mtype set child=child-'$i' where typeid='$iparentid'");

}

}

}//for end

}///m3 end

}else{

//如果原来是一级类别改成其他类别的下属类别

//得到所指定的类别的相关信息

$result=mysql_query("select * from b_mtype where typeid='$btype'");

$gg=mysql_fetch_array($result);

echo$rootid;

$result1=mysql_query("select count(*) from b_mtype where rootid='$rootid'");

$qq=mysql_fetch_array($result1);

$postcount=$qq[0];

//更新所指向的上级类别版面数,i为本次移动过来的版面数

$parentid=$btype;

//更新其父类版面数

mysql_query("update b_mtype set child=child+'$postcount' where typeid='$parentid'");

for($k=1;$k

//得到其父类的父类的版面ID

$result2=mysql_query("select parentid from b_mtype where typeid='$parentid'");

$rr=mysql_fetch_array($result2);

if($rr[0]){

$parentid=$rr[0];

//更新其父类的父类版面数

mysql_query("update b_mtype set child=child+'$postcount' where typeid='$parentid'");

}

}///for end

//在获得移动过来的版面数后更新排序在指定类别之后的类别排序数据

mysql_query("update b_mtype set orders=orders+'$postcount'+1 where rootid='$gg[rootid]' and orders>'$gg[orders]'");

$i=0;

$query=mysql_query("select * from b_mtype where rootid='$rootid' order by orders");

while($arr=mysql_fetch_array($query))

{

$i++;

if($arr['parentid'] ==0)

{

if($gg['parentstr'] =="0")

{

$parentstr=$gg['typeid'];

}else{

$parentstr=$gg['parentstr'] .",".$gg['typeid'];

}

mysql_query("update b_mtype set depth=depth+'$gg[depth]'+1,orders='$gg[orders]'+'$i',rootid='$gg[rootid]',parentstr='$parentstr',parentid='$btype' where typeid='$arr[typeid]'");

}else{

if($gg['parentstr'] =="0"){

$parentstr=$gg['typeid'] .",".$arr['parentstr'];

}else{

$parentstr=$gg['parentstr'] .",".$gg['typeid'] .",".$arr['parentstr'];

}

mysql_query("update b_mtype set depth=depth+'$gg[depth]'+1,orders='$gg[orders]'+'$i',rootid='$gg[rootid]',parentstr='$parentstr' where typeid='$arr[typeid]'");

}

}///while end

}//else end

}

echo"";

echo"alert('类别修改成功!');";

echo"location.href='?';";

echo"";

}

?>

functiondel(){

////更新其上级版面类别数,如果该类别含有下级类别则不允许删除

$editid=$_REQUEST["editid"];

$result=mysql_query("select parentstr,child,depth from b_mtype where typeid='$editid'");

$aa=mysql_fetch_array($result);

if($aa[0]!="") {

if($aa[1]>0){

echo"";

echo"alert('该类别含有下属类别,必须先删除其下属类别方能删除本类别!');";

echo"location.href='?';";

echo"";

die();

}

//如果有上级版面,则更新数据

if($aa[2]>0){

$query=mysql_query("update b_mtype set child=child-1 where typeid in ($aa[0])");

}

$query=mysql_query("delete from b_mtype where typeid='$editid'");

}

echo"";

echo"alert('类别删除成功!');";

echo"location.href='?';";

echo"";

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值