thinkphp The default pagination style is very bad, more than the page will appear style disorder, how to implement pagination with ellipsis? In fact, the method is very simple. This method is pure and natural, pollution-free.
The code is as follows:
| The code is as follows |
Copy Code |
Class Indexaction extends Action { Public Function index () { $M = M ("article_21"); $count = $M->count (); Import ("ORG.") Util.page "); $page = new Page ($count, 10); $showPage = $page->show ();//thinkphp Original method $showPage = $page->shows ()///custom method $list = $M->limit ($page->firstrow, $page->listrows)->select ();;
$this->assign ("page", $showPage); $this->assign ("list", $list); $this->display (); } } |
Find the paging class in the thinkphp core package
Add the code below:
| The code is as follows |
Copy Code |
Public function shows () { $adjacents = 2; if (0 = = $this->totalrows) return "; $p = $this->varpage; $nowCoolPage = ceil ($this->nowpage/$this->rollpage);
Analyze Paging parameters if ($this->url) { $depr = C (' Url_pathinfo_depr '); $url = RTrim (U ('/'. $this->url, ', false), $DEPR). $depr. ' __page__ '; }else{ if ($this->parameter && is_string ($this->parameter)) { Parse_str ($this->parameter, $parameter); }elseif (Is_array ($this->parameter)) { $parameter = $this->parameter; }elseif (Empty ($this->parameter)) { Unset ($_get[c (' var_url_params ')); $var =!empty ($_post) $_post:$_get; if (empty ($var)) { $parameter = Array (); }else{ $parameter = $var; } } $parameter [$p] = ' __page__ '; $url = U (', $parameter); }
Page up and down string $upRow = $this->nowpage-1; $downRow = $this->nowpage+1;
Previous page if ($upRow >0) { $pages. = "<a href= '". Str_replace (' __page__ ', $upRow, $url). "' class= ' prev ' > '. $this->config[' prev ']." </a> "; }else{ $pages. = "<a class= ' prev current ' >". $this->config[' prev ']. " </a> "; }
First page if ($this->nowpage> ($adjacents + 1)) { $pages. = "<a href= '". Str_replace (' __page__ ', 1, $url). "' >1</a> "; }
Add ellipsis if ($this->nowpage> ($adjacents +2)) { $pages. = "<a>...</a>"; }
12345 $pmin = ($this->nowpage> $adjacents)? ($this->nowpage-$adjacents): 1; $pmax = ($this->nowpage< ($this->totalpages-$adjacents))? ($this->nowpage+ $adjacents): $this->totalpages; for ($i = $pmin; $i <= $pmax; $i + +) { if ($i = = $this->nowpage) { $pages. = "<a class= ' current ' >". $i. " </a> "; }else{ $pages. = "<a href= '". Str_replace (' __page__ ', $i, $url). "' > ". $i." </a> "; } }
Add ellipsis if ($this->nowpage < ($this->totalpages-$adjacents-1)) { $pages. = "<a>...</a>"; }
Last page if ($this->nowpage< ($this->totalpages-$adjacents)) {
$pages. = "<a href= '". Str_replace (' __page__ ', $this->totalpages, $url). "' > ". $this->totalpages." </a> "; }
Next page if ($downRow <= $this->totalpages) { $pages. = "<a href= '". Str_replace (' __page__ ', $downRow, $url). "' class= ' Next ' > '. $this->config[' next '." </a> "; }else{ $pages. = "<a class= ' next current ' >". $this->config[' next '. " </a> "; } return $pages; } HTML code: <title></title> <style> A{text-decoration:none;} . Pages a{ border:1px solid #ccc; Display:inline-block; height:20px; line-height:20px; Margin:0 5px; Text-align:center; width:20px; Color: #222; Background-color: #eee; } . pages >.prev {width:60px} . pages >.next {width:60px} . pages >.current{color:red;border:1px solid red; . Box UL Li{list-style:none; </style> <body> <div> <div class= "box" > <ul> <volist name= "list" id= "V" key= "K" > <li>{$k}->{$v .title}</li> </volist> </ul> </div> <div class= "Pages" > {$page} </div> </div> </body> |
Effect Chart: