下面提供一个PHP的无限分级类代码,想要实现无限分级类的朋友们可以试试 。Bug已经检查修正过,直接复制代码就能使用(尖括号需要替换一下) 。
1、数据库ProductShow中有b_mtype表,建表语句如下:
-
CREATE TABLE `b_mtype` (
-
`typeid` int(4) NOT NULL default 0,
`typename` varchar(50) NOT NULL default ,
`parentid` int(4) NOT NULL default 0,
`parentstr` varchar(200) NOT NULL default ,
`depth` int(4) NOT NULL default 0,
`rootid` int(4) NOT NULL default 0,
`child` varchar(200) NOT NULL default ,
`orders` varchar(200) NOT NULL default ,
PRIMARY KEY (`typeid`)
) TYPE=MyISAM;
2、ProductClass.php代码:(直接复制即可)
-
< html>
-
< head>
-
< meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
-
< title>WHB_PHP无限级分类< /title>
-
< style type="text/css">
-
< !--
-
.STYLE1 {color: #FF0000}
-
body,td,th {
-
font-family: 宋体;
-
font-size: 12px;
-
}
-
.STYLE2 {
-
color: #FFFFFF;
-
font-size: 14px;
-
font-weight: bold;
-
}
-
-->
-
< /style>
-
< /head>
-
-
< body>
-
< ?php
-
ini_set("error_reporting","E_ALL & 'E_NOTICE");
-
require_once("conn.php");
-
@$ToDo=$_GET["ToDo"];
-
switch($ToDo)
-
{
-
case "add":
-
echo add();
-
break;
-
-
case "edit":
-
echo edit();
-
break;
-
-
case "saveadd":
-
echo saveadd();
-
break;
-
-
case "saveedit":
-
echo saveedit();
-
break;
-
-
case "del":
-
echo del();
-
break;
-
-
default:
-
?>
-
-
< table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="tblborder">
-
< tr>
-
< td valign="top">< table width="100%" border="0" cellspacing="1" cellpadding="3">
-
< tr>
-
< td width="55%" height="20" align="center" bgcolor="#698FC7">< span class="tblhead">< b>< span class="STYLE2">商品类别设置< /span> < a href="?ToDo=add">添加一个新类别< /a>< /b>< /span>< /td>
-
-
< /tr>
-
< /table>
-
< table width="100%" bgcolor="#cccccc" cellspacing="1" cellpadding="3">
-
< tr bgcolor="#F9F9F9">
-
< td width="6%" height="21">ID< /td>
-
< td width="49%">类别名称< /td>
-
< td width="14%">排序< /td>
-
< td width="31%">操作< /td>
-
< /tr>
-
< ?php
-
-
$query=mysql_query("select * from b_mtype order by rootid,orders");
-
while($arr=mysql_fetch_array($query))
-
{
-
-
?>
-
< tr>
-
< td bgcolor="#FFFFFF">< ?php echo $arr["typeid"];?>< /td>
-
< td bgcolor="#FFFFFF">< ?php if ($arr["depth"]>0) {
-
for($i=1;$i< =$arr["depth"];$i++){ echo " ";}
-
}?>
-
-
< ? if($arr["child"]>0) { echo "+";}else {echo "-";}?>
-
< ? if($arr["parentid"]==0) { echo "< b>";}?>
-
< ? echo $arr["typename"];?>
-
< ? if ($arr["child"]>0) {?>(< ? echo $arr["child"];?>)< ? }?>< /td>
-
< td bgcolor="#FFFFFF">< ?php echo $arr["orders"];?>< /td>
-
< td bgcolor="#FFFFFF">< a href="?ToDo=add&editid=< ?php echo $arr["typeid"];?>">< U>添加子类< /U>< /a>
-
| < a href="?ToDo=edit&editid=< ?php echo $arr["typeid"]?>">< U>编辑类别< /U>< /a>
-
| < ? if($arr["child"]==0){?>< a href="?ToDo=del&editid=< ? echo $arr["typeid"];?>" onClick="{if(confirm(删除类别时,该类别下的所有产品将一并被删除,确定删除吗?)){return true;}return false;}">< U>删除类别< /U>< /a>< ?}else{?>< a href="#" onClick="{if(confirm(该类别含有下属类别,必须先删除其下属类别方能删除本类别!)){return true;}return false;}">< U>删除类别< /U>< /a>< ?}?> < /td>
-
< /tr>
-
< ?php
-
}
-
?>
-
< /table>
-
< /td>
-
< /tr>
-
< /table>
-
< ?php
-
break;
-
}
-
?>
-
-
-
< ?php
-
function add(){
-
@$editid=$_REQUEST["editid"];
-
?>
-
< form name="form1" method="post" action="?ToDo=saveadd">
-
< table width="90%" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
-
< tr bgcolor="#CCCCCC">
-
< td colspan="2" align="center" bgcolor="#698FC7">< span class="STYLE2">创建新的类别< /span>< /td>
-
< /tr>
-
< tr>
-
< td width="25%" bgcolor="#FFFFFF">类别名称:< /td>
-
< td width="75%" bgcolor="#FFFFFF">< input name="typename" type="text" id="typename">< /td>
-
< /tr>
-
< tr>
-
< td width="25%" height=30 bgcolor="#FFFFFF">< U>所属类别< /U>< /td>
-
< td width="75%" bgcolor="#FFFFFF">
-
< select name=btype>
-
< option value="0">做为主类别< /option>
-
< ?
-
$query=mysql_query("select * from b_mtype order by rootid,orders");
-
while($arr=mysql_fetch_array($query)){ ?>
-
< option value="< ? echo $arr["typeid"]?>" < ?php if($editid == $arr[typeid]){ echo " selected"; }?>>
-
< ? if($arr["depth"]>0) {
-
for($i=1;$i< =$arr["depth"];$i++)
-
{
-
echo "-";
-
}
-
-
}?>
-
< ? echo $arr["typename"]?>< /option>
-
< ?
-
}
-
?>
-
< /select>
-
< /td>
-
< /tr>
-
< tr>
-
< td bgcolor="#FFFFFF"> < /td>
-
< td bgcolor="#FFFFFF">< input type="submit" name="Submit" value="保存">
-
< label>
-
< input type="button" name="cancel" id="cancel" value="返回" onclick="history.go(-1);" />
-
< /label>< /td>
-
< /tr>
-
< /table>
-
< /form>
-
< ? }?>
-
-
< ?php
-
function saveadd(){
-
-
$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"];
-
-
-
-
-
if ($ntid=="" or $tn=="")
-
{
-
echo "< script language=javascript>";
-
echo "alert(参数有误,请重新填写.!);";
-
echo "location.href=?;";
-
echo "< /script>";
-
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];
-
-
$parentid=$aa[typeid];
-
$depth=$aa[depth];
-
$orders=$aa[orders];
-
$parentstr=$aa[parentstr];
-
-
-
if(($aa["depth"]+1)>20){ die("本分类限制最多只能有20级分类"); }
-
-
}
-
-
if($ntid == $btype)
-
{
-
echo "< script language=javascript>";
-
echo "alert(您所指定的typeid值重复!);";
-
echo "location.href=?;";
-
echo "< /script>";
-
die();
-
}
-
-
if($btype!=0){
-
-
$depth=$depth+1;
-
$rootid=$rootid;
-
$orders =$ntid;
-
$parentid =$btype;
-
-
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)
-
{
-
-
for ($i=1;$i< =$depth;$i++){
-
-
if ($parentid!=""){
-
$query=mysql_query("update b_mtype set child=child+1 where typeid=$parentid");
-
}
-
-
$result=mysql_query("select parentid from b_mtype where typeid=$parentid");
-
$par=mysql_fetch_array($result);
-
-
if ($par[parentid]!=""){
-
$parentid=$par[parentid];
-
}
-
-
if ($i==$depth && $parentid!=""){
-
$query=mysql_query("update b_mtype set child=child+1 where typeid=$parentid");
-
}
-
}
-
-
$query=mysql_query("update b_mtype set orders=orders+1 where rootid=$rootid and orders>$orders");
-
-
-
$query=mysql_query("update b_mtype set orders=$orders+1 where typeid=$ntid");
-
-
}else{
-
$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 "< script language=javascript>";
-
echo "alert(类别添加成功!);";
-
echo "location.href=?;";
-
echo "< /script>";
-
}
-
?>
-
-
< ?PHP
-
function edit(){
-
-
-
$editid=$_REQUEST["editid"];
-
-
$result=mysql_query("select * from b_mtype where typeid=$editid");
-
$tn=mysql_fetch_array($result);
-
?>
-
< form name="form2" action ="?ToDo=saveedit" method="post">
-
< input type="hidden" name="editid" value="< ?php echo $editid;?>">
-
< table width="90%" border="0" align=center cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
-
< tr>
-
< th height=24 colspan=2 bgcolor="#698FC7" class="STYLE2">编辑类别:< ?PHP echo $tn["typename"];?>< /th>
-
< /tr>
-
< tr>
-
< td width="44%" height=30 bgcolor="#FFFFFF">类别名称< /td>
-
< td width="56%" bgcolor="#FFFFFF">
-
< input type="text" name="typename" size="35" value="< ?php echo $tn["typename"];?>">
-
< /td>
-
< /tr>
-
-
< tr>
-
< td width="44%" height=30 bgcolor="#FFFFFF">< p>所属类别< /p>
-
< p> < span class="STYLE1">特别提示:< /span>< br />
-
所属类别不能指定当前类别(即自己)为父类别< BR>
-
所属类别不能指定当前类别的子类别为父类别< /p>< /td>
-
< td width="56%" bgcolor="#FFFFFF">
-
< select name="class">
-
< option value="0">做为主类别< /option>
-
< ?
-
$query=mysql_query("select * from b_mtype order by rootid,orders");
-
-
while($arr=mysql_fetch_array($query))
-
{ ?>
-
< option value="< ?php echo $arr[typeid]?>" < ?php if ($tn["parentid"] == $arr["typeid"]) { echo "selected"; } ?>>< ? if ($arr["depth"]>0) {?>
-
< ?for ($i=1;$i< =$arr["depth"];$i++){ echo "-";}?>
-
< ? } ?>< ?php echo $arr["typename"]?>< /option>
-
< ?
-
}
-
?>
-
< /select>
-
< /td>
-
< /tr>
-
-
< tr>
-
< td width="44%" height=24 bgcolor="#FFFFFF"> < /td>
-
< td width="56%" bgcolor="#FFFFFF">
-
< input type="submit" name="Submit" value="提交修改">
-
< /td>
-
< /tr>
-
< /table>
-
< /form>
-
-
< ?
-
}
-
-
?>
-
-
-
< ?php
-
function saveedit(){
-
-
$editid=$_REQUEST["editid"];
-
$btype=$_REQUEST["class"];
-
$tn=$_REQUEST["typename"];
-
-
if($editid == $btype ){
-
echo "< script language=javascript>";
-
echo "alert(所属类别不能指定当前类别(即自己)为父类别!);";
-
echo "location.href=?;";
-
echo "< /script>";
-
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 "< script language=javascript>";
-
echo "alert(所属类别不能指定当前类别的子类别为父类别!);";
-
echo "location.href=?;";
-
echo "< /script>";
-
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 "< script language=javascript>";
-
echo "alert(所属类别不能指定当前类别的子类别为父类别!);";
-
echo "location.href=?;";
-
echo "< /script>";
-
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;}
-
-
-
-
-
-
-
-
if ($parentid != $btype && !($iparentid==0 && $btype==0)) {
-
-
-
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< =$depth;$i++)
-
{
-
-
$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");
-
}
-
}
-
-
if ($child >0){
-
-
-
-
-
$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]");
-
}
-
}
-
}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") {
-
-
-
-
-
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)){
-
$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]");
-
}
-
-
$parentid=$btype;
-
if ($rootid==$gg[rootid]) {
-
-
-
-
$query=mysql_query("update b_mtype set child=child+$i where (! parentid=0) and typeid=$parentid");
-
for ($k=1;$k< =$gg[depth];$k++){
-
-
$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");
-
}
-
-
}
-
-
mysql_query("update b_mtype set child=child-$i where (! parentid=0) and typeid=$iparentid");
-
-
-
for ($k=1;$k< =$depth;$k++){
-
-
$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");
-
}
-
}
-
-
}else{
-
-
-
mysql_query("update b_mtype set child=child+$i where typeid=$parentid");
-
for ($k=1;$k< =$gg["depth"];$k++){
-
-
$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");
-
}
-
-
}
-
-
mysql_query("update b_mtype set child=child-$i where typeid=$iparentid");
-
-
for ($k=1;$k< =$depth;$k++){
-
-
$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");
-
}
-
}
-
}
-
}
-
}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];
-
-
$parentid=$btype;
-
-
mysql_query("update b_mtype set child=child+$postcount where typeid=$parentid");
-
-
for ($k=1;$k< =$gg[depth];$k++){
-
-
$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");
-
}
-
-
}
-
-
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]");
-
-
}
-
}
-
}
-
}
-
echo "< script language=javascript>";
-
echo "alert(类别修改成功!);";
-
echo "location.href=?;";
-
echo "< /script>";
-
}
-
?>
-
-
< ?php
-
function del(){
-
-
-
$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 "< script language=javascript>";
-
echo "alert(该类别含有下属类别,必须先删除其下属类别方能删除本类别!);";
-
echo "location.href=?;";
-
echo "< /script>";
-
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 "< script language=javascript>";
-
echo "alert(类别删除成功!);";
-
echo "location.href=?;";
-
echo "< /script>";
-
}
-
?>
-
< /body>
-
< /html>
-
试试看这个无限分级类吧!
|