【问题标题】:How and where to add the amenity values in this json response在此 json 响应中如何以及在何处添加便利值
【发布时间】:2018-03-11 09:10:40
【问题描述】:

这里有两张桌子(rent_propertyrent_amenity)。我正在使用连接查询,其中rent_property.id。我正在使用 rent_amenity rentPropertyId 的外键。这里我是房地产项目,一个项目有多个便利设施,所以我使用两张桌子。

rent_property:

id     fullName    propertyName
-------------------------------
1       A          House Name1
2       B          House Name2
3       C          House Name3
4       D          House Name4

rent_amenity:

rentamenityId         rentPropertyId       amenityName
-------------------------------------------------------    
1                         1                 Lift
2                         1                 Gym
3                         2                 Power backup
4                         4                 Gym

我的 SQL 查询

$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;
}

根据我的 SQL 查询,我应该得到两条记录,而且我得到了正确的结果,但我无法达到预期的结果,请参阅下面我将发布我目前得到的结果:

{
"status": "success",
"message": "Data Found.",
"data": {
    "rent_id": [
        {
            "id": "4",
            "fullName": "D",
            "propertyName": "House Name4"
        },
        {
            "id": "1",
            "fullName": "A",
            "propertyName": "House Name1"
        }
    ]
}
}

我的主要问题是我也想给响应提供便利,我不知道我必须在这个数组中的哪里添加便利。 id 1 有两个便利设施,id 2 有一个便利设施

【问题讨论】:

    标签: php json php-7


    【解决方案1】:

    您可以创建一个数据透视表以将 propertyId 与 amenityId 相关联,并构建一个连接查询来获取与特定属性相关的 amentite。

    【讨论】:

    • 你能更新你的答案吗,我真的做不到
    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多