php 分页
用的是ci框架,辅助函数文件里helper/view_helper.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | <?php defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' ); /** * CodeIgniter views Helpers * * @package htdocs * @subpackage application * @category helpers */ /** * 页码构造函数 * * @param array $params * * @return string */ function pagers($ params ){ if (!$ params [ 'current' ]) $ params [ 'current' ] = 1; if (!$ params [ 'total' ]) $ params [ 'total' ] = 1; if ($ params [ 'total' ] < 1){ return '' ; } $prev = $ params [ 'current' ] > 1? '<a href="' .str_replace($ params [ 'token' ],$ params [ 'current' ] - 1, $ params [ 'link' ]). '" class="prev" title="上一页">«上一页</a>' : '<a href="javascript:" class="prev" title="上一页">«上一页</a>' ; $next = $ params [ 'current' ] < $ params [ 'total' ]? '<a href="' .str_replace($ params [ 'token' ], $ params [ 'current' ]+1, $ params [ 'link' ]). '" class="next last" title="下一页">下一页»</a>' : '<a href="javascript:" class="next" title="下一页">«下一页</a>' ; // if($params['type']=='mini'){ // return <<<EOF // <div class="pager"><strong class="pagecurrent">{$params['current']}</strong><span class="line">/</span><span class="pageall">{$params['total']}</span>{$prev}{$next}</div> // EOF; // }else{ $c = $ params [ 'current' ]; $t = $ params [ 'total' ]; $v = array(); $l = $ params [ 'link' ]; $p = $ params [ 'token' ]; // 仿JD page rule if ($t < 11){ // $t是总页数,当总页数 > 11的时候 $v[] = pager_link(1, $t, $l, $p, $c); //pager_link 是构造分页的函数 pager_link( $from,$to,$link,$站位,$c当前页面 ) } else { if ($c < 6) { //当前页数<6,总页数>=11 eg:c=5,t=12 || 1 2 3 4 5c 6 7 ... 12 $v[] = pager_link(1, $c + 2, $l, $p, $c); //1 2 3 4 5c 6 7 $v[] = pager_link($t, $t, $l, $p); //12 } elseif($t - $c < 4) { //最后4页的时候 eg:c=11,t=12 || 1 ... 10 11c 12 $v[] = pager_link(1, 1, $l, $p); //1 $v[] = pager_link($c - 1, $t, $l, $p, $c); // 10 11c 12 } else { // 为了适应700像素,小改动 前1 后2 //当c>=6 并且不是最后4页的时候 5,11 1..4567...11 $v[] = pager_link(1, 1, $l, $p); $v[] = pager_link($c - 1, $c + 2, $l, $p, $c); $v[] = pager_link($t, $t, $l, $p); } } $links = implode( '<b class="pn-break">...</b>' ,$v); return <<<EOF <span class = "p-num" >{$prev}{$links}{$next}</span> EOF; // } } /** * 页码链接地址 */ function pager_link($ from ,$to,$l,$p,$c= null ){ for ($i=$ from ;$i<$to+1;$i++){ if ($c==$i){ $r[]= '<a href="javascript:;" class="curr">' .$i. '</a>' ; } else { $r[]= ' <a href="' .str_replace($p,$i,$l). '">' .$i. '</a> ' ; } } return implode( ' ' ,$r); } /** * CI pager */ function ci_pager($ params ) { $ this ->load->library( 'pagination' ); return $ this ->pagination->initialize($ params ); } /** * 截取字符串 * * @param string $string * @param int $length * @param string $ext * * @param string $res */ function cut_str($ string , $length, $ext = '...' ) { $res = '' ; if (!$length || $length < 1) return '' ; if ($ string {$length+1}) { $i = 0; $vchar_num = 0; while ($vchar_num < $length && $i < strlen($ string )) { $ord_char = ord($ string {$i}); if ($ord_char < 192) { $res .= $ string {$i}; $i++; } elseif($ord_char < 224) { $res .= $ string {$i} . $ string {$i + 1}; $i += 2; } else { $res .= $ string {$i} . $ string {$i + 1} . $ string {$i + 2}; $i += 3; } $vchar_num++ ; } } else { $res = $ string ; } if (!empty($ext) && $i && $ string {$i}) $res .= $ext; return $res; } /** * 构造链接 * * @param array $model * @param array $before_params * @param array $add_url_str * @param string $target_blank //是否在当前页,不是则直需要重新构造@TODO * jd,官网采用单页html格式,而采用友好链需解析后才能构造 * * @return string */ function com_url($model, $before_params = array(), $add_url_str = array(), $target_blank = false ) { $new_url = '' ; $site = site_url(); if (!$target_blank) { $parse_url_arr = array(); $new_url .= $site; if (!empty($model) && is_array($model)) { $new_url .= implode( '/' , $model); } if (!empty($before_params) && is_array($before_params)) { foreach ($before_params as $bk => &$bv) { if (isset($add_url_str[$bk])) { $bv = $add_url_str[$bk]; } } $new_url .= '/' .implode( '/' , $before_params). '/' ; } } else { // @TODO } return $new_url; } /** * 格式化数字 * * 慎用此函数,前台购物车累计与接口提交计算有出入不一致时,影响,建议不知道接口计算规则 * 情况下不使用此函数 * @todo 汇率多国货币class * * @param number $number * @param int $decimal * @param string $operater * * @return number */ function format_number($number, $ decimal , $operater = 'round' ) { $bm = bcmul($number, pow(10, $ decimal ), $ decimal ); $opres = call_user_func($operater, $bm); $result = bcdiv($opres, pow(10, $ decimal ), $ decimal ); return $result; } |
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 配置pagination $token = time(); $current_checked[ 'page' ] = $token; $page_params = array( 'current' => $current_page, 'total' => $list_select->TOTAL_PAGE, 'totallistnum' => $list_select->RECORD_COUNT, 'defaultListnum' => $page_size, 'pageLimit' => $page_size, 'link' => com_url($ this ->data[ 'c_m' ], $current_checked, '' ), 'token' => $token ); $ this ->data[ 'pager' ] = $page_params; |
view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <!-- page ***************************************************** --> <? if ($pager): ?> <div class = "page clearfix" > <div id= "J_bottomPage" class = "p-wrap" > <!-- <span class = "p-num" > <a class = "pn-prev disabled" > <i><</i> <span>上一页</span> </a> <a href= "javascript:;" class = "curr" >1</a> <a href= "javascript:;" >2</a> <a href= "javascript:;" >3</a> <a href= "javascript:;" >4</a> <a href= "javascript:;" >5</a> <b class = "pn-break" >...</b> <a class = "pn-next" href= "javascript:;" title= "使用方向键右键也可翻到下一页哦!" > <span>下一页</span> <i>></i> </a> </span> --> <?php echo pagers($pager); ?> <span class = "p-skip" > <input type= "hidden" id= "first_page_x" value= "<? echo com_url($c_m, $current_checked, array('page' => 1)); ?>" /> <span>共<? echo $pager[ 'total' ]; ?>页 到第</span> <input id= "p_j_num" class = "input-txt" type= "text" maxlength= "4" value= "<? echo $current?$current:$pager['current']; ?>" onkeydown= "javascript:if(event.keyCode==13){SEARCH.page_jump(100,1);return false;}" /> <span>页</span> <a class = "btn btn-default" onclick= "MGB.page_jump()" href= "javascript:;" >确定</a> </span> </div> </div> <? endif; ?> <!-- *************************************************************page --> |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步