Php 如何在CodeiCenter中为搜索查询联接表

Php 如何在CodeiCenter中为搜索查询联接表,php,mysql,codeigniter,activerecord,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

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

    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>

您的搜索返回了以下结果:
图像名称;?>"
class=“img响应式”>

地点:|价格:R



您可以使用正在使用的活动记录类的
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.

在您的示例中:

$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.

您可以使用正在使用的活动记录类的
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.

在您的示例中:

$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.

您可以在查询中使用join:

     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();
}

希望它能帮助您……

您可以在查询中使用join:

     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();
}

希望它能帮助您……

谢谢quys感谢输入我让它在加入表格后提取图像,但现在它已返回所有图像我只需要每个属性一个图像我该如何实现这一点?这里是网站,只需单击带有默认值的搜索,您将看到我的意思谢谢quys感谢输入我得到它o在加入表格后拉出图像,但现在它已退出所有图像。我只需要每个属性一个图像。我如何实现这一点?这里是网站,只需单击带有默认值的搜索,您就会看到我的意思。我让它在加入表格后拉出图像,但现在它已退出所有图像,我只需要每个属性一个图像属性我将如何实现这一点?这里是网站,只需单击搜索,使用默认值,你就会看到我的意思。好的,你想每个属性只显示一张图像吗?对吗?现在它显示所有图像,因此记录显示3次或4次,取决于图像的数量。嗯,有趣的是,现在它只返回一条记录,而不是全部结果,所以我假设我需要在某处传递一个$id变量?ohhkei,所以删除这一行,我的意思是你添加了$this->db->limit(1);并在加入行中添加left:$this->db->join('property\u images','property.property\u image\u id=property\u images.property\u image\u id','left'));让我们来吧。我让它在加入表后提取图像,但现在它已输出所有图像。我只需要每个属性一个图像。我该如何实现这一点?这里是网站,只需单击带有默认值的搜索,您就会看到我的意思。好的,您希望每个属性只显示一个图像吗?对吗?现在它将显示所有图像因此,记录显示3次或4次取决于图像的数量。嗯,有趣的是,现在它只返回一条记录,而不是所有的结果,所以我假设我需要在某个地方传递$id变量?ohhkei,所以删除该行,我是说添加了$this->db->limit(1)的行;然后在加入行中添加left:$this->db->join('property_images'、'property.property_images_id=property_images.property_images_id'、'left'),让我们来看看。