rent_property(表名)
id fullName propertyName
1 Kani House Name1
2 B House Name2
3 C House Name3
4 Sarvan House Name4
rent_amenity(表名)
rentamenityId rentPropertyId amenityName
1 1 Lift
2 1 Gym
3 1 Swimming Pool
4 4 Lift
5 4 Gym
mysql查询
$sql = "SELECT a.id,a.fullName,a.propertyName FROM rent_property a LEFT JOIN rent_amenity b ON a.id = b.rentPropertyId WHERE a.city='1' AND a.propertyType IN ( '1','2' ) AND b.amenityName IN ( 'Gym' ) AND a.approveStatus!='Inactive' GROUP BY a.id order by a.id desc ";
$result = $this->GetJoinRecord($sql);
我的动态功能
public function GetJoinRecord($query_string){
$con = $this->DBConnection();
$query = mysqli_query($con,$query_string);
if(@mysqli_num_rows($query)>0){
while($data=mysqli_fetch_assoc($query)){
$record[] = $data;
}
mysqli_free_result($query);
}
mysqli_close($con);
return $record;
}
的print_r($结果);
Array
(
[0] => Array
(
[id] => 4
[fullName] => Sarvan
[propertyName] => House Name4
)
[1] => Array
(
[id] => 1
[fullName] => Kani
[propertyName] => House Name1
)
)
到目前为止,每件事情都运转良好,此后我想采用 amenityName 为基础的属性,我想推送$ result数组中的设施
$result = $this->GetJoinRecord($sql);
foreach ($result as $key => $value) {
$propertyId = $value['id'];
$farrs = array();
$q1 = "SELECT * FROM rent_amenity WHERE `rentPropertyId` = '$propertyId' AND amenityStatus != 'Deactive'";
$amenities = $this->GetJoinRecord($q1);
foreach($amenities as $key => $value){
$rowame['rentamenityId'] = $value['rentamenityId'];
$rowame['rentPropertyId'] =$value['rentPropertyId'];
$rowame['amenityName'] = $value['amenityName'];
$rowame['amenityStatus'] = $value['amenityStatus'];
array_push($farrs,$rowame);
}
$row['amenities'] = $farrs;
array_push($result,$row);
}
$response_array['status']='success';
$response_array['message']='Data Found.';
$response_array['data']=array('rent_id'=>$result);
$this->response($this->json($response_array), 200);
我正在获得这样的输出
{
"status": "success",
"message": "Data Found.",
"data": {
"rent_id": [
{
"id": "4",
"fullName": "Sarvan",
"propertyName": "House Name4"
},
{
"id": "1",
"fullName": "Kani",
"propertyName": "House Name1"
},
{
"amenities": [
{
"rentamenityId": "9",
"rentPropertyId": "4",
"amenityName": "Lift",
"amenityStatus": "Active"
},
{
"rentamenityId": "10",
"rentPropertyId": "4",
"amenityName": "Gym",
"amenityStatus": "Active"
}
]
},
{
"amenities": [
{
"rentamenityId": "1",
"rentPropertyId": "1",
"amenityName": "Lift",
"amenityStatus": "Active"
},
{
"rentamenityId": "2",
"rentPropertyId": "1",
"amenityName": "Gym",
"amenityStatus": "Active"
},
{
"rentamenityId": "3",
"rentPropertyId": "1",
"amenityName": "Swimming Pool",
"amenityStatus": "Active"
}
]
}
]
}
}
但这不是我预期的输出,这里是我的表和sql查询记录正确,我无法制作我预期的JSON格式
我期望的JSON输出就像是
{
"status": "success",
"message": "Data Found.",
"data": {
"rent_id": [
{
"id": "4",
"fullName": "Sarvan",
"propertyName": "House Name4",
"amenities":[
{
"rentamenityId":"4",
"rentPropertyId":"4",
"amenityName":"Lift"
},
{
"rentamenityId":"5",
"rentPropertyId":"4",
"amenityName":"Gym"
}
]
},
{
"id": "1",
"fullName": "A",
"propertyName": "House Name1",
"amenities":[
{
"rentamenityId":"1",
"rentPropertyId":"1",
"amenityName":"Lift"
},
{
"rentamenityId":"2",
"rentPropertyId":"1",
"amenityName":"Gym"
},
{
"rentamenityId":"3",
"rentPropertyId":"1",
"amenityName":"Swimming Pool"
}
]
}
]
}
}
我的代码
$sql = "SELECT a.id,a.fullName,a.propertyName FROM rent_property a LEFT JOIN rent_amenity b ON a.id = b.rentPropertyId WHERE a.city='1' AND a.propertyType IN ( '1','2' ) AND b.amenityName IN ( 'Gym' ) AND a.approveStatus!='Inactive' GROUP BY a.id order by a.id desc ";
$result = $this->GetJoinRecord($sql);
foreach ($result as $key => $value) {
$propertyId = $value['id'];
$farrs = array();
$q1 = "SELECT * FROM rent_amenity WHERE `rentPropertyId` = '$propertyId' AND amenityStatus != 'Deactive'";
$amenities = $this->GetJoinRecord($q1);
foreach($amenities as $key => $value){
$rowame['rentamenityId'] = $value['rentamenityId'];
$rowame['rentPropertyId'] =$value['rentPropertyId'];
$rowame['amenityName'] = $value['amenityName'];
$rowame['amenityStatus'] = $value['amenityStatus'];
array_push($farrs,$rowame);
}
$row['amenities'] = $farrs;
array_push($result,$row);
}
$response_array['status']='success';
$response_array['message']='Data Found.';
$response_array['data']=array('rent_id'=>$result);
$this->response($this->json($response_array), 200);
答案 0 :(得分:0)
您需要更改以下代码:首先创建新结果,其中包含来自两个查询的合并数据,然后最后将该结果添加到数据
$new_result = array();
$result = $this->GetJoinRecord($sql);
foreach ($result as $key => $value) {
$propertyId = $value['id'];
$farrs = array();
$q1 = "SELECT * FROM rent_amenity WHERE `rentPropertyId` = '$propertyId' AND amenityStatus != 'Deactive'";
$amenities = $this->GetJoinRecord($q1);
foreach($amenities as $key => $value){
$rowame['rentamenityId'] = $value['rentamenityId'];
$rowame['rentPropertyId'] =$value['rentPropertyId'];
$rowame['amenityName'] = $value['amenityName'];
$rowame['amenityStatus'] = $value['amenityStatus'];
array_push($farrs,$rowame);
}
$row['id'] = $value['id'];
$row['fullName'] = $value['fullName'];
$row['propertyName'] = $value['propertyName'];
$row['amenities'] = $farrs;
array_push($new_result,$row);
}
$response_array['status']='success';
$response_array['message']='Data Found.';
$response_array['data']=array('rent_id'=>$new_result);
$this->response($this->json($response_array), 200);