Pagination with search in Codeigniter is not worki

2019-09-19 10:53发布

问题:

I have a problem in pagination with search in codeigniter

  • URL is not change when select the other page link

  • the data is not change when select the other link

This my controller

public function search_code(){

    $this->load->library('pagination');
    $param = new stdClass();
    $param->item_code = $this->input->get('by_item_code');
    $param->description = $this->input->get('by_description');

    if (!isset($page) || $page == '') {
        $page = 1;
    }
    $param->per_page = 50;
    $param->limit = ($page - 1) * $param->per_page;

    $paginate_url = base_url('warehouse/search_code?item_code='.$param->item_code.'&by_description='.$param->description.'&page='.($page+1));

    $data['total_result'] = $this->m_stock->count_stock_search ($param);

    $config['uri_segment'] = 3;
    $config['num_links'] = 4;
    $config['base_url'] = $paginate_url;
    $config['total_rows'] = $data['total_result'];
    $config['per_page'] = $param->per_page;
    $config['use_page_numbers'] = TRUE;
    $config['page_query_string'] = TRUE;

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

    $data['item'] = $this->m_stock->search_code ($param);
    $this->load->view('v_all_stocks', $data);
}

This my model

function search_code($param){

    $result = array();
    $sql = "select ic.id, ic.item_code, ic.description, maxdt.maxdt, lc.balance,lc.dt,lc.createdate,lc.id_lines_code, io.ONHANDQTY ";
    $sql .= "from tbl_item_code ic ";
    $sql .= "left join ( tbl_lines_code lc inner join ( select id, max(createdate) maxdt from tbl_lines_code where active = 1 group by id ) maxdt ";
    $sql .= "on lc.id = maxdt.id and lc.createdate = maxdt.maxdt ) on ic.id = lc.id ";
    $sql .= "left join item_ostendo io on io.ITEMCODE = ic.item_code ";
    $sql .= "where ic.active = 1 ";

    if ($param->item_code != '') {
        $sql .= "AND ic.item_code LIKE '%$param->item_code%' ";
    }
    if ($param->description != '') {
        $sql .= "AND ic.description LIKE '%$param->description%' ";
    }
    $sql .= "group by ic.id order by ic.item_code ";
    if ($param->limit > 0)
        $sql .= " LIMIT ".$param->limit.", ".$param->per_page;
    else
        $sql .= " LIMIT ".$param->per_page;
    $query = $this->db->query($sql);

    if ($query->num_rows() > 0) {
        $result = $query->result();
    }

    return $result;

}

and about view i checked it's can working but i don't know why i select the link of number the data is show like the last page but the number of link is working

Show all data not search yet the link show like this

when search show like this but when select number 2 of link it's show like this and the data is same every page

回答1:

You need to change your:

$paginate_url = base_url('warehouse/search_code?item_code='.$param->item_code.'&by_description='.$param->description.'&page='.($page+1));

This:

$config['uri_segment'] = 3;

defines that segment for the offset clause.

I would suggest:

$query = "?item_code='.$param->item_code.'&by_description='.$param->description.'&page='.($page+1))";
$paginate_url = base_url('warehouse/search_code/page/').$query;
$config['uri_segment'] = 4;

Take this as offset:

$param->limit = $this->uri->segment(4); 


回答2:

You have to change paging base url as below :

    $paginate_url = base_url('warehouse/search_code').'?item_code='.$param->item_code.'&by_description='.$param->description;