欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php無限極分類實現(xiàn)的兩種解決方法

 更新時間:2013年04月28日 15:05:58   作者:  
本篇文章介紹了,在php中無限極分類實現(xiàn)的兩種解決方法。需要的朋友參考下

今天寫了下無限極分類 下面就把代碼貼上來了 寫的不怎么樣。

method of classify one

復制代碼 代碼如下:

<?php
/*

reader: 這是自己寫的無限極分類實現(xiàn)方法 里面的編輯方法只是對分類名進行了編輯
沒有進行移動操作 小弟能力有限忘大家多多包涵啊

 第一種方法:CREATE TABLE `types` (
  `type_id` int(11) NOT NULL AUTO_INCREMENT,
  `type_name` varchar(20) NOT NULL,
  `type_p_id` varchar(64) NOT NULL DEFAULT '-',
  PRIMARY KEY (`type_id`),
  KEY `type_name` (`type_name`),
  KEY `tname` (`type_name`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
注:
 這里沒做字段有效驗證;
 type_id     表示主鍵自增
 type_name     表示分類名
 type_p_id     表示分類路徑 這里的分類路徑是 上層父類的分類路徑加上此類的主鍵id 用逗號隔開
*/
error_reporting(7);
header("Content:text/html;charset=utf-8");
//這里先進行操作的判斷
$arr = array('list','add','del','edit','addok','edit_ok');
$act= in_array($_GET['ac'],$arr)?$_GET['ac']:$arr[0];
//連接數(shù)據(jù)庫
$conn = mysql_connect("localhost","root","root")or die('數(shù)據(jù)庫連接失敗');
mysql_select_db("study",$conn);
mysql_query("set names utf8");
//根據(jù)分類層進行排序
$sql = "select * from types order by type_p_id";
$sql1 = mysql_query($sql);
//添加分類
if($act == "addok"){
    $type_id = $_POST['type_id'];
    $type_name = $_POST['type_name'];
    //如果等于0表示是跟目錄
    if($type_id=="0"){
        $sql = "insert into types set type_name = '{$type_name}'";
        $res = mysql_query($sql);
        $id = mysql_insert_id();
        $sql = "update types set type_p_id = '$id,' where type_id=$id";
        $res = mysql_query($sql);
        if( mysql_affected_rows ()>0){
            echo "<script>location.href='ok.php?act=list'</script>";
        }else{
            echo "<script>alert('添加失敗');</script>";
        }
    }//如果不等于0
    else{
        //根據(jù)typeid 找到 該id下的type_p_id
        $sql = "select type_p_id from `types` where type_id =  $type_id";
        $res = mysql_query($sql);
        $res = mysql_fetch_assoc($res);
        $type_id = $res['type_p_id'];

        //先將名稱插入進去
        $sql = "insert into types set type_name = '{$type_name}'";
        $res = mysql_query($sql);
        //獲取最后執(zhí)行的id 然后進行數(shù)據(jù)更新 主要更新 type_p_id
        $id = mysql_insert_id();
        $sql = "update types set type_p_id = '$type_id$id,' where type_id=$id";
        $res = mysql_query($sql);
        if($res){
            echo "<script>location.href='ok.php?act=list'</script>";
        }else{
            echo "<script>alert('添加失敗');</script>";
        }
    }
}elseif($act=="add"){
?>
<form method="post" action="?ac=addok">
新分類名稱:    <input type="text" name="type_name"><br/>
當前分類:<select name="type_id" >
    <option  value="0">頂級分類</option>
    <?php
    //循環(huán)遍歷分類
    while($res = mysql_fetch_assoc($sql1)){
        //查找 ","號出現(xiàn)的次數(shù)
        $m=substr_count($res['type_p_id'],",");
        //使用空格替換 "空格"
        $strpad = str_pad("",$m*8*2,"&#12288;");
        if($m==1){
            $sele = "disabled";
        }else{
            $sele = "";
        }
    ?>
        <option value="<?php echo $res['type_id']?>"><?php  echo $strpad.$res['type_name']?></option>
    <?php
    }
    ?>
    </select><br />
    <input type="submit"  value="添加"/>
</form>
<?php               
}elseif($act == "list"){
    //獲取列表根據(jù) 分類層進行排序
    $sql = "select * from types order by type_p_id";
    $res = mysql_query($sql);
?>
<table width="50%" style="font-size:12px;margin-left:20%;">
<tr><td>id</td><td>分類名</td><td>path路徑</td><td>操作</td></tr>
<?php
while($arr = mysql_fetch_assoc($res)){?>
<tr>
    <td><?php echo $arr['type_id']?></td>
    <td><?php echo $arr['type_name']?></td>
    <td><?php echo $arr['type_p_id']?></td>
    <td ><a href="?ac=edit&type_id=<?php echo $arr['type_id'];?>">編輯</a> |
        <a href="?ac=del&type_id=<?php echo $arr['type_id'];?>">刪除</a></td>
</tr>
<?php
    }
?>
</table>
<input type="button"  value="添加"  onclick="(location.href='?ac=add')">
<?php               
}elseif($act == "edit"){
    $id = $_GET['type_id'];
    $sql = "select *from  `types` where type_id=$id ";
    $res = mysql_query($sql);
    echo "<form  method='post'  action='?ac=edit_ok'>";
    while($arr = mysql_fetch_assoc($res)){
        echo "當前名稱:{$arr['type_name']}"."<br />";
        echo "新名稱:<input type='text'  name='n_type_name'  >"."<br />";
        echo "<input type='hidden'  value={$arr['type_id']} name='id'/>";
    }
    echo "<input type='submit'  value='更新'>";
    echo "</form>";
}elseif($act == "edit_ok"){
    $name = trim($_POST['n_type_name']);
    $id = $_POST['id'];
    if(!empty($name)){
        $sql = "update  `types` set type_name='$name' where type_id=$id";
        mysql_query($sql);
        echo "<script>location.href('ok.php?act=list')</script>";
    }else{
        echo "<script>location.href('ok.php?act=list')</script>";
    }
}elseif($act == 'del'){
    //這里的刪除是要把當前分類 和當前的子分類都要刪除 所以用到$id%
    $id = $_GET['type_id'];
    $sql ="delete from `types` where type_p_id like '$id%' ";
    $res = mysql_query($sql);
    if($res){
        echo "<script>location.href('ok.php?act=list')</script>";
    }else{
        echo "<script>alert('刪除失敗');</script>";
    }
}
?>


