I want to display treeid, treelatitude and treelongitude by selecting the treecondition column.
When I POST "healthy" I want to display treeid 4,6 & 7, And If I POST "Balanced" want to get treeid 1 & 8. How can I get it?
In my below code I can only able to display any of them
Please help to where exactly I am getting wrong in my code
Treeid treelatitude treelongitude treeCondition
1 12.33 17.22 Balanced
2 12.33 17.22 Healthy
3 12.33 17.33 Dieseased
4 13.44 17.55 Healthy
5 11.32 17.66 Imbalanced
6 12.33 18.33 Healthy
7 14.44 18.44 Healthy
8 11.22 17.22 Balanced
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/public_html/Config.php');
// Connecting to mysql database
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
// json response array
$response = array();
$sq = "SELECT treeid, treelatitude, treelongitude FROM tree WHERE treecondition = ?";
if (isset($_POST['treecondition'])) {
// receiving the post params
$treecondition = $_POST['treecondition'];
// get the tree details for google map marker
if($stmt = $mysqli->prepare($sq)){
$stmt->bind_param("s", $treecondition);
$stmt->execute();
if ($stmt->num_rows) {
while($tree = $stmt->fetch()) {
$treeItem = array();
$treeItem["treeid"] = $tree['treeid'];
$treeItem["treelatitude"] = $tree['treelatitude'];
$treeItem["treelongitude"] = $tree['treelongitude'];
$response[] = $treeItem;
} echo json_encode($response);
}
}else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Tree list view credentials are wrong. Please try again!";
echo json_encode($response);
}
$mysqli->close();
}
I have tried and find answer from myself, even though I am beginner I have achieved to get result. I think PHP expert should look.
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/public_html/Config.php');
// Connecting to mysql database
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
// json response array
$response = array();
if (isset($_POST['treecondition'])) {
// receiving the post params
$treecondition = $_POST['treecondition'];
// get the tree details for google map marker
if($stmt = $mysqli->prepare("SELECT treeid, treelatitude, treelongitude FROM tree WHERE treecondition = ?")){
$stmt->bind_param("s", $treecondition);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($treeid, $treelatitude, $treelongitude);
if ($stmt->num_rows > 0) {
while($tree = $stmt->fetch()) {
$response[treeid] = $treeid;
$response[treelatitude] = $treelatitude;
$response[treelongitude] = $treelongitude;
echo json_encode($response);
}
}
}else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Tree list view credentials are wrong. Please try again!";
echo json_encode($response);
}
$mysqli->close();
}