The CodeIngniter framework has its own paging class, but it finds a problem during use, that is, when the current page number is negative, it will cause paging display exceptions. The following code controls that the current page number does not exceed the range. the CodeIngniter framework has its own paging class. However, when the current page number is negative, the page display is abnormal.
The following code controls that the current page number does not exceed the range:
if($cpage >= $total_pages){$cpage = $total_pages;}elseif($cpage <= 1){$cpage = 1;}
Use the code above: when the current page number exceeds the maximum page number, you can set $ cpage = $ total_pages. the page style is displayed normally. However, when the current page number is less than 0, although $ cpage = 1, however, an exception occurs in the paging style.
To make up for this exception, we can create a paging class of our own. The Creation procedure is as follows:
1. create the class Myclass. php in the project application directory libraries
Tip:
1) the first letter of the file name is capitalized. for example: Myclass. php
2) the first letter of the class declaration is capitalized. for example: class Myclass
3) the class name and file name should comply
2. class Myclass. php
'; // The first page if ($ cpage> 1) {$ page. ="Homepage";}// Output the first 4 Pages $ start_page = $ cpage-5; $ start_page = ($ start_page <= 1 )? 1: $ start_page; for ($ I = $ start_page; $ I <$ cpage; $ I ++) {$ page. ="
{$ I}";}// 5 pages after output $ end_page = $ cpage + 5; $ end_page = ($ end_page >=$ total_page )? $ Total_page: $ end_page; for ($ I = $ cpage; $ I <= $ end_page; $ I ++) {if ($ I = $ cpage) {$ page. ="
{$ I}";} Else {$ page. ="
{$ I}";}}// The last page if ($ cpage <$ total_page) {$ page. ="
Last page";} $ Page. =''; Return $ page ;}}
The above is your own paging class.
3. use your own class
Among all Controller functions, you can initialize your class in the following standard mode:
$this->load->library('myclass');
When myclass is a file name, you do not need to add the ". php" extension. The name is case-insensitive.
Once the custom class is loaded, you can call the class in the following ways. Note that the lower-case name is used:
$ This-> myclass-> my_function (); // The instance name of the object is always in lower case.
Parameters are passed when custom classes are initialized.
When initializing the class library, you can use the second parameter to dynamically pass an array to the class constructor:
$params = array('type' => 'large', 'color' => 'red'); $this->load->library('Someclass', $params);
When using this feature, you must add parameters to the class constructor:
Of course, you can also pass parameters according to the code above.