Skip to content

Instantly share code, notes, and snippets.

@ziedrebhi
Last active October 13, 2017 22:51
Show Gist options
  • Save ziedrebhi/e3d3001b626f21e26d39 to your computer and use it in GitHub Desktop.
Save ziedrebhi/e3d3001b626f21e26d39 to your computer and use it in GitHub Desktop.
<?php
/*
* Following code will list all the categories
*/
// 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 categories from categories table
$result = mysqli_query($con->myconn, "SELECT * FROM categories") or die(mysql_error());
// check for empty result
if (mysqli_num_rows($result) > 0) {
// looping through all results
// categories node
$response["categories"] = array();
while ($row = mysqli_fetch_array($result)) {
// temp user array
$categorie = array();
$categorie["id"] = $row["id"];
$categorie["name_cat"] = $row["name_cat"];
$categorie["image_cat"] = $row["image_cat"];
array_push($response["categories"], $categorie);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no categories found
$response["success"] = 0;
$response["message"] = "No categories found";
// echo no categories JSON
echo json_encode($response);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment