
日期:2006-07-25 作者:喜騰小二 來源:PHPChina
轉自:http://blog.csdn.net/song2004_2008/archive/2006/07/21/953672.aspx
根據動網的論壇無限級的分類,特開發了PHP版的無限級的分類.
即然是PHP,資料表當然是 MYSQL:在應用之前,先在mysql中建立資料表.b_mtype.其中的欄位包括:typeid,typename,parentid,paretnstr,rootid,child,orders.
俱體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:
?>
|
function add(){
// global $db,$postnum,$editid;
$editid=$_REQUEST["editid"];
$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;
}
?>
function saveadd(){
$ntid =$_REQUEST["newtypeid"];
$tn =$_REQUEST["typename"];
$btype =$_REQUEST["btype"];
//echo $btype;
if ($ntid=="" or $tn=="")
{
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)
{
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<=$depth;$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{
//當上級分類深度為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 "類別填加成功";
}
}
?>
function edit(){
//global $db,$editid,$tn,$arr;
$editid=$_REQUEST["editid"];
$result=mysql_query("select * from b_mtype where typeid='$editid'");
$tn=mysql_fetch_array($result);
?>
?>
function saveedit(){
//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 ){ 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']) {
die("您不能指定該版麵的下屬論壇作為所屬論壇11");
}
}
}else{
$result=mysql_query("select typeid from b_mtype where parentstr like '%$parentstr%' and typeid='$btype'");
$cc=mysql_fetch_array($result);
if ($cc[0]){
die("您不能指定該版麵的下屬論壇作為所屬論壇2");
}
}
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)
{
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;$i<=$depth;$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) {
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['depth'];$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<=$depth;$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
echo "ccc";
//更新所指嚮的上級類別版麵數,i為本次移動過來的版麵數
//更新其父類版麵數
mysql_query("update b_mtype set child=child+'$i' where typeid='$parentid'");
for ($k=1;$k<=$gg["depth"];$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<=$depth;$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{
echo "第三種情況";
//如果原來是一級類別改成其他類別的下屬類別
//得到所指定的類別的相關資訊
$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<=$gg['depth'];$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 "
類別修改成功!
";
}
}
?>
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){
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 "類別移除成功!";
}
?>