PHP - Generate JSON File

Submitted by: 
Visitors have accessed this post 610 times.

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

And, this is the link for the bootstrap that i used for the layout design

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.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.