【问题标题】:How to search in multiple tables using codeigniter?如何使用codeigniter在多个表中搜索?
【发布时间】:2013-03-23 15:50:21
【问题描述】:

所以我有一个搜索框可以在我的工厂表中搜索。它就像一个魅力,但我对 codeigniter 还是很陌生。

我是否需要使用 select 来选择我的数据库中的多个表?

我的控制器功能:

    function search()
    {
        $this->load->view('views/header');
        $this->load->view('views/search');
        $this->load->view('views/footer');
    }

    function searchresults()
    {
        $match = $this->input->post('search');
        $data['query'] = $this->bedrijven_model->get_search($match);
        $this->load->view('views/header');
        $this->load->view('views/searchresults', $data);
        $this->load->view('views/footer');
    }

我的模特:

function get_search($match)
{
    $this->db->like('Bedrijfsnaam', $match);
    $this->db->or_like('Postcode', $match);
    $this->db->or_like('Plaats', $match);
    $this->db->or_like('Telefoonnummer', $match);
    $this->db->or_like('Email', $match);
    $this->db->or_like('Website', $match);
    $this->db->or_like('Profiel', $match);
    $this->db->or_like('Adres', $match);
    $query = $this->db->get('bedrijven');

    return $query->result();
}

我也想知道您是否可以将模型中的搜索功能放入控制器中。因为那时我需要为每个表创建一个新的模型函数。

提前致谢。

【问题讨论】:

    标签: mysql codeigniter search multiple-tables


    【解决方案1】:

    尝试加入两个表,然后像这样申请

    $this->db->like('Bedrijfsnaam', $match);
    $this->db->or_like('Postcode', $match);
    $this->db->or_like('Plaats', $match);
    $this->db->or_like('Telefoonnummer', $match);
    $this->db->or_like('Email', $match);
    $this->db->or_like('Website', $match);
    $this->db->or_like('Profiel', $match);
    $this->db->or_like('Adres', $match);
    

    在 col1,col2 中的 table2 中搜索匹配

    $this->db->or_like('table2.col1',$match);
    $this->db->or_like('table2.col2',$match);
    

    最后将上面应用了like的表与table1连接起来

    $this->db->join('table2','table1.id = table2.id');
    $query = $this->db->get('bedrijven');
    

    【讨论】:

    • 但它并不完全有效。当我申请加入时,搜索结果为空
    • 然后在DB中查看...可能没有match和join结合的记录
    • 嗯,这有点令人困惑。它只显示在我的第二个表和我的第一个表中具有 id 的记录。所以当我有一个 id 为 1 的表记录时,它不会在第二个表中没有 id 1 时显示。
    • 所以......如果你想要他们单独然后没办法......你需要单独经历......
    • 我有一个表格标签。我在这里为我的工厂存储了标签。但是我添加了多少标签工厂在搜索结果中显示的频率
    【解决方案2】:

    用户指南中的示例应说明这一点:

    $this->db->select('*'); // <-- There is never any reason to write this line!
    $this->db->from('blogs');
    $this->db->join('comments', 'comments.id = blogs.id');
    
    $query = $this->db->get();
    
    // Produces:
    // SELECT * FROM blogs
    // JOIN comments ON comments.id = blogs.id
    

    查看用户指南中Active Record 页面下的全部内容。

    【讨论】:

    • 我知道连接是如何工作的,但我不知道如何通过搜索来实现它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多