PHP CI Framework Learning Notes-pagination implementation program

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.