Last active
October 13, 2017 22:51
-
-
Save ziedrebhi/e3d3001b626f21e26d39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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