In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand the Codeigniter framework paging function", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to understand the Codeigniter framework paging function"!
Generally speaking, when data is paged, you need to obtain the data and the total number of entries of the current page. Most people encapsulate two functions in model to obtain the data and the total number of data of the current page, respectively. The business logic is similar and feels a bit redundant, so it can be encapsulated together:
The copy code is as follows:
/ * *
* obtain paging data and total number of entries
* @ param string @ tablename Table name
* @ param mixed $where condition
* @ param int $limit 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 CI framework paging class
There are four ways to url addresses for CI paging
A) locahost/news/page/2 this 2 represents the second page
B) localhost/news/page/20 this 20 indicates that paging begins with the 20th record, that is, the first record of the page, is the 20th record in the database.
C) Page 2 of localhost/news?per_page=2
D) localhost/news?per_page=20 with b)
First, let's take a look at the parameters of CI paging:
The copy code is as follows:
$config ['base_url'] = $url
/ * basic URL of paging
If you want to use the linked form of an and b, the url should be in the form of / news/page/
If the link is in the form of c, d, then url should be like / news?
, /
$config ['total_rows'] = the total number of total;// records. There is nothing more to say about this, but you get the total number of records from the database.
$config ['per_page'] = $pagesize; / / number of entries per page. Well, there's nothing to say about that. Set it yourself. The default is 10, like.
$config ['page_query_string'] = TRUE
/ * the form of passing parameters. Turning on true will automatically add & per_page=3 to your url. (this per_page is the default query character, of course, you can also use $config ['query_string_segment'] to set it yourself)
So the form in c and d is generally localhost/news?&per_page=2, but it's all the same, it doesn't matter. Get's per_page is still 3.
, /
$config ['first_link'] =' home page'; / / the first page is displayed
$config ['last_link'] =' last page'; / / the last page is displayed
$config ['next_link'] =' next page >'; / / display on the next page
$config ['prev_link'] ='
< 上一页'; // 上一页显示 $config['cur_tag_open'] = ' '; // 当前页开始样式 $config['cur_tag_close'] = ''; /*当前页结束样式。这些你可以自己尝试一下。 比如说我想让当前页的分页数字样式好看一点,红色字体等。你就可以在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就会表示记录数 */ 刚开始在网上查资料的时候,有很多这种写法。 复制代码 代码如下: $this->Model- > get_news ($config ['per_page'], $this- > uri- > segment (3))
In fact, this kind of writing is aimed at the connection form of b). Here $this- > uri- > segment (3) is the number of records fetched to page/20. $config ['per_page'] is to limit how many entries are output.
It is very limited and misleading. I didn't even know why I wrote that at first. Later, I found out that the manual is the best teacher.
When we have configured some parameters of the CI paging class, $this- > pagination- > initialize ($config); / / configure paging
The copy code is as follows:
$page = $this- > pagination- > create_links (); / / We get the paging
Pass it directly to the view page.
As for how to load the model, how to access the data record, how to pass variables to the view, let's not talk about it here, just look at the manual.
Forgot to mention, pagination with query parameters, that's what I did. The search method that submits the query parameter get to the controller in the view. In search, use $get = $this- > input- > get (); to get the query parameters.
Then load model, read the record with query parameters and paging parameters, and display the results to the view.
It is also found that a small bug, such as / news/page/-1000, will show a negative value in the following paging link
The system/libraries/Pagination.php code is found as follows
The copy code is as follows:
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 cur_page = $base_page
}
At this point, I believe you have a deeper understanding of "how to understand the Codeigniter framework paging function". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.