php 分页

用的是ci框架,辅助函数文件里helper/view_helper.php

<?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

// 配置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

<!--  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 -->

  

posted @ 2016-01-26 14:21  miyaye  阅读(280)  评论(0编辑  收藏  举报