用PHP做商品无限级的分类程序
时间:2006-12-31 来源:yueming
根据动网的论坛无限级的分类,特开发了PHP版的无限级的分类.
即然是PHP,数据表当然是 MYSQL:在应用之前,先在mysql中建立数据表.b_mtype.其中的字段包括:typeid,typename,parentid,paretnstr,rootid,child,orders.
具体PHP程序如下:
商品类别设置
ID
类别名称
排序
操作
0) {
for($i=1;$i
0) { echo "+";}else {echo "-";}?>
";}?>
0) {?>()
">添加版面
| ">基本设置
| " >删除删除
创建新的类别
" />
类别名称:
所属类别
做为主类别
" >
0) {
for($i=1;$i
20){ die("本分类限制最多只能有20级分类"); }
}
if($ntid == $btype)
{
die("您所指定的typeid值重复。");
}
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','')") ;
if ($btype!=0)
{
if ($depth>0)
{
//当上级分类深度大于0的时候要更新其父类(或父类的父类)的版面数和相关排序
for ($i=1;$i'$orders'");
//$orders1=$orders+1;
//echo "orders1=".$orders1;
$query=mysql_query("update b_mtype set orders='$orders'+1 where typeid='$ntid'");
}else{
//当上级分类深度为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 "类别填加成功";
}
}
?>
">
编辑类别:
类别名称
">
所属类别
所属类别不能指定为当前类别
所属类别不能指定为当前版面的下属类别
做为主类别
" >0) {?>
0 && $btype==0)
{
echo "第一部分";
//更新当前版面数据
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];
echo "postcount=".$postcount;
if (empty($postcount))
{
$postcount=1;
}else{
$postcount=$postcount+1;
}
//更新其原来所属类别版面数
mysql_query("update b_mtype set child=child-'$postcount' where typeid='$iparentid'");
//更新其原来所属类别数据,排序相当于剪枝而不需考虑
for ($i=1;$i0){ //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) {
echo "第二部分";
//将一个分类别移动到其他分类别下
//获得所指定的类别的相关信息
$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];
echo "postcount1=".$postcout;
if (empty($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;
// echo "ghh";
//如果有则更新下属版面数据
//深度为原有深度加上当前所属类别的深度
$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," ");
}
echo "iParentStr=".$iParentStr;
$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'$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 "类别修改成功!
";
}
}
?>
0){
die("该类别含有下属类别,请删除其下属类别后再进行删除本类别的操作");
}
//如果有上级版面,则更新数据
if ($aa[2]>0){
echo $aa[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 $editid;
//echo $arr[0];
echo "类别删除成功!";
}
?>
即然是PHP,数据表当然是 MYSQL:在应用之前,先在mysql中建立数据表.b_mtype.其中的字段包括:typeid,typename,parentid,paretnstr,rootid,child,orders.
具体PHP程序如下:
商品类别设置
ID
类别名称
排序
操作
0) {
for($i=1;$i
0) { echo "+";}else {echo "-";}?>
";}?>
0) {?>()
">添加版面
| ">基本设置
| " >删除删除
创建新的类别
" />
类别名称:
所属类别
做为主类别
" >
0) {
for($i=1;$i
20){ die("本分类限制最多只能有20级分类"); }
}
if($ntid == $btype)
{
die("您所指定的typeid值重复。");
}
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','')") ;
if ($btype!=0)
{
if ($depth>0)
{
//当上级分类深度大于0的时候要更新其父类(或父类的父类)的版面数和相关排序
for ($i=1;$i'$orders'");
//$orders1=$orders+1;
//echo "orders1=".$orders1;
$query=mysql_query("update b_mtype set orders='$orders'+1 where typeid='$ntid'");
}else{
//当上级分类深度为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 "类别填加成功";
}
}
?>
">
编辑类别:
类别名称
">
所属类别
所属类别不能指定为当前类别
所属类别不能指定为当前版面的下属类别
做为主类别
" >0) {?>
0 && $btype==0)
{
echo "第一部分";
//更新当前版面数据
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];
echo "postcount=".$postcount;
if (empty($postcount))
{
$postcount=1;
}else{
$postcount=$postcount+1;
}
//更新其原来所属类别版面数
mysql_query("update b_mtype set child=child-'$postcount' where typeid='$iparentid'");
//更新其原来所属类别数据,排序相当于剪枝而不需考虑
for ($i=1;$i0){ //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) {
echo "第二部分";
//将一个分类别移动到其他分类别下
//获得所指定的类别的相关信息
$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];
echo "postcount1=".$postcout;
if (empty($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;
// echo "ghh";
//如果有则更新下属版面数据
//深度为原有深度加上当前所属类别的深度
$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," ");
}
echo "iParentStr=".$iParentStr;
$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'$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 "类别修改成功!
";
}
}
?>
0){
die("该类别含有下属类别,请删除其下属类别后再进行删除本类别的操作");
}
//如果有上级版面,则更新数据
if ($aa[2]>0){
echo $aa[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 $editid;
//echo $arr[0];
echo "类别删除成功!";
}
?>
相关阅读 更多 +