如何在Codeiginter中为搜索查询连接表

时间:2015-11-02 06:25:40

标签: php mysql codeigniter activerecord

您好我有以下搜索用来查找信息的功能:

    function get_search(){
        $property_location = $this->input->post('property_location');
        $property_bedroom = $this->input->post('property_bedroom');
        $property_bathroom = $this->input->post('property_bathroom');
        $property_status = $this->input->post('property_status');
        $property_type = $this->input->post('property_type');

        $this->db->like('property_location',$property_location);
        $this->db->like('property_beds',$property_bedroom);
        $this->db->like('property_bath',$property_bathroom);
        $this->db->like('property_state',$property_status);
        $this->db->like('property_type',$property_type);
        $query = $this->db->get('property');
        error_log($this->db->last_query());
    return $query->result();
}

我如何加入另一个表格,将property_images添加到搜索中,以便数据包含在结果中?

以下是我的观点:

<div class="container">
<div class="page-header">
    <h4>Your search returned the following result(s):</h4>
</div>
<?php foreach ($results as $item): ?>
<div class="row">
        <div class="col-md-3">
            <div class="thumbnail">
                <img
                    src="<?php echo base_url() . 'data/images/property_images/' . $item->image_name; ?>"
                    class="img-responsive">
            </div>
        </div>
        <div class="col-md-9">
            <h4 style="margin-top: 0;">
                <a href="/property/view/<?php echo $item->property_slug;?>"><?php echo $item->property_name; ?></a>
            </h4>
            <p><?php echo $item->property_description;?></p>
            <p>Location: <?php echo $item->property_location ?> | Price: R<?php echo $item->property_price ?></p>
        </div>
</div>
    <hr>
<?php endforeach; ?>
</div>

2 个答案:

答案 0 :(得分:2)

您可以使用正在使用的活动记录类的join()方法来执行此操作。

join($table, $cond[, $type = ''[, $escape = NULL]])
    Parameters: 

        $table (string) – Table name to join
        $cond (string) – The JOIN ON condition
        $type (string) – The JOIN type
        $escape (bool) – Whether to escape values and identifiers

    Returns:    

    CI_DB_query_builder instance (method chaining)
    Return type:    

    CI_DB_query_builder

    Adds a JOIN clause to a query.

Reference

在你的例子中:

$this->db->like('property_location',$property_location);
$this->db->like('property_beds',$property_bedroom);
$this->db->like('property_bath',$property_bathroom);
$this->db->like('property_state',$property_status);
$this->db->like('property_type',$property_type);
$this->db->like('property_images.imageField', $imageFieldValue); // Observe change here.
$query = $this->db->get('property');
$this->db->join('property_images','property.your_id_field = property_images.your_id_field'); // Observe change here.

答案 1 :(得分:2)

您可以在查询中使用加入:

     function get_search(){
        $property_location = $this->input->post('property_location');
        $property_bedroom = $this->input->post('property_bedroom');
        $property_bathroom = $this->input->post('property_bathroom');
        $property_status = $this->input->post('property_status');
        $property_type = $this->input->post('property_type');

        $this->db->like('property_location',$property_location);
        $this->db->like('property_beds',$property_bedroom);
        $this->db->like('property_bath',$property_bathroom);
        $this->db->like('property_state',$property_status);
        $this->db->like('property_type',$property_type);
        $this->db->join('property_images','property.property_image_id=property_images.property_image_id');   //add this line in your code
        $query = $this->db->get('property');
        error_log($this->db->last_query());
    return $query->result();
}

希望它能帮到你......