search
HomeBackend DevelopmentPHP Tutorialphp无限极分类实现的两种解决方法_PHP

今天写了下无限极分类 下面就把代码贴上来了 写的不怎么样。

method of classify one
复制代码 代码如下:
/*

reader: 这是自己写的无限极分类实现方法 里面的编辑方法只是对分类名进行了编辑
没有进行移动操作 小弟能力有限忘大家多多包涵啊

 第一种方法: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];
//连接数据库
$conn = mysql_connect("localhost","root","root")or die('数据库连接失败');
mysql_select_db("study",$conn);
mysql_query("set names utf8");
//根据分类层进行排序
$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{
        //根据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);
        //获取最后执行的id 然后进行数据更新 主要更新 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"){
?>


新分类名称:   

当前分类:

   

}elseif($act == "list"){
    //获取列表根据 分类层进行排序
    $sql = "select * from types order by type_p_id";
    $res = mysql_query($sql);
?>


while($arr = mysql_fetch_assoc($res)){?>

   
   
   
   

    }
?>
id 分类名 path路径 操作
编辑 |
        删除


}elseif($act == "edit"){
    $id = $_GET['type_id'];
    $sql = "select *from  `types` where type_id=$id ";
    $res = mysql_query($sql);
    echo "
";
    while($arr = mysql_fetch_assoc($res)){
        echo "当前名称:{$arr['type_name']}"."
";
        echo "新名称:"."
";
        echo "";
    }
    echo "";
    echo "
";
}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
复制代码 代码如下:
/*

reader:
    这是自己写的无限极分类实现方法 里面的编辑很简单的(你们懂得。) 就没写了
    没有进行移动操作 小弟能力有限忘大家多多包涵啊
第二种方法:
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];
//连接数据库
$conn = mysql_connect("localhost","root","root")or die('数据库连接失败');
mysql_select_db("study",$conn);
mysql_query("set names utf8");

if($act=="add"){
    /**
    *    @access public
    * @param array $types 数组 这里的数组要传引用&
    *    @param interal $pid 所属的分类上层分类id
    *    @param int $path 分类层 默认从0开始每次循环加一
    * @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            $str.=" ";
        }
        $row['new_type_name'] = $str.$row['type_name'];
        $types[] = $row;
        getTypes($types,$row['type_id'],($path+1));
    }
    return $types;
}
//获取分类 调用函数
getTypes($types);
//获取列表根据 分类层进行排序
$sql1 = "select * from type order by type_id";
$sqll = mysql_query($sql1);
?>


新分类名称:   

当前分类:

   

}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"){
    //获取列表根据 分类层进行排序
    $sql = "select * from type order by concat(type_id,type_p_id)";
    $res = mysql_query($sql);
?>


while($arr = mysql_fetch_assoc($res)){?>

   
   
   
   

}
?>
id 分类名 path路径 操作
编辑 |
        删除


}elseif($act == "del"){
    /***
        这里要删除大分类的时候必须要删除该大分类下的子分类
        所以这里开始mysql事务 mysql 事务开启的方式 事务详细说明请参考
    */
    mysql_query("SET AUTOCOMMIT=1");//开启事务
    $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表:

 

下面是效果图

写的确实不怎么样啊 还望大家见谅。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),