php+html
PHP:
知識點:
- json_encode 數組轉字符串
- json_decode 字符串轉換成數組 默認不會把對象轉換成數組:json_decode(對象,true);//轉換成數組
- get和post的區別?
1.get參數顯示在地址欄,不安全,信息泄露
2.get比post數據量少很多
3.傳文件只能用post - multiple:多個文件上傳
<?php
$old_info = file_get_contents('data/product.json');//獲取舊的信息 (字符串)
$save_info = json_decode($old_info,true);//轉換成數組
if (!empty($_FILES)) {
$desc_pic_a = $_FILES['desc_pic']['tmp_name'];
if (!empty($desc_pic_a)) {
//有圖才遍歷,否則會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 (count($_GET)>0) {
$product_name = $_GET['product_name'];
$price = $_GET['price'];
$save_info[] = ['product_name'=> $product_name,'price'=> $price]; //二維數組 關聯數組
file_put_contents("data/product.json", json_encode($save_info));
// json_encode 數組轉字符串 json_decode 字符串轉換成數組
}else{
//沒有 顯示列表
echo('沒有就顯示列表');
}
?>
html:
<!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="desc_pic[]" class="form-cotrol" multiple>
</div>
<div class="form-group">
<!-- primary: 綠色 danger:紅色 -->
<input type="submit" class="btn btn-primary" value="保存"></input>
</div>
</form>
</div>
<table class="table">
<tr>
<th>商品類型</th>
<th>商品名稱</th>
<th>商品價格</th>
<th>商品圖片</th>
</tr>
<?php
if(!empty($save_info)){
foreach($save_info as $key => $value) {
echo "<tr>
<td></td>
<td>{$value['product_name']}</td>
<td>{$value['price']}</td>
<td></td>
</tr>";
}
}
?>
</table>
</div>
</body>
</html>