View HTML
<div id="Body">
<form action="/index.php/search/index/"Method="Get">
<p> Please enter the title, author, one or more of the publishers to inquire. </p>
<p><input type="text"Name="s"Value=""Size=" -"/> <input type="Submit"Value="Search"/></p>
</form>
</div>
Controller
Publicfunction index () {
$keyword = $ This->input->Get('s');
$offset = $ This->input->Get('Offset');
if(Empty ($offset)) {
$offset =0;
}
if(! empty ($keyword)) {
$ This->load->model ('Book_model');
$ This->load->library ('pagination');
$per _page =Ten;
$config ['num_links'] =5;
$config ['Base_url'] ='/index.php/'. $ This->router->class.'/'. $ This->router->method.'/?s='. $keyword;
$config ['Per_page'] = $per _page;
$config ['total_rows'] = $ This->book_model->find_by_name ($keyword, NULL, NULL,true);
$config ['page_query_string'] =false;
$config ['query_string_segment'] ='Offset';//redefine the parameter name at the start of the record, which defaults to Per_page
$ This->pagination->initialize ($config);
$data ['Books'] = $ This->book_model->find_by_name ($keyword, $per _page, $offset);
$ This->load->view ('Search', $data);
}Else{
$ This->load->view ('Search');
}
}
because the default enable_query_strings in config.php is false, the starting position is always at the end, so the result is similar to/index.php/search/index/?s= China/ Ten , the page number can not be taken, this configuration needs to be changed to false;
Data load Model
Publicfunction Find_by_name ($name, $per _page=0, $offset =0, $is _total =false) {
if($is _total) {//Total
$query = $ This->db->query ("select COUNT (ID) as CNT from {$this->_book} where book_name like '%{$name}% '");
If($query->num_rows () >0) {
$row = $query->row ();
$ret = $row->cnt;
}
}Else{//List
$query = $ This->db->query ("select * FROM {$this->_book} where book_name like '%{$name}% ' limit {$offset}, {$per _page}");
$ret = $query->result ();
}
return$ret;
}
PHP CI Framework Learning Notes-pagination implementation program