Skip to content

Instantly share code, notes, and snippets.

@ziedrebhi
Created April 24, 2015 14:24
Show Gist options
  • Save ziedrebhi/74572d56b7b2ca478a70 to your computer and use it in GitHub Desktop.
Save ziedrebhi/74572d56b7b2ca478a70 to your computer and use it in GitHub Desktop.
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$con = new DB_CONNECT();
$con->connect();
// get all products from products table
$result = mysqli_query($con->myconn, "SELECT * FROM products") or die(mysql_error());
// check for empty result
if (mysqli_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysqli_fetch_array($result)) {
// temp user array
$product = array();
$product["id"] = $row["id"];
$product["name_prod"] = $row["name_prod"];
$product["price_prod"] = $row["price_prod"];
$product["desc_prod"] = $row["desc_prod"];
$product["image_prod"] = $row["image_prod"];
$product["id_cat"] = $row["id_cat"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment