Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // required headers
- header("Access-Control-Allow-Origin: *");
- header("Content-Type: application/json; charset=UTF-8");
- // include database and object files
- include_once '../config/database.php';
- include_once '../objects/farmercart.php';
- // instantiate database and farmercart object
- $database = new Database();
- $db = $database->getConnection();
- // initialize object
- $farmercart = new FarmerCart($db);
- // query products
- $stmt = $farmercart->read();
- $num = $stmt->rowCount();
- // check if more than 0 record found
- if($num>0){
- // products array
- $farmers_arr=array();
- $farmers_arr["records"]=array();
- // retrieve our table contents
- // fetch() is faster than fetchAll()
- // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
- while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- // extract row
- // this will make $row['name'] to
- // just $name only
- extract($row);
- $farmer_item=array(
- "id" => $id,
- "farmer_name" => $farmer_name,
- "vegetable_name" => $vegetable_name,
- "district_id" => $district_id,
- "district_name" => $district_name,
- "upazila_name" => $upazila_name,
- "upazila_id" => $upazila_id,
- "current_address" => $current_address,
- "contact" => $contact,
- "lat" => $lat,
- "date" => $date,
- "lng" => $lng
- );
- array_push($farmers_arr["records"], $farmer_item);
- }
- // set response code - 200 OK
- http_response_code(200);
- // show products data in json format
- echo json_encode($farmers_arr);
- }
- else{
- // set response code - 404 Not found
- http_response_code(404);
- // tell the user no products found
- echo json_encode(
- array("message" => "No products found.")
- );
- }
Add Comment
Please, Sign In to add comment