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 :)
<?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);
?>
If you're trying to filter the dataset based on a request to your JSON conversion script with the query string containing owner
, I suggest using the database to filter out the result set, rather than filtering out the data after it's been iterated over already.
user.php?owner=sean
<?php
require_once __DIR__ . '/connection.php';
session_start();
$owner = isset($_GET['owner']) ? $_GET['owner'] : null;
$query = 'SELECT * FROM tbl_cars';
if (isset($owner)) {
$query .= ' WHERE owner = ?';
}
$query .= ' ORDER BY Car_No DESC';
$stmt = mysqli_prepare($conn, $query);
if (isset($owner)) {
mysqli_stmt_bind_param($stmt, 's', $owner);
}
mysqli_stmt_execute($stmt);
$data = array();
if ($result = mysqli_stmt_get_result($stmt)) {
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
}
echo json_encode($data);
If you're looking for owners that contains the word sean
replace the WHERE
statement with.
$query .= ' WHERE owner LIKE ?';
and replace mysqli_stmt_bind_param
with
mysqli_stmt_bind_param($stmt, 's', '%' . $owner . '%');
Result:
[
{
"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"
}
]