如何在url上过滤json?

时间:2016-08-15 01:30:17

标签: php json

如何在某个字段上过滤数据库中的json数据?

这是我的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"
    }
]

我希望它像http://carkila.esy.es/user.php?owner=sean一样,它会过滤所有拥有sean的所有者。

这是我的json转换代码。

<?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);

?>

谢谢你们:)

1 个答案:

答案 0 :(得分:2)

<?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);

?>