Created
April 24, 2015 14:23
-
-
Save ziedrebhi/6d3a8a8efc643efb0724 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 get single product details | |
* A product is identified by product id (id) | |
*/ | |
// 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(); | |
// check for post data | |
if (isset($_GET["id"])) { | |
$id = $_GET['id']; | |
// get a product from products table | |
$result = mysqli_query($con->myconn, "SELECT * FROM products WHERE id = $id"); | |
if (!empty($result)) { | |
// check for empty result | |
if (mysqli_num_rows($result) > 0) { | |
$result = mysqli_fetch_array($result); | |
$product = array(); | |
$product["id"] = $result["id"]; | |
$product["name_prod"] = $result["name_prod"]; | |
$product["price_prod"] = $result["price_prod"]; | |
$product["desc_prod"] = $result["desc_prod"]; | |
$product["image_prod"] = $result["image_prod"]; | |
$product["id_cat"] = $result["id_cat"]; | |
// success | |
$response["success"] = 1; | |
// user node | |
$response["product"] = array(); | |
array_push($response["product"], $product); | |
// echoing JSON response | |
echo json_encode($response); | |
} else { | |
// no product found | |
$response["success"] = 0; | |
$response["message"] = "No product found"; | |
// echo no users JSON | |
echo json_encode($response); | |
} | |
} else { | |
// no product found | |
$response["success"] = 0; | |
$response["message"] = "No product found"; | |
// echo no users JSON | |
echo json_encode($response); | |
} | |
} else { | |
// required field is missing | |
$response["success"] = 0; | |
$response["message"] = "Required field(s) is missing"; | |
// echoing JSON response | |
echo json_encode($response); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment