page.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. /**
  3. * 分页类
  4. *
  5. *
  6. * @package
  7. */
  8. defined('InShopNC') or exit('Access Invalid!');
  9. class Page{
  10. /**
  11. * url参数中页码参数名
  12. */
  13. private $page_name = 'curpage';
  14. /**
  15. * 信息总数
  16. */
  17. private $total_num = 1;
  18. /**
  19. * 页码链接
  20. */
  21. private $page_url = '';
  22. /**
  23. * 每页信息数量
  24. */
  25. private $each_num = 10;
  26. /**
  27. * 当前页码
  28. */
  29. private $now_page = 1;
  30. /**
  31. * 设置页码总数
  32. */
  33. private $total_page = 1;
  34. /**
  35. * 输出样式
  36. * 4、5为商城伪静态专用样式
  37. */
  38. private $style = 2;
  39. /**
  40. * ajax 分页 预留,目前先不使用
  41. * 0为不使用,1为使用,默认为0
  42. */
  43. private $ajax = 0;
  44. /**
  45. * 首页
  46. */
  47. private $pre_home = '';
  48. /**
  49. * 末页
  50. */
  51. private $pre_last = '';
  52. /**
  53. * 上一页
  54. */
  55. private $pre_page = '';
  56. /**
  57. * 下一页
  58. */
  59. private $next_page = '';
  60. /**
  61. * 页码 样式左边界符
  62. */
  63. private $left_html = '<li>';
  64. /**
  65. * 页码 样式右边界符
  66. */
  67. private $right_html = '</li>';
  68. /**
  69. * 选中样式左边界符
  70. */
  71. private $left_current_html = '<li>';
  72. /**
  73. * 选中样式右边界符
  74. */
  75. private $right_current_html = '</li>';
  76. /**
  77. * 省略号样式左边界符
  78. */
  79. private $left_ellipsis_html = '<li>';
  80. /**
  81. * 省略号样式右边界符
  82. */
  83. private $right_ellipsis_html = '</li>';
  84. /**
  85. * 在页码链接a内部的样式 <a>(样式名)页码(样式名)</a>
  86. */
  87. private $left_inside_a_html = '';
  88. /**
  89. * 在页码链接a内部的样式 <a>(样式名)页码(样式名)</a>
  90. */
  91. private $right_inside_a_html = '';
  92. /**
  93. * 构造函数
  94. *
  95. * 数据库使用到的方法:
  96. * $this->setTotalNum($total_num);
  97. * $this->getLimitStart();
  98. * $this->getLimitEnd();
  99. *
  100. * @param
  101. * @return
  102. */
  103. public function __construct(){
  104. Language::read('core_lang_index');
  105. $lang = Language::getLangContent();
  106. $this->pre_home = $lang['first_page'];
  107. $this->pre_last = $lang['last_page'];
  108. $this->pre_page = $lang['pre_page'];
  109. $this->next_page = $lang['next_page'];
  110. /**
  111. * 设置当前页码
  112. */
  113. $this->setNowPage($_GET[$this->page_name]);
  114. /**
  115. * 设置当前页面的页码url
  116. * 商城伪静态分页不需要使用这个方法
  117. */
  118. if (!in_array($this->style, array(4,5))) {
  119. $this->setPageUrl();
  120. }
  121. }
  122. /**
  123. * 取得属性
  124. *
  125. * @param string $key 属性键值
  126. * @return string 字符串类型的返回结果
  127. */
  128. public function get($key){
  129. return $this->$key;
  130. }
  131. /**
  132. * 设置属性
  133. *
  134. * @param string $key 属性键值
  135. * @param string $value 属性值
  136. * @return bool 布尔类型的返回结果
  137. */
  138. public function set($key,$value){
  139. return $this->$key = $value;
  140. }
  141. /**
  142. * 设置url页码参数名
  143. *
  144. * @param string $page_name url中传递页码的参数名
  145. * @return bool 布尔类型的返沪结果
  146. */
  147. public function setPageName($page_name){
  148. $this->page_name = $page_name;
  149. return true;
  150. }
  151. /**
  152. * 设置当前页码
  153. *
  154. * @param int $page 当前页数
  155. * @return bool 布尔类型的返回结果
  156. */
  157. public function setNowPage($page){
  158. $this->now_page = intval($page)>0?intval($page):1;
  159. return true;
  160. }
  161. /**
  162. * 设置每页数量
  163. *
  164. * @param int $num 每页显示的信息数
  165. * @return bool 布尔类型的返回结果
  166. */
  167. public function setEachNum($num){
  168. $this->each_num = intval($num)>0?intval($num):10;
  169. return true;
  170. }
  171. /**
  172. * 设置输出样式
  173. *
  174. * @param int $style 样式名
  175. * @return bool 布尔类型的返回结果
  176. */
  177. public function setStyle($style){
  178. $this->style = ($style == 'admin' ? 2:$style);
  179. return true;
  180. }
  181. /**
  182. * 设置信息总数
  183. *
  184. * @param int $total_num 信息总数
  185. * @return bool 布尔类型的返回结果
  186. */
  187. public function setTotalNum($total_num){
  188. $this->total_num = $total_num;
  189. return true;
  190. }
  191. /**
  192. * 取当前页码
  193. *
  194. * @param
  195. * @return int 整型类型的返回结果
  196. */
  197. public function getNowPage(){
  198. return $this->now_page;
  199. }
  200. /**
  201. * 取页码总数
  202. *
  203. * @param
  204. * @return int 整型类型的返回结果
  205. */
  206. public function getTotalPage(){
  207. $this->setTotalPage();
  208. return $this->total_page;
  209. }
  210. /**
  211. * 取信息总数
  212. *
  213. * @param
  214. * @return int 整型类型的返回结果
  215. */
  216. public function getTotalNum(){
  217. return $this->total_num;
  218. }
  219. /**
  220. * 取每页信息数量
  221. *
  222. * @param
  223. * @return int 整型类型的返回结果
  224. */
  225. public function getEachNum(){
  226. return $this->each_num;
  227. }
  228. /**
  229. * 取数据库select开始值
  230. *
  231. * @param
  232. * @return int 整型类型的返回结果
  233. */
  234. public function getLimitStart(){
  235. if ($this->getNowPage() <= 1){
  236. $tmp = 0;
  237. }else {
  238. $this->setTotalPage();
  239. $this->now_page = $this->now_page > $this->total_page ? $this->total_page : $this->now_page;
  240. $tmp = ($this->getNowPage()-1)*$this->getEachNum();
  241. }
  242. return $tmp;
  243. }
  244. /**
  245. * 取数据库select结束值
  246. *
  247. * @param
  248. * @return int 整型类型的返回结果
  249. */
  250. public function getLimitEnd(){
  251. $tmp = $this->getNowPage()*$this->getEachNum();
  252. if ($tmp > $this->getTotalNum()){
  253. $tmp = $this->getTotalNum();
  254. }
  255. return $tmp;
  256. }
  257. /**
  258. * 设置页码总数
  259. *
  260. * @param int $id 记录ID
  261. * @return array $rs_row 返回数组形式的查询结果
  262. */
  263. public function setTotalPage(){
  264. $this->total_page = ceil($this->getTotalNum()/$this->getEachNum());
  265. }
  266. /**
  267. * 输出html
  268. *
  269. * @param
  270. * @return string 字符串类型的返回结果
  271. */
  272. public function show($style = null){
  273. /**
  274. * 设置总数
  275. */
  276. $this->setTotalPage();
  277. if (!is_null($style)){
  278. $this->style = $style;
  279. }
  280. $html_page = '';
  281. $this->left_current_html = '<li><span class="currentpage">';
  282. $this->right_current_html = '</span></li>';
  283. $this->left_inside_a_html = '<span>';
  284. $this->right_inside_a_html = '</span>';
  285. switch ($this->style) {
  286. case '1':
  287. $html_page .= '<ul>';
  288. if ($this->getNowPage() <= 1){
  289. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</li>';
  290. }else {
  291. $html_page .= '<li><a class="demo" href="'. $this->page_url . ($this->getNowPage()-1) .'">'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</a></li>';
  292. }
  293. if ($this->getNowPage() == $this->getTotalPage() || $this->getTotalPage() == 0){
  294. $html_page .= '<li>'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</li>';
  295. }else {
  296. $html_page .= '<li><a class="demo" href="'. $this->page_url . ($this->getNowPage()+1) .'">'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</a></li>';
  297. }
  298. $html_page .= '</ul>';
  299. break;
  300. case '2':
  301. $html_page .= '<ul>';
  302. if ($this->getNowPage() <= 1){
  303. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_home.$this->right_inside_a_html.'</li>';
  304. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</li>';
  305. }else {
  306. $html_page .= '<li><a class="demo" href="'. $this->page_url .'1">'.$this->left_inside_a_html.$this->pre_home.$this->right_inside_a_html.'</a></li>';
  307. $html_page .= '<li><a class="demo" href="'. $this->page_url . ($this->getNowPage()-1) .'">'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</a></li>';
  308. }
  309. $html_page .= $this->getNowBar();
  310. if ($this->getNowPage() == $this->getTotalPage() || $this->getTotalPage() == 0){
  311. $html_page .= '<li>'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</li>';
  312. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_last.$this->right_inside_a_html.'</li>';
  313. }else {
  314. $html_page .= '<li><a class="demo" href="'. $this->page_url . ($this->getNowPage()+1) .'">'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</a></li>';
  315. $html_page .= '<li><a class="demo" href="'. $this->page_url . $this->getTotalPage() .'">'.$this->left_inside_a_html.$this->pre_last.$this->right_inside_a_html.'</a></li>';
  316. }
  317. $html_page .= '</ul>';
  318. break;
  319. case '3':
  320. $html_page .= '<ul>';
  321. if ($this->getNowPage() <= 1){
  322. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</li>';
  323. }else {
  324. $html_page .= '<li><a class="demo" href="'. $this->page_url . ($this->getNowPage()-1) .'">'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</a></li>';
  325. }
  326. $html_page .= $this->getNowBar();
  327. if ($this->getNowPage() == $this->getTotalPage() || $this->getTotalPage() == 0){
  328. $html_page .= '<li>'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</li>';
  329. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_last.$this->right_inside_a_html.'</li>';
  330. }else {
  331. $html_page .= '<li><a class="demo" href="'. $this->page_url . ($this->getNowPage()+1) .'">'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</a></li>';
  332. }
  333. $html_page .= '</ul>';
  334. break;
  335. case '4':
  336. $html_page .= '<ul>';
  337. if ($this->getNowPage() <= 1){
  338. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</li>';
  339. }else {
  340. $html_page .= '<li><a class="demo" href="'. $this->setShopPseudoStaticPageUrl($this->getNowPage()-1) .'">'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</a></li>';
  341. }
  342. if ($this->getNowPage() == $this->getTotalPage() || $this->getTotalPage() == 0){
  343. $html_page .= '<li>'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</li>';
  344. }else {
  345. $html_page .= '<li><a class="demo" href="'. $this->setShopPseudoStaticPageUrl($this->getNowPage()+1) .'">'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</a></li>';
  346. }
  347. $html_page .= '</ul>';
  348. break;
  349. case '5':
  350. $html_page .= '<ul>';
  351. if ($this->getNowPage() <= 1){
  352. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_home.$this->right_inside_a_html.'</li>';
  353. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</li>';
  354. }else {
  355. $html_page .= '<li><a class="demo" href="'. $this->setShopPseudoStaticPageUrl('1') .'">'.$this->left_inside_a_html.$this->pre_home.$this->right_inside_a_html.'</a></li>';
  356. $html_page .= '<li><a class="demo" href="'. $this->setShopPseudoStaticPageUrl($this->getNowPage()-1) .'">'.$this->left_inside_a_html.$this->pre_page.$this->right_inside_a_html.'</a></li>';
  357. }
  358. $html_page .= $this->getNowBar();
  359. if ($this->getNowPage() == $this->getTotalPage() || $this->getTotalPage() == 0){
  360. $html_page .= '<li>'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</li>';
  361. $html_page .= '<li>'.$this->left_inside_a_html.$this->pre_last.$this->right_inside_a_html.'</li>';
  362. }else {
  363. $html_page .= '<li><a class="demo" href="'. $this->setShopPseudoStaticPageUrl($this->getNowPage()+1) .'">'.$this->left_inside_a_html.$this->next_page.$this->right_inside_a_html.'</a></li>';
  364. $html_page .= '<li><a class="demo" href="'. $this->setShopPseudoStaticPageUrl($this->getTotalPage()) .'">'.$this->left_inside_a_html.$this->pre_last.$this->right_inside_a_html.'</a></li>';
  365. }
  366. $html_page .= '</ul>';
  367. break;
  368. default:
  369. break;
  370. }
  371. /**
  372. * 转码
  373. */
  374. /**
  375. if (strtoupper(CHARSET) == 'GBK' && !empty($html_page)){
  376. $html_page = iconv('UTF-8','GBK',$html_page);
  377. }
  378. */
  379. return $html_page;
  380. }
  381. /**
  382. * 页码条内容
  383. * 样式为: 前面2个页码 ... 中间7个页码 ...
  384. *
  385. * @param
  386. * @return string 字符串类型的返回结果
  387. */
  388. private function getNowBar(){
  389. /**
  390. * 显示效果
  391. * 中间显示7个,左右两个,不足则不显示省略号
  392. */
  393. /**
  394. * 判断当前页是否大于7
  395. */
  396. if ($this->getNowPage() >= 7){
  397. /**
  398. * 前面增加省略号,并且计算开始页码
  399. */
  400. $begin = $this->getNowPage()-2;
  401. }else {
  402. /**
  403. * 小于7,前面没有省略号
  404. */
  405. $begin = 1;
  406. }
  407. /**
  408. * 计算结束页码
  409. */
  410. if ($this->getNowPage()+5 < $this->getTotalPage()){
  411. /**
  412. * 增加省略号
  413. */
  414. $end = $this->getNowPage()+5;
  415. }else {
  416. $end = $this->getTotalPage();
  417. }
  418. /**
  419. * 整理整个页码样式
  420. */
  421. $result = '';
  422. if ($begin > 1){
  423. $result .= $this->setPageHtml(1,1).$this->setPageHtml(2,2);
  424. $result .= $this->left_ellipsis_html.'<span>...</span>'.$this->right_ellipsis_html;
  425. }
  426. /**
  427. * 中间部分内容
  428. */
  429. for ($i=$begin;$i<=$end;$i++){
  430. $result .= $this->setPageHtml($i,$i);
  431. }
  432. if ($end < $this->getTotalPage()){
  433. $result .= $this->left_ellipsis_html.'<span>...</span>'.$this->right_ellipsis_html;
  434. }
  435. return $result;
  436. }
  437. /**
  438. * 设置单个页码周围html代码
  439. *
  440. * @param string $page_name 页码显示内容
  441. * @param string $page 页码数
  442. * @return string 字符串类型的返回结果
  443. */
  444. private function setPageHtml($page_name,$page){
  445. /**
  446. * 判断是否是当前页
  447. */
  448. if ($this->getNowPage() == $page){
  449. $result = $this->left_current_html.$page.$this->right_current_html;
  450. }else {
  451. if (in_array($this->style, array(4,5))) { // 商城伪静态使用
  452. $result = $this->left_html."<a class='demo' href='". $this->setShopPseudoStaticPageUrl($page) ."'>".$this->left_inside_a_html.$page_name.$this->right_inside_a_html."</a>".$this->right_html;
  453. } else { // 普通分页使用
  454. $result = $this->left_html."<a class='demo' href='". $this->page_url . $page ."'>".$this->left_inside_a_html.$page_name.$this->right_inside_a_html."</a>".$this->right_html;
  455. }
  456. }
  457. return $result;
  458. }
  459. /**
  460. * 取url地址
  461. *
  462. * @param
  463. * @return string 字符串类型的返回结果
  464. */
  465. private function setPageUrl(){
  466. $uri = request_uri() ;
  467. $_SERVER['REQUEST_URI'] = $uri ;
  468. /**
  469. * 不存在QUERY_STRING时
  470. */
  471. if(empty($_SERVER['QUERY_STRING'])){
  472. $this->page_url = $_SERVER['REQUEST_URI']."?".$this->page_name."=";
  473. }else{
  474. if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
  475. /**
  476. * 地址存在页面参数
  477. */
  478. $this->page_url = str_replace($this->page_name.'='.$this->now_page,'',$_SERVER['REQUEST_URI']);
  479. $last = $this->page_url[strlen($this->page_url)-1];
  480. if($last=='?' || $last=='&'){
  481. $this->page_url .= $this->page_name."=";
  482. }else{
  483. $this->page_url .= '&'.$this->page_name."=";
  484. }
  485. }else{
  486. $this->page_url = $_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
  487. }
  488. }
  489. return true;
  490. }
  491. /**
  492. * 取url地址
  493. *
  494. * @param int $page
  495. * @return string 字符串类型的返回结果
  496. */
  497. private function setShopPseudoStaticPageUrl($page){
  498. $param = $_GET;
  499. $act = $param['act'] == '' ? 'index' : $param['act'];
  500. unset($param['act']);
  501. $op = $param['op'] == '' ? 'index' : $param['op'];
  502. unset($param['op']);
  503. $param[$this->page_name] = $page;
  504. return urlShop($act, $op, $param);
  505. }
  506. }