php分页函数

/**
 * 分页函数
 * by hkshadow
 * 2011-04-07
 * Enter description here ...
 * @param 结果总条数count(); $num
 * @param 每页条数 $perpage
 * @param 当前页 $curpage
 * @param 分页地址$url = 'index.php?' . $para; $mpurl
 * @param 数字分页数量 $page
 * $multipage = ListMulti ( $max, $tpp, $page, $url ,0,5);
 */

function ListMulti($num, $perpage, $curpage, $mpurl, $maxpages = 0, $page = 10, $autogoto = TRUE, $simple = FALSE) {
    $multipage = '';
    $mpurl .= strpos ( $mpurl, '?' ) ? '&' : '?';
    $realpages = 1;
    /*  if($num > $perpage)
    {*/
    $offset = 2;
    $realpages = @ceil ( $num / $perpage );
    $pages = $maxpages && $maxpages < $realpages ? $maxpages : $realpages;
    if ($page > $pages) {
        $from = 1;
        $to = $pages;
    } else {
        $from = $curpage - $offset;
        $to = $from + $page - 1;
        if ($from < 1) {
            $to = $curpage + 1 - $from;
            $from = 1;
            if ($to - $from < $page) {
                $to = $page;
            }
        } elseif ($to > $pages) {
            $from = $pages - $page + 1;
            $to = $pages;
        }
    }
    $multipage = '<li><a href="' . $mpurl . 'page=1" class="prev">首页</a></li>';
    $multipage .= ($curpage - $offset > 1 && $pages > '<li>' . $page . '</li>' ? '<li><a href="' . $mpurl . 'page=1" class="first">1 ...</a></li>' : '') . ($curpage > 1 && ! $simple ? '<li><a href="' . $mpurl . 'page=' . ($curpage - 1) . '" class="prev">上一页</a></li>' : '');
    for($i = $from; $i <= $to; $i ++) {
        $multipage .= $i == $curpage ? '<li class=currentPage>' . $i . '</li>' : '<li><a href="' . $mpurl . 'page=' . $i . '">' . $i . '</a></li>';
    }

    $multipage .= ($curpage < $pages && ! $simple ? '<li><a href="' . $mpurl . 'page=' . ($curpage + 1) . '">下一页</a></li><li><a href="' . $mpurl . 'page=' . $pages . '" class="next">末页</a></li>' : '') . ($to < $pages ? '<li><a href="' . $mpurl . 'page=' . $pages . '" class="last">... ' . $realpages . '</a></li>' : '') . (! $simple && $pages > $page && 0 > 1 ? '<input type="text" name="custompage" size="3" onkeydown="if(event.keyCode==13) {window.location=\'' . $mpurl . 'page=\'+this.value; return false;}" />' : '');

    $multipage = $multipage ? $multipage . (! $simple ? '<li>共 ' . $pages . '  页' . $num . '  条</li>' : '') : '';
    /*  }*/
    return $multipage;
}

使用方法


$max = $dsql->ExecuteNoneQuery2 ( $sql ); //返回一个总数结果
//分页
$tpp = "4";
$realpages = @ceil ( $max / $tpp );
if ($page > $realpages) {
    $page = $realpages;
}
$para = "?mtype=0&uid=$uid&action=mybidding";   //地址

$page = isset ( $page ) ? max ( 1, intval ( $page ) ) : 1;

$start_limit = ($page - 1) * $tpp;

$url = 'index.php' . $para;

$multipage = ListMulti ( $max, $tpp, $page, $url );

    //$sql.= $limitsql . $start_limit . ',' . $tpp;

    echo $multipage;   //输出分页 

Leave a Reply

(will not be published)