删除:
步骤:
- 创建一个lesson_2.php文件作为页面文件;functions.php作为方法封装文件;del.php作为信息删除文件;product.json文件为信息存储的文件
- 首先我们将代码进行一小部分优化;把方法封装
- 信息删除时进行一个页面的跳转
知识点:
- 引入外部文件的方法:include:'外部文件名'
- 方法需要return;
- 时间戳:time()
- 打印:print_r ()、var_dump()
- 页面跳转:header("location:跳转的页面")
- unset():删除对象
- unlink():删除文件
- 随机数:rand()
- 拓展:rand(),mt_rand()
- mt_rand()比rand()速度快四倍
lesson_2.php:
<?php
include 'functions.php';
$save_info = get_json();
if (!empty($_FILES['desc_pic']['name'][0])) {
$desc_pic_a = $_FILES['desc_pic']['tmp_name'];
if (!empty($desc_pic_a)) {
$desc_pic = [];
//有图才遍历,否则会error
//随机数
//rand(1,99)
foreach ($desc_pic_a as $key => $value) {
$file_type = $_FILES['desc_pic']['type'][$key];
//字符串分割
//explode
list($type,$houzui) = explode("/",$file_type);
//
$first_name = time().rand(1000,9999);
copy($value,'data/'.$first_name.'.'.$houzui);
}
}
// echo move_uploaded_file($_FILES['thumb']['tmp_name'],"data/222.jpg");
}
if (!empty($_POST['product_name'])) {
$product_name = $_POST['product_name'];
$price = $_POST['price'];
// $desc_pic[] = $_POST['desc_pic'];
$save_info[] = ['product_name'=> $product_name,'price'=> $price]; //二维数组 关联数组
if ($product_name!=''||$price!='') {
set_json($save_info);
// json_encode 数组转字符串 json_decode 字符串转换成数组
}
}else{
//没有 显示列表
echo('没有就显示列表');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>第二个表单</title>
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<form action="" class="form" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="">商品名称</label>
<input type="text" name="product_name" class="form-cotrol">
</div>
<!-- <div class="form-group">
<label for="">商品图片</label>
<input type="file" name="thumb" class="form-cotrol" >
</div> -->
<div class="form-group">
<label for="">商品价格</label>
<input type="text" name="price" class="form-cotrol">
</div>
<div class="form-group">
<label for="">商品描述图</label>
<input type="file" name="" class="form-cotrol" multiple>
</div>
<div class="form-group">
<!-- primary: 绿色 danger:红色 -->
<input type="submit" class="btn btn-primary" value="保存"></input>
</div>
</form>
</div>
<form action="del.php">
<table class="table">
<tr>
<th width="120"><input type="checkbox" name="">全选/反选<input type="submit" value="删除" class="btn btn-danger"></th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品图片</th>
</tr>
<!-- {$value['desc_pic[]']} -->
<?php
if(!empty($save_info)){
foreach($save_info as $key => $value) {
echo "<tr>
<td><input type='checkbox' name='del_index[]' value='{$key}' >{$key}</td>
<td>{$value['product_name']}</td>
<td>{$value['price']}</td>
<td></td>
</tr>";
}
}
?>
</table>
</form>
</div>
</body>
</html>
functions.php:
<?php
/**
* 获取json文件内容
* @return array 信息列表
*/
// 获取json
function get_json(){
$old_info = file_get_contents('data/product.json');//获取旧的信息 (字符串)
return json_decode($old_info,true);//转换成数组
}
/**
* 添加json文件内容
*/
function set_json($save_info){
file_put_contents("data/product.json",json_encode($save_info));
}
?>
del.php:
<?php
// var_dump($_GET);
if ($_GET['del_index']) {
include 'functions.php';
$list = get_json();
foreach ($_GET['del_index'] as $value) {
unset($list[$value]);
}
// var_dump($list);
set_json($list);
}
// 跳转页面
header("location:lesson_2.php")
?>
product.json文件为空文件。
小case bug:
- 保存信息后刷新;会添加之前添加的信息
未实现:
- 全选