CRUD Operation on JSON File using PHP
Submitted by nurhodelta_17 on Wednesday, April 4, 2018 - 21:40.
Getting Started
In the previous tutorial, How to create JSON File from MySQL Database using PHP, we have discussed on how to create a JSON file from MySQL Database. This time, we are going to create a CRUD on JSON file and we're going to use the json file generated in the previous tutorial as our sample json file.Displaying our Data
First, we display the data in our json file in the form of a table and we are going to add the three actions: add, edit and delete. Create a new file, name it as index.php and paste the codes below.- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>CRUD Operation on JSON File using PHP</title>
- </head>
- <body>
- <a href="add.php">Add</a>
- <table border="1">
- <thead>
- <th>ID</th>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Address</th>
- <th>Gender</th>
- <th>Action</th>
- </thead>
- <tbody>
- <?php
- //fetch data from json
- //decode into php array
- $index = 0;
- foreach($data as $row){
- echo "
- <tr>
- <td>".$row->id."</td>
- <td>".$row->firstname."</td>
- <td>".$row->lastname."</td>
- <td>".$row->address."</td>
- <td>".$row->gender."</td>
- <td>
- <a href='edit.php?index=".$index."'>Edit</a>
- <a href='delete.php?index=".$index."'>Delete</a>
- </td>
- </tr>
- ";
- $index++;
- }
- ?>
- </tbody>
- </table>
- </body>
- </html>
Creating our Add Form and Script
Next, we are going to create our add form with the add script whenever the form is submitted. Create a new file, name it as add.php and paste the codes below.- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>CRUD Operation on JSON File using PHP</title>
- </head>
- <body>
- <form method="POST">
- <a href="index.php">Back</a>
- <p>
- <label for="id">ID</label>
- <input type="text" id="id" name="id">
- </p>
- <p>
- <label for="firstname">Firstname</label>
- <input type="text" id="firstname" name="firstname">
- </p>
- <p>
- <label for="lastname">Lastname</label>
- <input type="text" id="lastname" name="lastname">
- </p>
- <p>
- <label for="address">Address</label>
- <input type="text" id="address" name="address">
- </p>
- <p>
- <label for="gender">Gender</label>
- <input type="text" id="gender" name="gender">
- </p>
- <input type="submit" name="save" value="Save">
- </form>
- <?php
- //open the json file
- //data in out POST
- 'id' => $_POST['id'],
- 'firstname' => $_POST['firstname'],
- 'lastname' => $_POST['lastname'],
- 'address' => $_POST['address'],
- 'gender' => $_POST['gender']
- );
- //append the input to our array
- $data[] = $input;
- //encode back to json
- }
- ?>
- </body>
- </html>
Creating our Edit Form and Script
Next, we create our edit form containing the data of selected row/item in our data with the script to update our json if the form is submitted. Create a new file, name it as edit.php and paste the codes below.- <?php
- //get the index from URL
- $index = $_GET['index'];
- //get json data
- //assign the data to selected index
- $row = $data_array[$index];
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>CRUD Operation on JSON File using PHP</title>
- </head>
- <body>
- <form method="POST">
- <a href="index.php">Back</a>
- <p>
- <label for="id">ID</label>
- <input type="text" id="id" name="id" value="<?php echo $row->id; ?>">
- </p>
- <p>
- <label for="firstname">Firstname</label>
- <input type="text" id="firstname" name="firstname" value="<?php echo $row->firstname; ?>">
- </p>
- <p>
- <label for="lastname">Lastname</label>
- <input type="text" id="lastname" name="lastname" value="<?php echo $row->lastname; ?>">
- </p>
- <p>
- <label for="address">Address</label>
- <input type="text" id="address" name="address" value="<?php echo $row->address; ?>">
- </p>
- <p>
- <label for="gender">Gender</label>
- <input type="text" id="gender" name="gender" value="<?php echo $row->gender; ?>">
- </p>
- <input type="submit" name="save" value="Save">
- </form>
- <?php
- //set the updated values
- 'id' => $_POST['id'],
- 'firstname' => $_POST['firstname'],
- 'lastname' => $_POST['lastname'],
- 'address' => $_POST['address'],
- 'gender' => $_POST['gender']
- );
- //update the selected index
- $data_array[$index] = $input;
- //encode back to json
- }
- ?>
- </body>
- </html>
Creating our Delete Script
Lastly, we create the script that deletes the row/item in our json file. Create a new file, name it as delete.php and paste the codes below.- <?php
- //get the index
- $index = $_GET['index'];
- //fetch data from json
- //delete the row with the index
- //encode back to json
- ?>
Comments
Add new comment
- Add new comment
- 3635 views