Utshaw

Untitled

Aug 24th, 2020
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. <?php
  2. // required headers
  3. header("Access-Control-Allow-Origin: *");
  4. header("Content-Type: application/json; charset=UTF-8");
  5.  
  6. // include database and object files
  7. include_once '../config/database.php';
  8. include_once '../objects/farmercart.php';
  9.  
  10. // instantiate database and farmercart object
  11. $database = new Database();
  12. $db = $database->getConnection();
  13.  
  14. // initialize object
  15. $farmercart = new FarmerCart($db);
  16. // query products
  17. $stmt = $farmercart->read();
  18. $num = $stmt->rowCount();
  19.  
  20. // check if more than 0 record found
  21. if($num>0){
  22.  
  23. // products array
  24. $farmers_arr=array();
  25. $farmers_arr["records"]=array();
  26.  
  27. // retrieve our table contents
  28. // fetch() is faster than fetchAll()
  29. // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
  30. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  31. // extract row
  32. // this will make $row['name'] to
  33. // just $name only
  34. extract($row);
  35.  
  36. $farmer_item=array(
  37. "id" => $id,
  38. "farmer_name" => $farmer_name,
  39. "vegetable_name" => $vegetable_name,
  40. "district_id" => $district_id,
  41. "district_name" => $district_name,
  42. "upazila_name" => $upazila_name,
  43. "upazila_id" => $upazila_id,
  44. "current_address" => $current_address,
  45. "contact" => $contact,
  46. "lat" => $lat,
  47. "date" => $date,
  48. "lng" => $lng
  49.  
  50. );
  51.  
  52. array_push($farmers_arr["records"], $farmer_item);
  53. }
  54.  
  55. // set response code - 200 OK
  56. http_response_code(200);
  57.  
  58. // show products data in json format
  59. echo json_encode($farmers_arr);
  60. }
  61. else{
  62.  
  63. // set response code - 404 Not found
  64. http_response_code(404);
  65.  
  66. // tell the user no products found
  67. echo json_encode(
  68. array("message" => "No products found.")
  69. );
  70. }
Add Comment
Please, Sign In to add comment