thinkPHP 3.2 Implementation of Paging Custom Style
- 2021-09-04 23:41:53
- OfStack
In this paper, an example is given to illustrate the method of implementing paging custom style in thinkPHP 3.2. Share it for your reference, as follows:
The following is a custom paging of Tp 3.2, which was inspired by reading a netizen's blog. After some modifications, you can also modify the custom style after seeing the code;
The main style control file is page. css, and the paging class at the bottom of the framework can be directly pasted and copied;
1. The page. class. php path at the bottom of the framework (Engine\ Library\ Think)
In fact, this document does not need to be modified too much, and it can also be directly used by the official; The following is what I use now, with slight modifications;
<?php
namespace Think;
class Page{
public $firstRow; // Number of starting lines
public $listRows; // List rows per page
public $parameter; // Parameters to take when paging jump
public $totalRows; // Total number of rows
public $totalPages; // Total number of paged pages
public $rollPage = 11;// Pages per page displayed in the paging bar
public $lastSuffix = true; // Finally 1 Whether the page displays the total number of pages
private $p = 'p'; // Paging parameter name
private $url = ''; // Current link URL
private $nowPage = 1;
// Paging display customization
private $config = array(
'header' => '<span class="rows"> Altogether %TOTAL_ROW% A record </span>',
'prev' => '<<',
'next' => '>>',
'first' => '1...',
'last' => '...%TOTAL_PAGE%',
'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
);
/**
* Schema function
* @param array $totalRows Total number of records
* @param array $listRows Number of records displayed per page
* @param array $parameter Parameters of paging jump
*/
public function __construct($totalRows, $listRows=20, $parameter = array()) {
C('VAR_PAGE') && $this->p = C('VAR_PAGE'); // Set the paging parameter name
/* Basic settings */
$this->totalRows = $totalRows; // Set the total number of records
$this->listRows = $listRows; // Set the number of rows per page
$this->parameter = empty($parameter) ? $_GET : $parameter;
$this->nowPage = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
$this->firstRow = $this->listRows * ($this->nowPage - 1);
}
/**
* Customize paging link settings
* @param string $name Set name
* @param string $value Setting value
*/
public function setConfig($name,$value) {
if(isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
/**
* Generate Link URL
* @param integer $page Page number
* @return string
*/
private function url($page){
return str_replace(urlencode('[PAGE]'), $page, $this->url);
}
/**
* Assemble paging links
* @return string
*/
public function show() {
if(0 == $this->totalRows) return '';
/* Generate URL */
$this->parameter[$this->p] = '[PAGE]';
$this->url = U(ACTION_NAME, $this->parameter);
/* Calculate paging information */
$this->totalPages = ceil($this->totalRows / $this->listRows); // Total pages
if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
$this->nowPage = $this->totalPages;
}
/* Calculate paging zero time variable */
$now_cool_page = $this->rollPage/2;
$now_cool_page_ceil = ceil($now_cool_page);
$this->lastSuffix && $this->config['last'] = $this->totalPages;
// Upper 1 Page
$up_row = $this->nowPage - 1;
$up_page = $up_row > 0 ? '<a class="prev" href="' . $this->url($up_row) . '" rel="external nofollow" >' . $this->config['prev'] . '</a>' : '';
// Under 1 Page
$down_row = $this->nowPage + 1;
$down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="' . $this->url($down_row) . '" rel="external nofollow" >' . $this->config['next'] . '</a>' : '';
// No. 1 1 Page
$the_first = '';
if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
$the_first = '<a class="first" href="' . $this->url(1) . '" rel="external nofollow" >' . $this->config['first'] . '</a>';
}
// Finally 1 Page
$the_end = '';
if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
$the_end = '<a class="end" href="' . $this->url($this->totalPages) . '" rel="external nofollow" >' . $this->config['last'] . '</a>';
}
// Digital connection
$link_page = "";
for($i = 1; $i <= $this->rollPage; $i++){
if(($this->nowPage - $now_cool_page) <= 0 ){
$page = $i;
}elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
$page = $this->totalPages - $this->rollPage + $i;
}else{
$page = $this->nowPage - $now_cool_page_ceil + $i;
}
if($page > 0 && $page != $this->nowPage){
if($page <= $this->totalPages){
$link_page .= '<a class="num" href="' . $this->url($page) . '" rel="external nofollow" >' . $page . '</a>';
}else{
break;
}
}else{
if($page > 0 && $this->totalPages != 1){
$link_page .= '<span class="current">' . $page . '</span>';
}
}
}
// Replace paging content
$page_str = str_replace(
array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
$this->config['theme']);
return "<div>{$page_str}</div>";
}
}
2. Controller, just write demo.
public function index(){
$obj=M("news");
$count = $obj->where('status=1 and classID=74 ')->count();// Query the total number of records that meet the requirements
$limit = 10;
$Page = new \Think\Page($count,$limit);// Instantiate the paging class Total number of incoming records and number of records displayed per page (25)
$show = $Page->show();// Paged display output
$list = $obj->where('status=1 and classID=74 ')->order('writetime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
$firstlist = $obj->where('status=1 and classID=74 and Indexfirst=1')->order('writetime desc')->limit(4)->select();
$this->assign('firstlist',$firstlist);
$this->assign('page',$show);// Assignment paging output
$this->assign('list',$list);
$this->display();
}
3. Next is the View layer, style control. File page. css
.b-page {
background: #fff;
box-shadow: 0px 1px 2px 0px #E2E2E2;
}
.page {
width: 100%;
background: #FFF;
text-align: center;
overflow: hidden;
font-size:14px;
margin-top:50px;
}
.page .first,
.page .prev,
.page .current,
.page .num,
.page .current,
.page .next,
.page .end {
padding: 8px 16px;
margin: 0px 5px;
display: inline-block;
color: #144970;
border: 1px solid #F2F2F2;
border-radius: 5px;
}
.page .first:hover,
.page .prev:hover,
.page .current:hover,
.page .num:hover,
.page .current:hover,
.page .next:hover,
.page .end:hover {
text-decoration: none;
background: #F8F5F5;
}
.page .current {
background-color: #144970;
color: #FFF;
border-radius: 5px;
}
.page .current:hover {
text-decoration: none;
background: #144970;
}
.page .not-allowed {
cursor: not-allowed;
}
For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.