rent_property (table name)
id fullName propertyName
1 A House Name1
2 B House Name2
3 C House Name3
4 D House Name4
rent_amenity (table name)
rentamenityId rentPropertyId amenityName
1 1 Lift
2 1 Gym
3 2 Power backup
4 4 Gym
my sql query
$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);
my dynamic function
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;
}
根据我的表和 mysql 查询,我应该得到两条记录,而且我也得到了正确的结果,但我无法达到预期的结果,请参阅下面我将发布当前我得到的结果我的预期结果是什么
Current Out Put
{
"status": "success",
"message": "Data Found.",
"data": {
"rent_id": [
{
"id": "4",
"fullName": "D",
"propertyName": "House Name4"
},
{
"id": "1",
"fullName": "A",
"propertyName": "House Name1"
}
]
}
}
My Expected Results
{
"status": "success",
"message": "Data Found.",
"data": {
"rent_id": [
{
"id": "4",
"fullName": "D",
"propertyName": "House Name4",
"amenities":[
{
"rentamenityId":"4",
"rentPropertyId":"4",
"amenityName":"Gym"
}
]
},
{
"id": "1",
"fullName": "A",
"propertyName": "House Name1",
"amenities":[
{
"rentamenityId":"1",
"rentPropertyId":"1",
"amenityName":"Lift"
},
{
"rentamenityId":"2",
"rentPropertyId":"1",
"amenityName":"Gym"
}
]
}
]
}
}
在这里,我想根据我的属性(property)从第二个表(rent_amenity)添加设施数组(设施名称),我不知道如何推送设施记录在我的第一个数组中。
1 Câu trả lời
您可能想要更接近以下内容的内容。
LỰA CHỌN *
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' ) -- Removed to return all properties */
AND a.approveStatus!='Inactive'
GROUP BY a.id order by a.id desc
但是,这将为您的属性(property)中的每个设施返回一行,因此您必须在循环遍历各行时处理每个属性(property)并检索其设施的句柄。
另一种选择是使用单个查询来检索属性,然后使用第二个查询来检索返回的属性的便利设施。
关于php - 如何将秒表结果(数组)推送到第一个表结果(数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46491150/
Tôi đang chạy PHP trong một tệp JavaScript, như... var = '';). Tôi cần sử dụng JavaScript để quét chuỗi để tìm các ký tự phân cách PHP (mở và đóng PHP). Tôi đã biết cách rồi
Tôi muốn có thể làm điều gì đó như thế này: php --determine-oldest-supported-php-version test.php và nhận được kết quả đầu ra này: 7.2 Nghĩa là nhị phân php được kiểm tra theo thử nghiệm.
Tôi đang phát triển một trang web php lớn hiện không sử dụng bất kỳ framework nào. Câu hỏi lớn của tôi là, liệu có nên thử tích hợp dần dần khung vào ứng dụng theo thời gian, chẳng hạn như tạo các tiện ích mới và cập nhật các tiện ích cũ không? Ví dụ, tất cả các trang đều được phục vụ trực tiếp thông qua URL.
Dưới đây là mã nguồn của tôi, tôi muốn sử dụng biến $r1 nằm trong tập lệnh php ở cuối trong một tập lệnh php khác ở đầu cùng một trang. Tôi cần một giải pháp đơn giản cho vấn đề này. Tôi muốn sử dụng biến đó trong truy vấn cập nhật có trong mã. $tên)
Tôi đang tạo một trang web thực hiện nhiều chuyển hướng PHP tùy thuộc vào các tình huống khác nhau. Giống như thế này... header("Location: somesite.com/redirectedpage.php"); để bảo mật