Codeigniter搜索结果与分页

时间:2016-12-14 07:03:26

标签: php codeigniter-3

您好我需要帮助我尝试对搜索结果进行分页。 我不知道自己做错了什么。因为我的结果不会显示出来。

这是我的模特

function search_posts($query, $pagina, $por_pagina)
{

        $this->db->select();
        $this->db->from('posts');
        $this->db->like("post_title", $query, 'both');
        $this->db->or_like("post", $query, 'both');
        $this->db->order_by('post_id', 'desc');
        $this->db->limit($pagina, $por_pagina);
        $query = $this->db->get();
        return $query->result_array();

}

public function contar_filas()
{
    $this->db->select();
    $this->db->from('posts');
    $query = $this->db->count_all_results();
    return $query;
}

这是我的控制器

public function buscar()
{

    $this->load->model('M_Articulos');
    $data['posts'] = '';
    $data['sin_resultados'] = '';
    $query = trim($this->input->get('query', TRUE));
    if (sizeof($query) == 0)
    {
        show_404();
    }
    if ($query)
    {
        if ($this->uri->segment(3))
        {
            $pagina = $this->uri_segment(3);
        }
        else
        {
            $pagina = 0;
        }
        $por_pagina = 4;
        $data['posts'] = $this->M_Articulos->search_posts($query, $pagina, $por_pagina);
        if ($data['posts'] != FALSE)
        {
            $data['posts'] = $this->M_Articulos->search_posts($query, $pagina, $por_pagina);
            $config['base_url'] = base_url() . 'admin/buscar?query=' . $query;
            $config['per_page'] = $por_pagina;
            $config['total_rows'] = $this->M_Articulos->contar_filas();
            $config['uri_segment'] = 3;
            $config['enable_query_strings'] = TRUE;
            $config['query_string_segment'] = 'page';
            $config['page_query_string'] = TRUE;

            $this->pagination->initialize($config);
        }
        else
        {
            $data['sin_resultados'] = 'nada';
        }
    }
    else
    {
        $data['sin_resultados'] = 'nada';
    }


    $this->load->view('admin/layouts/header');
    $this->load->view('admin/modules/buscar', $data);
    $this->load->view('admin/layouts/footer');
}

有谁知道如何让它发挥作用? 我会更具体一点。我想要的是,当我在搜索页面搜索某些内容时,会有一个分页。感谢

0 个答案:

没有答案