CommonWeb.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: songyongming
  5. * Date: 14/12/2
  6. * Time: 22:32
  7. */
  8. class CommonWeb
  9. {
  10. //分页
  11. public static function pager($total_items, $items_per_page=20,$current_page=1, $url,$client_type){
  12. //echo($url);exit;
  13. if($client_type == 'pc'){
  14. return self::pager_pc($total_items, $items_per_page,$current_page, $url);
  15. }else{
  16. return self::pager_mobile($total_items, $items_per_page,$current_page, $url);
  17. }
  18. }
  19. //PC版的分页
  20. public static function pager_pc($total_items, $items_per_page=20,$current_page=1, $url){
  21. //echo($url);exit;
  22. if ($total_items <= $items_per_page) {
  23. return '';
  24. }
  25. if($items_per_page==0){
  26. return '';
  27. }
  28. $pages_str='<ul class="am-pagination am-pagination-centered">';
  29. $max_pages = ceil($total_items / $items_per_page );
  30. //上一页
  31. if($current_page>1){
  32. $page=$current_page-1;
  33. if($page == 1){
  34. $pages_str.='<li><a href="'.str_replace("/%d%/", '', $url).'">&laquo;</a></li>';
  35. }else{
  36. $pages_str.='<li><a href="'.str_replace("%d%", $page, $url).'">&laquo;</a></li>';
  37. }
  38. }
  39. //第一页
  40. if($current_page>1){
  41. $page_str='<li><a href="'.str_replace("/%d%/", '', $url).'">1</a></li>';
  42. $pages_str.=$page_str;
  43. }else{
  44. $pages_str.='<li class="am-active"><a href="#">1</a></li>';
  45. }
  46. //显示当前两页的前两页到后两页
  47. if($max_pages-$current_page<3){
  48. $prev_2_page=$max_pages-5;
  49. }else{
  50. $prev_2_page=$current_page-2;
  51. }
  52. if($prev_2_page<2){
  53. $prev_2_page=2;
  54. }
  55. $next_2_page=$prev_2_page+5;
  56. if($next_2_page>$max_pages) $next_2_page=$max_pages;
  57. for($i=$prev_2_page; $i<=$next_2_page; $i++){
  58. if($i==$current_page){
  59. $pages_str.='<li class="am-active"><a href="#">'.$i.'</a></li>';
  60. }else{
  61. $pages_str.='<li><a href="'.str_replace("%d%", $i, $url).'">'.$i.'</a></li>';
  62. }
  63. }
  64. //下一页
  65. if($current_page<$max_pages){
  66. $page=$current_page+1;
  67. $pages_str.='<li><a href="'.str_replace("%d%", $page, $url).'">&raquo;</a></li>';
  68. }
  69. $pages_str.'</ul>';
  70. return $pages_str;
  71. }
  72. //手机版的分页
  73. public static function pager_mobile($total_items, $items_per_page=20,$current_page=1, $url){
  74. if ($total_items <= $items_per_page) {
  75. return '';
  76. }
  77. if($items_per_page==0){
  78. return '';
  79. }
  80. $pages_str='<ul data-am-widget="pagination" class="am-pagination am-pagination-select">';
  81. $max_pages = ceil($total_items / $items_per_page );
  82. //上一页
  83. if($current_page>1){
  84. $page=$current_page-1;
  85. $pages_str.='<li class="am-pagination-prev "><a href="'.str_replace("%d%", $page, $url).'">上一页</a></li>';
  86. }
  87. $pages_str.='<li class="am-pagination-select"><select>';
  88. for($i=1; $i<=$max_pages; $i++){
  89. if($i==$current_page){
  90. $pages_str.='<option value="#" class="">'.$i.'/'.$max_pages.'</option>';
  91. }
  92. }
  93. $pages_str.= '</select></li>';
  94. //下一页
  95. if($current_page<$max_pages){
  96. $page=$current_page+1;
  97. $pages_str.='<li class="am-pagination-next "><a href="'.str_replace("%d%", $page, $url).'">下一页</a></li>';
  98. }
  99. $pages_str.'</ul>';
  100. return $pages_str;
  101. }
  102. //判断终端类型
  103. //返回设备类型
  104. public static function checkmobile() {
  105. $mobile = array();
  106. static $touchbrowser_list =array('iphone', 'android', 'phone', 'mobile', 'wap', 'netfront', 'java', 'opera mobi', 'opera mini',
  107. 'ucweb', 'windows ce', 'symbian', 'series', 'webos', 'sony', 'blackberry', 'dopod', 'nokia', 'samsung',
  108. 'palmsource', 'xda', 'pieplus', 'meizu', 'midp', 'cldc', 'motorola', 'foma', 'docomo', 'up.browser',
  109. 'up.link', 'blazer', 'helio', 'hosin', 'huawei', 'novarra', 'coolpad', 'webos', 'techfaith', 'palmsource',
  110. 'alcatel', 'amoi', 'ktouch', 'nexian', 'ericsson', 'philips', 'sagem', 'wellcom', 'bunjalloo', 'maui', 'smartphone',
  111. 'iemobile', 'spice', 'bird', 'zte-', 'longcos', 'pantech', 'gionee', 'portalmmm', 'jig browser', 'hiptop',
  112. 'benq', 'haier', '^lct', '320x320', '240x320', '176x220', 'windows phone');
  113. static $wmlbrowser_list = array('cect', 'compal', 'ctl', 'lg', 'nec', 'tcl', 'alcatel', 'ericsson', 'bird', 'daxian', 'dbtel', 'eastcom',
  114. 'pantech', 'dopod', 'philips', 'haier', 'konka', 'kejian', 'lenovo', 'benq', 'mot', 'soutec', 'nokia', 'sagem', 'sgh',
  115. 'sed', 'capitel', 'panasonic', 'sonyericsson', 'sharp', 'amoi', 'panda', 'zte');
  116. static $pad_list = array('ipad');
  117. if(isset($_SERVER['HTTP_USER_AGENT'])){
  118. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  119. }else{
  120. $useragent = '';
  121. }
  122. if(CommonFn::dstrpos($useragent, array('windows'))) {
  123. return 'pc';
  124. }
  125. if(CommonFn::dstrpos($useragent, $pad_list)) {
  126. return 'pad';
  127. }
  128. if(($v = CommonFn::dstrpos($useragent, $touchbrowser_list, true))){
  129. if(strpos($useragent, 'iphone') || strpos($useragent, 'ipod')){
  130. return 'ios';
  131. }else{
  132. return 'android';
  133. }
  134. }
  135. if(($v = CommonFn::dstrpos($useragent, $wmlbrowser_list))) {
  136. return 'android';
  137. }
  138. return 'pc';
  139. }
  140. public static function header_redirect($url, $code=301){
  141. header('Location: '.$url, true, $code);
  142. return false;
  143. }
  144. }