zhengwei1949 / myblog

个人博客
10 stars 6 forks source link

注册案例CRUD文档 #141

Open zhengwei1949 opened 6 years ago

zhengwei1949 commented 6 years ago

注册案例(CRUD 增删改查四大功能做出来)

查询

[
    {
        "id":1,
        "name":"张三1",
        "nickname":"张三昵称",
        "age":20,
        "phone":"13000000000",
        "class":"黑马一期",
        "gender":"男",
        "src":"./upload/monkey.png"
    },
    {
        "id": 2,
        "name": "张三2",
        "nickname": "张三昵称",
        "age": 20,
        "phone": "13000000000",
        "class": "黑马一期",
        "gender": "女",
        "src": "./upload/monkey.png"
    },
    {
        "id": 3,
        "name": "张三3",
        "nickname": "张三昵称",
        "age": 20,
        "phone": "13000000000",
        "class": "黑马一期",
        "gender": "男",
        "src": "./upload/monkey.png"
    },
    {
        "id": 4,
        "name": "张三4",
        "nickname": "张三昵称",
        "age": 20,
        "phone": "13000000000",
        "class": "黑马一期",
        "gender": "女",
        "src": "./upload/monkey.png"
    },
    {
        "id": 5,
        "name": "张三5",
        "nickname": "张三昵称",
        "age": 20,
        "phone": "13000000000",
        "class": "黑马一期",
        "gender": "男",
        "src": "./upload/monkey.png"
    }
]
header('content-type:text/html;charset=utf-8');
$str = file_get_contents('./student.json');
$arr = json_decode($str,true);
<tbody>
    <?php foreach($arr as $key=>$value){?>
    <tr>
        <td><?php echo $value['id'] ?></td>
        <td><?php echo $value['name'] ?></td>
        <td><?php echo $value['nickname'] ?></td>
        <td><?php echo $value['age'] ?></td>
        <td><?php echo $value['phone'] ?></td>
        <td><?php echo $value['gender'] ?></td>
        <td><?php echo $value['class'] ?></td>
        <td><img src="<?php echo $value['src'] ?>"></td>
        <td>
            <a href="#">编辑</a>
            <a href="#">删除</a> 
        </td>
    </tr>
    <?php } ?>
</tbody>

删除

header('content-type:text/html;charset=utf-8');
print_r($_GET);
$str = file_get_contents('./student.json');
$arr = json_decode($str,true);
header('content-type:text/html;charset=utf-8');
// print_r($_GET);
$id = $_GET['id'];
$str = file_get_contents('./student.json');
$arr = json_decode($str,true);
foreach ($arr as $key => $value) {
    if($value['id'] == $id){
        echo '找到了要删除的项';
        break;
    }
}
header('content-type:text/html;charset=utf-8');
// print_r($_GET);
$id = $_GET['id'];
$str = file_get_contents('./student.json');
$arr = json_decode($str,true);
foreach ($arr as $key => $value) {
    if($value['id'] == $id){
        // echo '找到了要删除的项';
        array_splice($arr,$key,1);
        break;
    }
}
header('content-type:text/html;charset=utf-8');
// print_r($_GET);
$id = $_GET['id'];
$str = file_get_contents('./student.json');
$arr = json_decode($str,true);
foreach ($arr as $key => $value) {
    if($value['id'] == $id){
        // echo '找到了要删除的项';
        array_splice($arr,$key,1);
        file_put_contents('./student.json',json_encode($arr));
        break;
    }
}
header('content-type:text/html;charset=utf-8');
// print_r($_GET);
$id = $_GET['id'];
$str = file_get_contents('./student.json');
$arr = json_decode($str,true);
foreach ($arr as $key => $value) {
    if($value['id'] == $id){
        // echo '找到了要删除的项';
        array_splice($arr,$key,1);
        file_put_contents('./student.json',json_encode($arr));
        echo '删除成功';
        header('refresh:2;url=list.php');
        break;
    }
}

新增

header('content-type:text/html;charset=utf-8');
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    print_r($_POST);
    print_r($_FILES);
}
header('content-type:text/html;charset=utf-8');
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // print_r($_POST);
    // print_r($_FILES);
+    if(isset($_FILES['photo']) && $_FILES['photo']['error'] == 0){
+        $file = $_FILES['photo'];
+        $name = time().rand();
+        $ext = strrchr($file['name'],'.');
+        $fileName = './upload/'.$name.$ext;
+        move_uploaded_file($file['tmp_name'],$fileName);
    }
}
header('content-type:text/html;charset=utf-8');
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // print_r($_POST);
    // print_r($_FILES);
    if(isset($_FILES['photo']) && $_FILES['photo']['error'] == 0){
        $file = $_FILES['photo'];
        $name = time().rand();
        $ext = strrchr($file['name'],'.');
        $fileName = './upload/'.$name.$ext;
        move_uploaded_file($file['tmp_name'],$fileName);
+        $str = file_get_contents('./student.json');
+        $arr = json_decode($str,true);

    }
}
header('content-type:text/html;charset=utf-8');
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // print_r($_POST);
    // print_r($_FILES);
    if(isset($_FILES['photo']) && $_FILES['photo']['error'] == 0){
        $file = $_FILES['photo'];
        $name = time().rand();
        $ext = strrchr($file['name'],'.');
        $fileName = './upload/'.$name.$ext;
        move_uploaded_file($file['tmp_name'],$fileName);
        $str = file_get_contents('./student.json');
        $arr = json_decode($str,true);
        $myid = $arr[count($arr) - 1]['id'] + 1;//思考一下为什么要这样写
        echo $myid;
    }
}
header('content-type:text/html;charset=utf-8');
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // print_r($_POST);
    // print_r($_FILES);
    if(isset($_FILES['photo']) && $_FILES['photo']['error'] == 0){
        $file = $_FILES['photo'];
        $name = time().rand();
        $ext = strrchr($file['name'],'.');
        $fileName = './upload/'.$name.$ext;
        move_uploaded_file($file['tmp_name'],$fileName);
        $str = file_get_contents('./student.json');
        $arr = json_decode($str,true);
        $myid = $arr[count($arr) - 1]['id'] + 1;//思考一下为什么要这样写
        // echo $myid;
        $arr[] = [
            "id"=>$myid,
            "name"=>$_POST['username'],
            "nickname"=>$_POST['nickname'],
            "age"=>$_POST['age'],
            "phone"=>$_POST["tel"],
            "class"=>$_POST['class'],
            "gender"=>$_POST['gender'],
            "src"=>$fileName
        ];
        print_r($arr);
    }
}
file_put_contents('./student.json',json_encode($arr));
echo '新增成功';
header('refresh:2;url=list.php');

编辑

显示编辑页面

处理编辑提交的数据