php+mysql實現的無限極分類

<?php
/*
* 迭代sql查詢,生成無限級分類樹
* 數據結構
    CREATE TABLE `cat` (
        `cid` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
        `cname` VARCHAR(50) NOT NULL,
        `lev` INT(5) UNSIGNED NOT NULL COMMENT '分類層數level',
        `pid` INT(5) UNSIGNED NOT NULL COMMENT '上級cid',
        PRIMARY KEY (`cid`)
    )
* 本程序僅供演示「目錄樹」的算法概念,未作參數有效性的校驗
* 本程序含有添加、刪除、列目錄樹功能,編輯功能未實現(很是簡單啦,大家懂得)
*/
error_reporting(7);
header("charset=utf-8");
mysql_connect("localhost", "root", "root");
mysql_select_db("cat");
mysql_query("set names utf8");php

$act = $_GET['act'];
if($act == "insert") {
    $cname = trim($_POST['cname']);
    $pid = intval($_POST['cid']);mysql

    //取得上級目錄的分類層數
    $query = "select * from cat where cid='$pid'";
    $res = mysql_query($query);
    $row = mysql_fetch_assoc($res);
    $parent_lev = $row['lev'];算法

    $query = "insert into cat set cname='$cname',lev='".($parent_lev+1)."',pid='$pid'";
    mysql_query($query);sql

    echo "<script>alert('增長新分類成功');";
    echo "location.href='cat.php';</script>";
    die;
}elseif($act == "del") {
    $cid = intval($_GET['cid']);數組

    if(delCat($cid)) {
        echo "<script>alert('分類樹刪除成功');";
        echo "location.href='cat.php';</script>";
        die;
    }
}elseif($act == "edit") {
    $cid = intval($_GET['cid']);數據結構

    $query = "select * from cat where cid='$cid'";
    $res = mysql_query($query);
    $editRow = mysql_fetch_assoc($res);ide

    //將當前分類及其子分類的cid所有得到,在select中要屏蔽(disabled)
    $pid = $editRow['cid'];
    $subArr = getCat($pid);
    //提取cid
    $subCidArr = array();
    foreach($subArr as $_row) {
        $subCidArr[] = $_row['cid'];
    }
    array_unshift($subCidArr, $editRow['cid']);//全部子分類cid的數組頭部壓入當前編輯分類的cid,造成當前分類及其所屬全部子分類的cid數組post


    $pid = 0;//從根分類開始列目錄樹
    $okArr = getCat($pid);
?>
    <form method="post" action="cat.php?act=update">
        分類名:<input type="text" name="cname" value="<?php echo($editRow['cname']);?>"><br><br>
        所屬分類:<select name="cid">
            <option value="0">根目錄</option>
            <?php
            foreach($okArr as $row) {
                echo "<option value='".$row['cid']."'";
                if($row['cid'] == $editRow['pid']) {//將該分類的原始父分類設爲默認選中狀態
                    echo "selected ";
                }
                if(in_array($row['cid'], $subCidArr)) {//將該分類本身及其子分類屏蔽
                    echo "disabled ";
                }
                echo ">";
                for($i=1;$i<$row['lev'];$i++) {
                    echo " ";
                }
                echo $row['cname'];
                echo "</option>";
            }
            ?>
        </select><br><br>
        <input type="submit" name="提交">
        <input type="hidden" name="edit_cid" value="<?php echo($cid);?>">
    </form>
<?php
}elseif($act == "update") {
    $cid = intval($_POST['edit_cid']);fetch

    $cname = trim($_POST['cname']);//新的分類名
    $pid = intval($_POST['cid']);//新的父分類orm

    //原始分類記錄
    $query = "select * from cat where cid='$cid'";
    $res = mysql_query($query);
    $cat_old_row = mysql_fetch_assoc($res);

    //新的父分類記錄
    $query = "select * from cat where cid='$pid'";
    $res = mysql_query($query);
    $cat_p_row = mysql_fetch_assoc($res);

    //比對原始記錄的lev和將所屬新父分類後的lev
    $lev_old = $cat_old_row['lev'];
    $lev_new = $cat_p_row['lev']+1;
    $lev_diff = $lev_old - $lev_new;//調整後的lev差值

    //得到全部子分類
    $subArr = getCat($cid);
    //提取全部子分類的cid
    $subCidArr = array();
    foreach($subArr as $_row) {
        $subCidArr[] = $_row['cid'];
    }
    array_unshift($subCidArr, $cid);//全部子分類cid的數組頭部壓入當前編輯分類的cid,造成當前分類及其所屬全部子分類的cid數組

    //修改當前分類記錄信息
    $query1 = "update cat set cname='$cname', pid='$pid' where cid='$cid'";
    mysql_query($query1);

    //並當前分類及其全部子分類的lev作相應的差值調整
    $query2 = "update cat set lev=lev-$lev_diff where cid in (".implode(",", $subCidArr).")";
    mysql_query($query2);

    echo "<script>alert('分類編輯成功');";
    echo "location.href='cat.php';</script>";
    die;
}else{
    /* 按照分類層次等級列目錄樹 */


    $pid = 0;//從根分類開始列目錄樹
    $catAllArr = getCat($pid);
  

    echo "<h2>目錄樹</h2>";
    foreach($catAllArr as $row) {
        for($i=1;$i<$row['lev'];$i++) {
            echo " ";
        }
        echo $row['cname']."  <a href='cat.php?act=edit&cid=".$row['cid']."'>編輯</a>  <a href='?act=del&cid=".$row['cid']."'>刪除</a><BR>";
    }

    echo "<HR>";
    echo "<h4>建立新分類</h4>";
?>
    <meta charset="utf-8">
    <form method="post" action="cat.php?act=insert">
        分類名:<input type="text" name="cname"><br><br>
        所屬分類:<select name="cid">
            <option value="0">根目錄</option>
            <?php
            foreach($catAllArr as $row) {
                echo "<option value='".$row['cid']."'>";
                for($i=1;$i<$row['lev'];$i++) {
                    echo " ";
                }
                echo $row['cname'];
                echo "</option>";
            }
            ?>
        </select><br><br>
        <input type="submit" name="提交">
    </form>
<?php

}

//遞歸得到分類目錄樹
function getCat($pid, &$arr="") {
    if(empty($arr)) {
        $arr = array();
    }

    $sql = "select * from cat where pid='$pid'";
    $res = mysql_query($sql);

    while($row = mysql_fetch_assoc($res)) {
        if(!empty($row)) {
            $arr[] = $row;

            getCat($row['cid'], $arr);
        }
    }
    return $arr;
}

//遞歸刪除分類目錄樹
function delCat($cid) {
    //得到下一層分類
    $sql = "select * from cat where pid='$cid'";
    $res = mysql_query($sql);

    while($row = mysql_fetch_assoc($res)) {
        if(!empty($row)) {
            delCat($row['cid']);
        }
    }

    //刪除本層分類
    $sql = "delete from cat where cid='$cid'";
    $res = mysql_query($sql);

    return $res; }

相關文章
相關標籤/搜索