PHP MySQLi CRUD Operations on JSON File

PHP MySQLi CRUD Operations on JSON File

In this Post We Will Explain About is PHP MySQLi CRUD Operations on JSON File With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to PHP CRUD JSON file instead of mysql database Example

In this post we will show you Best way to implement Simple PHP MySQL JSON File CRUD(insert update delete), hear for PHP CRUD Application Using Data From JSON File with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Displaying our Data

Make a new PHP file, name Like as index.php and simple copy and paste the source codes below.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>PHP CRUD JSON file instead of mysql database</title>
</head>
<body>
<a href="add.php">Add</a>
<table border="1">
	<thead>
		<th>ID</th>
		<th>Article Title</th>
		<th>Article Name</th>
		<th>Article Desc</th>
		<th>Article View</th>
		<th>Action</th>
	</thead>
	<tbody>
		<?php
			//Retrive jsonArr from json
			$jsonArr = file_get_contents('articleList.json');
			//jsin decode into php array
			$jsonArr = json_decode($jsonArr);
 
			$index = 0;
			foreach($jsonArr as $aricle_rw){
				echo "
					<tr>
						<td>".$aricle_rw->id."</td>
						<td>".$aricle_rw->artcle_title."</td>
						<td>".$aricle_rw->artcle_name."</td>
						<td>".$aricle_rw->artcle_desc."</td>
						<td>".$aricle_rw->views."</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 simple Add Form

And then, i am gonna to make simple add HTML form with the put the simple script whenever the HTML form is submitted. Make a new simple file, name Like as add.php and sopy paste the source codes below.

Also Read :   AngularJS Parsing JSON data RESTful JSON Parsing

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>PHP CRUD JSON file instead of mysql database</title>
</head>
<body>
<form method="POST">
	<a href="index.php">Back</a>
	<p>
		<label for="id">Artcle ID</label>
		<input type="text" id="id" name="id">
	</p>
	<p>
		<label for="artcle_title">Artcle title</label>
		<input type="text" id="artcle_title" name="artcle_title">
	</p>
	<p>
		<label for="artcle_name">Artcle Name</label>
		<input type="text" id="artcle_name" name="artcle_name">
	</p>
	<p>
		<label for="artcle_desc">Artcle Desc</label>
		<input type="text" id="artcle_desc" name="artcle_desc">
	</p>
	<p>
		<label for="views">views</label>
		<input type="text" id="views" name="views">
	</p>
	<input type="submit" name="save" value="Save">
</form>
 
<?php
	if(isset($_POST['save'])){
		//here PHP open the json file
		$jsonArr = file_get_contents('articleList.json');
		$jsonArr = json_decode($jsonArr);
 
		//jsonArr in out POST
		$input = array(
			'id' => $_POST['id'],
			'artcle_title' => $_POST['artcle_title'],
			'artcle_name' => $_POST['artcle_name'],
			'artcle_desc' => $_POST['artcle_desc'],
			'views' => $_POST['views']
		);
 
		//append the input to our array
		$jsonArr[] = $input;
		//encode back to json
		$jsonArr = json_encode($jsonArr, JSON_PRETTY_PRINT);
		file_put_contents('articleList.json', $jsonArr);
 
		header('location: index.php');
	}
?>
</body>
</html>

Creating Simple Edit Form

Make a new PHP file, name Like as server side edit.php as well as copy paste the source codes below.

Also Read :   Bootstrap Toggle switch with ajax update to mysql in PHP

<?php
	//get the index from URL
	$index = $_GET['index'];
 
	//get json jsonArr
	$jsonArr = file_get_contents('articleList.json');
	$data_array = json_decode($jsonArr);
 
	//assign the jsonArr to selected index
	$aricle_rw = $data_array[$index];
 
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>PHP CRUD JSON file instead of mysql database</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 $aricle_rw->id; ?>">
	</p>
	<p>
		<label for="artcle_title">Article Title</label>
		<input type="text" id="artcle_title" name="artcle_title" value="<?php echo $aricle_rw->artcle_title; ?>">
	</p>
	<p>
		<label for="artcle_name">Article Name</label>
		<input type="text" id="artcle_name" name="artcle_name" value="<?php echo $aricle_rw->artcle_name; ?>">
	</p>
	<p>
		<label for="artcle_desc">Article Desc</label>
		<input type="text" id="artcle_desc" name="artcle_desc" value="<?php echo $aricle_rw->artcle_desc; ?>">
	</p>
	<p>
		<label for="views">Article View</label>
		<input type="text" id="views" name="views" value="<?php echo $aricle_rw->views; ?>">
	</p>
	<input type="submit" name="save" value="Save">
</form>
 
<?php
	if(isset($_POST['save'])){
		//set the updated values
		$input = array(
			'id' => $_POST['id'],
			'artcle_title' => $_POST['artcle_title'],
			'artcle_name' => $_POST['artcle_name'],
			'artcle_desc' => $_POST['artcle_desc'],
			'views' => $_POST['views']
		);
 
		//update the selected index
		$data_array[$index] = $input;
 
		//encode back to json
		$jsonArr = json_encode($data_array, JSON_PRETTY_PRINT);
		file_put_contents('articleList.json', $jsonArr);
 
		header('location: index.php');
	}
?>
</body>
</html>

Creating Simple Delete Script

Last step, I make the PHP serever sided script that all the records deletes the Mysql table aricle_rw/item in simple json file. Make a PHP new file, name Like as a delete.php here simple copy and paste the source codes below.

Also Read :   Vue js toggle class switch switch show and hide Example

<?php
//PHP CRUD Application Using Data From JSON File
	//get the index
	$index = $_GET['index'];
 
	//Retrive jsonArr from json
	$jsonArr = file_get_contents('articleList.json');
	$jsonArr = json_decode($jsonArr);
 
	//delete the aricle_rw with the index
	unset($jsonArr[$index]);
 
	//encode back to json
	$jsonArr = json_encode($jsonArr, JSON_PRETTY_PRINT);
	file_put_contents('articleList.json', $jsonArr);
 
	header('location: index.php');
?>

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example





Example

I hope you have Got What is PHP CRUD JSON file instead of mysql database And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.