In this tutorial we will create a
Generate JSON File using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...
Getting Started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server
https://www.apachefriends.org/index.html.
And, this is the link for the bootstrap that i used for the layout design
https://getbootstrap.com/.
Creating Database
Open your database web server then create a database name in it db_daily, after that click Import then locate the database file inside the folder of the application then click ok.
Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it
conn.php.
<?php
if(!$conn){
die("Error: Failed to connect to database!"); }
?>
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as
index.php.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">PHP - Generate JSON File</h3>
<hr style="border-top:1px dotted #ccc;"/>
<a class="btn btn-primary" href="generate.php"><span class="glyphicon glyphicon-save"></span> Generate</a>
<br /><br />
<table class="table table-bordered">
<thead class="alert-info">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
require 'conn.php';
?>
<tr>
<td><?php echo $fetch['firstname']?></td>
<td><?php echo $fetch['lastname']?></td>
<td><?php echo $fetch['age']?></td>
<td><?php echo $fetch['address']?></td>
</tr>
<?php
}
?>
</tbody>
</talbe>
</div>
</body>
</html>
Creating the Main Function
This code contains the main function of the application. This code will generate a json file when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as
generate.php.
<?php
require 'conn.php';
$output = "";
header("Content-Type: application/txt"); header("Content-Disposition: attachment; filename=json.txt");
$query = $conn->query("SELECT * FROM `member`");
while($fetch = $query->fetch_assoc()){
$array[] = $fetch;
}
$output .=
?>
There you have it we successfully created
Generate JSON File using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!