php - 如何在 Codeigniter 中加入表以进行搜索查询

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

我如何将另一个表 say 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>

最佳答案

您可以在查询中使用 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();
}

希望对你有所帮助....

关于php - 如何在 Codeigniter 中加入表以进行搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33471861/

相关文章:

php - 是否需要包含命名空间文件?

php - Wordpress 组合查询

php - ionauth 登录最大尝试次数实现

PHP没有返回最新更新的sql过程结果

php - 如何从 mysql 数据库检索数据并填充到 jquery mobile 的 ListView 中

php - 如何(重新)组织这些数据 - Lumen/Laravel

mysql - XAMPP 服务器 mysql 未启动 "Attempting to start mysql"

MySQL 使用 Union 按日期列求和前一行

mysql - 如何在SQL中显示未分配的id?

php - 结合 Angularjs 和 CodeIgniter