types表:

下面是效果圖:

method of classify two

復制代碼 代碼如下:

<?php
/*

reader:
    這是自己寫的無限極分類實現(xiàn)方法 里面的編輯很簡單的(你們懂得。) 就沒寫了
    沒有進行移動操作 小弟能力有限忘大家多多包涵啊
第二種方法:
CREATE TABLE `types` (
  `type_id` int(11) NOT NULL AUTO_INCREMENT,
  `type_name` varchar(20) NOT NULL,
  `type_p_id` varchar(64) NOT NULL,
  PRIMARY KEY (`type_id`),
  KEY `type_name` (`type_name`),
  KEY `tname` (`type_name`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
注:
 這里沒做字段有效驗證;
 type_id     表示主鍵自增
 type_name     表示分類名
 type_p_id     表示分類路徑 這里的分類路徑是 上層的分類id 如果是當前分類 這個值為 0
*/
error_reporting(7);
header("Content-type:text/html;charset=utf-8");
//這里先進行操作的判斷
$arr = array('list','add','del','edit','addok','edit_ok');
$act= in_array($_GET['ac'],$arr)?$_GET['ac']:$arr[0];
//連接數(shù)據(jù)庫
$conn = mysql_connect("localhost","root","root")or die('數(shù)據(jù)庫連接失敗');
mysql_select_db("study",$conn);
mysql_query("set names utf8");

