Sensation Energy

Tutorials


Content


Advertising


Function 7

JSON PHP




JSON PHP version 1


Id First name Last name Email Year Actiona

JSON PHP Documentation

PHP JSON is used to read value from a web server and print data to a website.


Select (index.php)

Select allows us to list all the data in the table. Here, all columns from the table are selected unconditionally and printed


<table>
<thead>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Email</a></th>
<th>Year</th>
<th>Actiona</th>
</tr>
</thead>
<tbody>
<?php
$uzmi = file_get_contents('base_v1.json');
$dzejson = json_decode($uzmi);
foreach ($dzejson->lista as $id => $assignment):
?>
<tr>
<td><?php echo $id+1; ?></td>
<td><?php echo $assignment->name; ?></td>
<td><?php echo $assignment->last_name; ?></td>
<td><?php echo $assignment->email; ?></td>
<td><?php echo $assignment->year; ?></td>
<td><a href="view.php?id=<?php echo $id; ?>">View</a>
<a href="edit.php?id=<?php echo $id; ?>">Edit</a>
<a href="delete.php?id=<?php echo $id; ?>">Delete></a>
</td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>

View (view.php)

The view allows us to see only the individual values from the table via id. From select.php, click View, and then go to the View.php page. It is important to emphasize that this ID is important if the value of id 1 shows all data related to 1


<table>
<thead>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Email</a></th>
<th>Year</th>
</tr>
</thead>
<tbody>
<?php
if (isset($_GET["id"])) {
$id = (int) $_GET["id"];
$getfile = file_get_contents('base_v1.json');
$jsonfile = json_decode($getfile, true);
$jsonfile = $jsonfile["lista"];
$jsonfile = $jsonfile[$id];
}
if (isset($_GET["id"])): ?>
<tr>
<td><?php echo $id+1 ?></td>
<td"><?php echo  $jsonfile["name"];  ?></td>
<td><?php echo  $jsonfile["last_name"]; ?></td>
<td><?php echo  $jsonfile["email"]; ?></td>
<td><?php echo  $jsonfile["year"]; ?></td>
</tr>
<?php endif; ?>
</tbody>
</table>

Insert (add.php)

Insert allows us to insert data into the database. It is important that the name = "add" long and ame = "name" input from the form match if (isset ($ _ POST ['add'])) {and $ name = $ _POST ['name'] ;, insert jus those values we set.


<?php
if (isset($_POST["dodaj"])) {
$add = file_get_contents('base_v1.json');
$vlada = json_decode($add, true);
unset($_POST["add"]);
$vlada["lista"] = array_values($vlada["lista"]);
array_push($vlada["lista"], $_POST);
file_put_contents("base_v1.json", json_encode($vlada, JSON_PRETTY_PRINT));
header("Location: index.php");
}
?>
<form action="" method="post">
<div class="form-group">
<label for="thread_title">Id:</label>
<input type="text" name="id" class="form-input" />
</div>
<div class="form-group">
<label for="thread_title">First name:</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="form-group">
<label for="thread_title">Last name:</label>
<input type="text" name="last_name"  class="form-input" />
</div>
<div class="form-group">
<label for="thread_title1">Email:</label>
<input type="text" name="email" class="form-input">
</div>
<div class="form-group">
<label for="datum">Year</label>
<input type="number" name="year" class="form-input">
</div>
<br>
<div class="btn-group">
<button type="submit" name="add" class="btn-3" >Add</button>
</div>
</form>


Edit (edit.php)

Edit lets we edit data from the table using ID. Also from selekt.php we chose edit.php and in the value of the ID it shows all the data related to that ID.


<?php
if (isset($_GET["id"])) {
$id = (int) $_GET["id"];
$getfile = file_get_contents('base_v1.json');
$jsonfile = json_decode($getfile, true);
$jsonfile = $jsonfile["lista"];
$jsonfile = $jsonfile[$id];
}
if (isset($_POST["id"])) {
$id = (int) $_POST["id"];
$getfile = file_get_contents('base_v1.json');
$all = json_decode($getfile, true);
$jsonfile = $all["lista"];
$jsonfile = $jsonfile[$id];
$post["name"] = isset($_POST["name"]) ? $_POST["name"] : "";
$post["last_name"] = isset($_POST["last_name"]) ? $_POST["last_name"] : "";
$post["year"] = isset($_POST["year"]) ? $_POST["year"] : "";
$post["email"] = isset($_POST["email"]) ? $_POST["email"] : "";
if ($jsonfile) {
unset($all["lista"][$id]);
$all["lista"][$id] = $post;
$all["lista"] = array_values($all["lista"]);
file_put_contents("base_v1.json", json_encode($all, JSON_PRETTY_PRINT));
}
header("Location: index.php");
}
?>
<?php if (isset($_GET["id"])){ ?>
<h2>Edit ID: <?php echo $id+1 ?></h2>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" value="<?php echo $id ?>" name="id"/>
<div class="form-group">
<label><h2>First name</h2></label>
<input type="text" name="name" class="form-input" value="<?php echo $jsonfile["name"]; ?>">
</div>
<div class="form-group">
<label><h2>Last name</h2></label>
<input type="text" name="last_name" class="form-input" value="<?php echo $jsonfile["last_name"]; ?>">
</div>
<div class="form-group">
<label><h2>Email</h2></label>
<input type="text" name="email" class="form-input" value="<?php echo $jsonfile["email"];  ?>">
</div>
<div class="form-group">
<label><h2>Year</h2></label>
<input type="text" class="form-input" name="year" value="<?php echo $jsonfile["year"]; ?>">
</div>
<div class="btn-group">
<input type="submit" class="btn-1" name="edit" value="Edit">
</div>
</form>
<?php
}
?>

Delete (delete.php)

Delete allows us to delete data from the table. Of course via id. From select.php we delete and click delete and all values related to the ID we selected will be deleted.


<?php
if (isset($_GET["id"])) {
$id = (int) $_GET["id"];
$all = file_get_contents('base_v1.json');
$all = json_decode($all, true);
$jsonfile = $all["lista"];
$jsonfile = $jsonfile[$id];
if ($jsonfile) {
unset($all["lista"][$id]);
$all["lista"] = array_values($all["lista"]);
file_put_contents("base_v1.json", json_encode($all));
}
header("Location: index.php");
}
?>

Database (base_v1.json)


{
"lista":
[
{
"id":"2",
"name":"admin",
"last_name":"admin",
"email":"admin@mail.com",
"year":"20"
}
]
}