Codeigniter of CI Frame Paging Function and Related Knowledge

  • 2021-07-24 10:27:16
  • OfStack

1. When data is paged, it is necessary to obtain the data and total number of the current page. 1. People encapsulate two functions in model to obtain the data and total number of data of the current page respectively. The business logic is similar and feels a little redundant. It can be encapsulated in 1:


/**
     * Get paging data and total number
     * @param string @tablename Table name
     * @param mixed $where Condition
     * @param int $limit Number of strips per page
     * @param int $offset Current page
     */
    public function get_page_data($tablename, $where, $limit, $offset, $order_by, $db)
    {
        if(empty($tablename))
        {
            return FALSE;
        }
       
        $dbhandle = empty($db) ? $this->db : $db;
       
        if($where)
        {
            if(is_array($where))
            {
                $dbhandle->where($where);
            }
            else
            {
                $dbhandle->where($where, NULL, false);
            }
        }
       
        $db = clone($dbhandle);
        $total = $dbhandle->count_all_results($tablename);
       
        if($limit)
        {
            $db->limit($limit);
        }
       
        if($offset)
        {
            $db->offset($offset);
        }
       
        if($order_by)
        {
            $db->order_by($order_by);
        }
       
        $data = $db->get($tablename)->result_array();
       
        return array('total' => $total, 'data' => $data);
    }

Experience of using paging class in CI framework

There are four ways for CI to page url addresses
a) locahost/news/page/2 This 2 represents page 2
b) localhost/news/page/20 This 20 means that the page starts with the 20th record, the first record on the page, which is the 20th record in the database.
c) localhost/news? per_page=2 Page 2
d) localhost/news? per_page=20 Same as b)

First, let's look at the parameters of CI paging under 1:


$config['base_url'] = $url;  
/* 分页的基础 URL
如果你想用a、b的链接形式,则该url应该形式如/news/page/ 
如果链接是c、d的形式,则url应该如/news? 
*/ 
$config['total_rows'] = $total;//记录总数,这个没什么好说的了,就是你从数据库取得记录总数  
$config['per_page'] = $pagesize; //每页条数。额,这个也没什么好说的。。自己设定。默认为10好像。  
$config['page_query_string'] = TRUE;  
/*传参形式。开启true则会自动在你的url后面加上&per_page=3。(这个per_page是默认的查询字符,当然你也可以用$config['query_string_segment']来自己设定)
因此c、d中的形式1般是为localhost/news?&per_page=2不过都1样,没什么影响。get的per_page还是3 
*/ 
$config['first_link'] = '首页'; // 第1页显示  
$config['last_link'] = '末页'; // 最后1页显示  
$config['next_link'] = '下1页 >'; // 下1页显示  
$config['prev_link'] = '< 上1页'; // 上1页显示  
$config['cur_tag_open'] = ' <a class="current">'; // 当前页开始样式  
$config['cur_tag_close'] = '</a>';  
/*当前页结束样式。这些你可以自己尝试1下。
比如说我想让当前页的分页数字样式好看1点,红色字体等。你就可以在current上加上css代码 
*/ 
$config['num_links'] = 2;// 当前连接前后显示页码个数。意思就是说你当前页是第5页,那么你可以看到3、4、5、6、7页。  
$config['uri_segment'] = 4;  
/*这个是你在用a)、b)链接样式的时候,用来判断页页数。
比如localhost/news/page/3  这个uri_segment就要设定为3。localhost/news/title/page/3这个就要设定为4 
*/ 
$config['use_page_numbers'] = TRUE;  
/*这个就是a)、b)的差别了。开启了,page就会表示页数。false就会表示记录数
*/ 

When I first started to look up information on the Internet, there were many such writing methods.


$this->model->get_news($config['per_page'],$this->uri->segment(3)); 

In fact, this writing is aimed at b). Here's $this- > uri- > segment (3) is to get the number of records 20 in page/20. $config ['per_page'] is to limit the output.
There are great limitations and misleading. I didn't even know why I wrote it in the first place. . Later, it was discovered that manuals are the best teachers.

After we have configured all the parameters of the CI paging class, $this- > pagination- > initialize ($config); //Configure paging


$page = $this->pagination->create_links();  // We get paging  

Pass it directly to the view page.

As for how to load the model, how to access data records, and how to pass variables to the view, I won't say it here, just look at the manual.

I forgot to mention paging with query parameters, which is what I did. View submits the query parameter get to the controller's search method. In search, use $get = $this- > input- > get (); To get the query parameters.
Then load model, read the records with query parameters and paging parameters, and display the results to the view. .

In addition, when you find a small bug, such as/news/page/-1000, the following paging link will appear negative
The following code was found for system/libraries/Pagination. php


if ($this->use_page_numbers AND $this->cur_page == 0)  
{  
    $this->cur_page = $base_page;  
}  
// Should be   
if ($this->use_page_numbers AND $this->cur_page <= 0)  
{  
    $this->cur_page = $base_page;  


That's right. After modification, this problem is gone.


Related articles: