【问题标题】:search result from sql error with codeigniter's pagination index使用codeigniter的分页索引从sql错误中搜索结果
【发布时间】:2013-05-04 06:21:11
【问题描述】:

我有一张带有pagination 的桌子。首先,我从我的数据库中选择所有数据,pagination 完美运行。

然后,我开始过滤数据(搜索某些数据,例如:在 SQL 中使用WHERE)。分页的第一个索引运行良好(仅显示某些数据)但是当我单击 下一个索引(第 2、3 个)时,数据将恢复为默认值(过滤器WHERELIKE)工作)。

这是我用于分页的model我想我在这里犯了一个错误):

public function get_umat() {
        $this->db->select('*')->from('msumat')->limit(10, $this->uri->segment(3));
        $this->db->join('mskelas', 'msumat.kelas_id = mskelas.kelas_id');

        $search = $this->input->post('ddl_search');
        $kelas = $this->input->post('ddl_kelas');
        $kelas1 = $this->input->post('ddl_kelas1');
        $kelas2 = $this->input->post('ddl_kelas2');
        $nama = $this->input->post('txt_nama');
        $alamat = $this->input->post('txt_alamat');
        $bulan = $this->input->post('ddl_bulan');
        $bulan1 = $this->input->post('ddl_bulan1');
        $bulan2 = $this->input->post('ddl_bulan2');

        if($this->input->post('btn_search')) 
        {
            if($search === 'nama')
                $this->db->like('nama', $nama);

            else if($search === 'kelas')
                $this->db->where('mskelas.kelas_id', $kelas);

            else if($search === 'range_kelas')
                $this->db->where("mskelas.kelas_id BETWEEN $kelas1 AND $kelas2");

            else if($search === 'alamat')
                $this->db->like('alamat', $alamat);

            else if($search === 'bulan_lahir')
                $this->db->where("MONTH(tanggal_lahir) = $bulan");

            else if($search === 'range_tanggal_lahir')
                $this->db->where("MONTH(tanggal_lahir) BETWEEN $bulan1 AND $bulan2");
        }

        return $this->db->get()->result();
    }

public function count_umat(){
        return $this->db->count_all('msumat');
    }

那么这是我在controller中的pagination(我想我需要修改$config['total_rows']):

$config['base_url'] = site_url('/backend_umat/index');
        $config['total_rows'] = $this->umat_m->count_umat();
        $config['per_page'] = 10; 
        $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';

        $this->pagination->initialize($config); 

        $data['pagination'] = $this->pagination->create_links();

我认为问题是:

  1. 我需要COUNT$config['total_rows'] 的搜索结果(每次搜索)
  2. 不知何故,我需要修改 model (get_umat()) 才能正常工作

感谢任何帮助。谢谢:D

【问题讨论】:

    标签: codeigniter pagination


    【解决方案1】:

    在 CI 中,如果您想使用多个过滤器应用搜索,它适用于第一页,并且 ci 总是查找 url 段,对于第二页或第三页,您需要发送您为第一页发送的所有搜索参数通过表单,意味着您的 url 应该包含 url 段然后您可以过滤您的搜索记录,因为您没有提交第二页和第三页的表单。我建议你看看这个教程,它有很好的方法来使用带有多个过滤器的搜索记录,并且可以完美地使用分页

    CI Search on Tutorial Nettuts

    【讨论】:

    • 感谢您的帮助 :D 我现在正在看。如果它可以解决我的问题,我会接受你的回答。太糟糕了,我只能投票一次:D
    • 没问题,这个解决方案最适合从数据库中搜索和过滤记录我在这个项目上也使用过同样的方法uaeboattrading.com
    • 我认为这个视频的结论是我们需要使用“querystring”之类的东西来使搜索工作。这是真的吗?我还在路上:D
    • 是的查询字符串将被保存在数据库中,它将生成保存的查询字符串的 ID,您可以将其用于分页,并且搜索词的所有可能组合都将保存在数据库中
    • 好的,感谢您的关注。在我完成这个 tuts 后我会告诉你
    猜你喜欢
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多