if($act=="add"){
    /**
    *    @access public
    * @param array $types 數(shù)組 這里的數(shù)組要傳引用&
    *    @param interal $pid 所屬的分類上層分類id
    *    @param int $path 分類層 默認從0開始每次循環(huán)加一
    * @return array();
    */
function getTypes(&$types=array(),$pid=0,$path=0){
     $sql = "select * from `type` where type_p_id = $pid";
    $res = mysql_query($sql);
    while($row = mysql_fetch_assoc($res)){
        $str = "";
        for($i=0;$i<$path;$i++){
            $str.="&#12288;";
        }
        $row['new_type_name'] = $str.$row['type_name'];
        $types[] = $row;
        getTypes($types,$row['type_id'],($path+1));
    }
    return $types;
}
//獲取分類 調(diào)用函數(shù)
getTypes($types);
//獲取列表根據(jù) 分類層進行排序
$sql1 = "select * from type order by type_id";
$sqll = mysql_query($sql1);
?>
<form method="post" action="?ac=addok">
新分類名稱:    <input type="text" name="type_name"><br/>
當前分類:<select name="type_id" >
    <option  value="0">頂級分類</option>
    <?php
    //循環(huán)這個數(shù)組將分類正確輸出
    for($i=0;$i<count($types);$i++){
    ?>
        <option value="<?php echo $types[$i]['type_id']?>"><?php  echo $types[$i]['new_type_name']?></option>
    <?php
    }
    ?>
    </select><br />
    <input type="submit"  value="添加"/>
</form>
<?php
}elseif($act == "addok"){
    $type_name = $_POST['type_name'];
    $type_id = $_POST['type_id'];
    $sql = "insert into `type`(type_name,type_p_id) values ('$type_name','$type_id')";
    $res = mysql_query($sql);
    if(mysql_insert_id()>0){
        echo "<script>location.href='two.php?act=list'</script>";
    }else{
        echo "<script>alert('添加失敗');</script>";
    }
}elseif($act == "list"){
    //獲取列表根據(jù) 分類層進行排序
    $sql = "select * from type order by concat(type_id,type_p_id)";
    $res = mysql_query($sql);
?>
<table width="50%" style="font-size:12px;margin-left:20%;">
<tr><td>id</td><td>分類名</td><td>path路徑</td><td>操作</td></tr>
<?php
while($arr = mysql_fetch_assoc($res)){?>
<tr>
    <td><?php echo $arr['type_id']?></td>
    <td><?php echo $arr['type_name']?></td>
    <td><?php echo $arr['type_p_id']?></td>
    <td ><a href="?ac=edit&type_id=<?php echo $arr['type_id'];?>">編輯</a> |
        <a href="?ac=del&type_id=<?php echo $arr['type_id'];?>">刪除</a></td>
</tr>
<?php
}
?>
</table>
<input type="button"  value="添加"  onclick="(location.href='?ac=add')">
<?php
}elseif($act == "del"){
    /***
        這里要刪除大分類的時候必須要刪除該大分類下的子分類
        所以這里開始mysql事務(wù) mysql 事務(wù)開啟的方式 事務(wù)詳細說明請參考
    */
    mysql_query("SET AUTOCOMMIT=1");//開啟事務(wù)
    $type_id = $_GET['type_id'];
    //刪除該分類
    $sqlone = "delete from `type` where type_id=$type_id";
    //刪除該分類下的子分類
    $sqltwo = "delete from `type`where type_p_id=$type_id";
    $res1 = mysql_query($sqlone);
    $res2 =    mysql_query($sqltwo);
    if($res1 && $res2)
    {
        mysql_query("COMMIT");
        echo "<script>location.href='two.php?act=list'</script>";
    }else{
            mysql_query("ROLLBACK");
        echo "<script>alert('刪除失敗');</script>";
    }
}
?>


type表:

 

下面是效果圖

寫的確實不怎么樣啊 還望大家見諒。

相關(guān)文章

  • 淺談laravel-admin form中的數(shù)據(jù),在提交后,保存前,獲取并進行編輯

    淺談laravel-admin form中的數(shù)據(jù),在提交后,保存前,獲取并進行編輯

    今天小編就為大家分享一篇淺談laravel-admin form中的數(shù)據(jù),在提交后,保存前,獲取并進行編輯,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • 最新評論