B. Dee B. Dee - 3 months ago 8
JSON Question

How to filter json on url?

How can I filter the json data from database on a certain field?

Here is my json

[
{
"Car_No":"25",
"Car_Model":"car1",
"Car_Type":"car2",
"Capacity":"12",
"Image":"carkila.esy.es\/upload\/20160811031546.png",
"fuelType":"Diesel",
"carPlatenuNumber":"qwe - 123",
"carStatus":null,
"owner":"owner"
},
{
"Car_No":"24",
"Car_Model":"car",
"Car_Type":"car2",
"Capacity":"123",
"Image":"carkila.esy.es\/upload\/20160808114541.png",
"fuelType":"Biofuels (biodiesel and bioethanol)",
"carPlatenuNumber":"qwe - 123",
"carStatus":null,
"owner":"owner"
},
{
"Car_No":"23",
"Car_Model":"fortuner",
"Car_Type":"suv",
"Capacity":"56",
"Image":"carkila.esy.es\/upload\/20160805104115.png",
"fuelType":"Super unleaded petrol",
"carPlatenuNumber":"xxx888",
"carStatus":null,
"owner":"owner"
},
{
"Car_No":"22",
"Car_Model":"seannnn",
"Car_Type":"seanyboy",
"Capacity":"12",
"Image":"carkila.esy.es\/upload\/20160805091944.png",
"fuelType":"Biofuels (biodiesel and bioethanol)",
"carPlatenuNumber":"hjk123",
"carStatus":null,
"owner":"sean"
},
{
"Car_No":"21",
"Car_Model":"cars",
"Car_Type":"car1",
"Capacity":"12",
"Image":"carkila.esy.es\/upload\/20160805091429.png",
"fuelType":"Premium unleaded",
"carPlatenuNumber":"qwe321",
"carStatus":null,
"owner":"owner"
},
{
"Car_No":"20",
"Car_Model":"car",
"Car_Type":"car1",
"Capacity":"123",
"Image":"https:\/\/www.enterprise.ca\/content\/dam\/global-vehicle-images\/cars\/CHRY_200_2015.png",
"fuelType":"Biofuels (biodiesel and bioethanol)",
"carPlatenuNumber":"qwe123",
"carStatus":null,
"owner":"owner"
}
]


I want it to be like
http://carkila.esy.es/user.php?owner=sean
and it will filter all the owner that has sean in it.

here is my json conversion code.

<?PHP
include_once("connection.php");

session_start();

$query = "SELECT * FROM tbl_cars ORDER BY Car_No DESC";

$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
echo json_encode($data);

?>


Thanks guys :)

Answer
<?PHP
include_once("connection.php");

session_start();

$where = ''
if (isset($_GET['owner'])){
  $where = " WHERE owner like '%".addslashes($_GET['owner'])."%'";
}
$query = "SELECT * FROM tbl_cars ".$where." ORDER BY Car_No DESC"; 

$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}
echo json_encode($data);

?>