PHP - Generate JSON File

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. tut1

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.
  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_generate");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Generate JSON File</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <a class="btn btn-primary" href="generate.php"><span class="glyphicon glyphicon-save"></span> Generate</a>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Age</th>
  25. <th>Address</th>
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <?php
  30. require 'conn.php';
  31. $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  32. while($fetch = mysqli_fetch_array($query)){
  33. ?>
  34. <tr>
  35. <td><?php echo $fetch['firstname']?></td>
  36. <td><?php echo $fetch['lastname']?></td>
  37. <td><?php echo $fetch['age']?></td>
  38. <td><?php echo $fetch['address']?></td>
  39. </tr>
  40. <?php
  41. }
  42. ?>
  43. </tbody>
  44. </talbe>
  45. </div>
  46. </body>
  47. </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.
  1. <?php
  2. require 'conn.php';
  3.  
  4. $output = "";
  5.  
  6. header("Content-Type: application/txt");
  7. header("Content-Disposition: attachment; filename=json.txt");
  8. header("Pragma: no-cache");
  9. header("Expires: 0");
  10.  
  11. $query = $conn->query("SELECT * FROM `member`");
  12.  
  13. $array = array();
  14.  
  15. while($fetch = $query->fetch_assoc()){
  16. $array[] = $fetch;
  17. }
  18.  
  19. $output .=
  20. "<pre>".print_r($array).'</pre>';
  21.  
  22. ?>
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!

Add new comment