在另一个表中索引数据并将其传递以匹配另一个查询时出错

时间:2013-07-16 02:35:54

标签: php codeigniter

我有这些表:(在问题中截断了不需要的列)

间: room_id | room_type_id | room_number

room_types: room_type_id | room_type | room_type_default_price

保留: reservation_id room_type |签到|结帐

  1. 当用户通过输入所需的房间类型,到达日期,出发日期来搜索可用房间时,必须在下拉列表中填写可用的房间类型。然而,有两个问题,一个是已知的,一个是未知的。 下面的代码只返回页面(视图)加载时“数组到字符串转换错误”。

  2. 已知的问题是,房间类型的下拉列表也填充了来自room_types表的数据。对于JS / jQuery计算,我在填充时连接了room_type列和room_type_default_price列。所以生成的HTML是这样的:

  3. 双人间

    因此,用户输入$ room_type,例如“Double Room_20000”,其中最初没有列数据存在。在房间类型表中,room_type列虽然有“双人房”。如何将这两个值分开并将room_type_id设置为$ room_type_id?

    型号:

    function searchRoomType($room_type) {
        $query = $this->db->query(" SELECT room_type_id FROM room_types WHERE room_type = '$room_type' LIMIT 1");
        return $query->result();
    
        //return $room_type_id;
    
        //print_r ($room_type_id);
    }
    
    function searchRooms($room_type, $start_date, $end_date, $tmpRoomNumber = array()) {
        $room_type_id = $this->searchRoomType($room_type);
    
        $query = $this->db->query(
            "SELECT a.room_number
            FROM rooms a LEFT OUTER JOIN (  SELECT room_number
                                            FROM reservations
                                            WHERE checkin >= '$start_date'
                                            AND checkout <= '$end_date'
                                            ) b
            ON a.room_number = b.room_number
            WHERE a.room_type_id = '$room_type_id'
            AND a.housekeeping_status = 'Clean'
            AND b.room_number is NULL
            GROUP BY a.room_type_id
            ORDER BY a.room_number ASC");
    
        if($query->num_rows()>0) {
            foreach($query->result_array() as $row) {
                $tmpRoomNumber[$row['room_number']] = $row['room_number'];
            }
        }
        return $tmpRoomNumber;
    
    }
    

1 个答案:

答案 0 :(得分:0)

我认为您应该像这样更改函数searchRoomType

function searchRoomType($room_type) {
    $query = $this->db->query(" SELECT room_type_id FROM room_types WHERE room_type = '$room_type' LIMIT 1");
    foreach ($query->result() as $row)
    {
      $room_type_i=$row->room_type_id;
    }
  return $room_type_id;
}