Advertisement
Riju18

no.91_fetch_value_as_json_format_json_decode

Apr 9th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. database info:
  2. ----------------
  3. ----------------
  4. $servername = "localhost";
  5. $username = "root";
  6. $password = "";
  7. $dbname = "webservice";
  8.  
  9. table name: items
  10. no of columns: 4
  11. columns:
  12.          id int auto increment
  13.          name varchar(100)
  14.          description varchar(100)
  15.          price int(100)            // value not more than 1000
  16. --------------------------
  17.  
  18.  
  19. db_connect.php:
  20. ---------------
  21. ----------------
  22.  
  23. <?php
  24. /* Database connection start */
  25. $servername = "localhost";
  26. $username = "root";
  27. $password = "";
  28. $dbname = "webservice";
  29. $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
  30. if (mysqli_connect_errno()) {
  31.     printf("Connect failed: %s\n", mysqli_connect_error());
  32.     exit();
  33. }
  34. ?>
  35.  
  36. index.php:
  37. ------------
  38. ------------
  39.  
  40. <!DOCTYPE html>
  41. <html lang="en">
  42. <head>
  43.     <meta charset="UTF-8">
  44.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  45.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  46.     <title>index</title>
  47. </head>
  48. <body>
  49.     <title> Demo Create Basic Web Service with PHP, MySQL, XML and JSON</title>
  50.     <div>
  51.         <h2><h2 style="text-align:center">Create Basic Web Service with PHP, MySQL, XML and JSON</h2>
  52.         <br><br>
  53.         <a href="result.php?price=1000&num=10&format=xml" target="_blank"><p style="text-align:center">XML</a> <!-- link as xml -->
  54.         <br><br>
  55.         <a href="result.php?price=1000&num=10&format=json" target="_blank"><p style="text-align:center">JSON</a> <!-- link as json -->
  56.     </div>
  57. </body>
  58. </html>
  59.  
  60. result.php:
  61. -----------
  62. -----------
  63. <?php
  64. include_once("db_connect.php");
  65. if(isset($_GET['price'])) {   // in url bar if price is set
  66.  
  67.     $price = $_GET['price'];    // assign price in a var
  68.     $no_of_post = (isset($_GET['num'])?$_GET['num']:10);  // condition to hold number of posts
  69.     $format = (isset($_GET['format'])?$_GET['format']:"xml"); // condition to hold format
  70.     $sql_query = "SELECT id, name, description, price FROM items WHERE price <= $price ORDER BY price LIMIT $no_of_post"; // db query
  71.     $resultset = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn)); // connection check
  72.     $products = array();
  73.  
  74.     if(mysqli_num_rows($resultset) > 0) {
  75.         while($product = mysqli_fetch_assoc($resultset)) {
  76.             $products[] = array('product'=>$product);
  77.         }
  78.     }
  79.  
  80.     /* output result in required format */
  81.     if($format == 'json') {
  82.         header('Content-type: application/json');
  83.         echo json_encode($products);
  84.     } else if($format == 'xml') {
  85.         header('Content-type: text/xml');
  86.         echo '<products>';
  87.         foreach($products as $index => $product) {
  88.             if(is_array($product)) {
  89.                 foreach($product as $key => $value) {
  90.                     echo '<',$key,'>';
  91.                     if(is_array($value)) {
  92.                         foreach($value as $tag => $val) {
  93.                             echo '<',$tag,'>',$val,'</',$tag,'>';
  94.                         }
  95.                     }
  96.                     echo '</',$key,'>';
  97.                 }
  98.             }
  99.         }
  100.         echo '</products>';
  101.     }
  102.  
  103.     @mysqli_close($link);
  104. }
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement