PHP头条
热点:

php 自动分页类函数


php教程 自动分页类函数

不想重复的写sql代码,就用下面的函数去自动处理了。

$__t_page_moyo_html = '';
/**
*
* 临时代码:分页处理函数
* @param string $sql
*/
function page_moyo($sql = '')
{
    global $__t_page_moyo_html;
    if ($sql == '')
    {
        return $__t_page_moyo_html;
    }
    // config
    $max = 12;
    $flag = 'page';
    // step .1 处理sql语句
    $sql_count = preg_replace('/select.*?from/is', 'select count(*) as mcnt from', $sql);
    // step .2 获取数据量
    $result = dbc()->query($sql_count)->getrow();
    $total = $result['mcnt'];
    // step .3 判断是否需要分页
    if ($total < $max)
    {
        return $sql;
    }
    // step .4 获取当前页数
    $pn = isset($_get[$flag]) ? (int)$_get[$flag] : 1;
    if ($pn <= 0) $pn = 1;
    // step .5 重组sql语句
    $sql = $sql . ' limit '.($pn-1)*$max.','.$max;
    // step .6 组装分页html代码
    $url = $_server['request_uri'];
    if (preg_match('/'.$flag.'=d+/i', $url))
    {
        $url = preg_replace('/[&]?'.$flag.'=d+/', '', $url);
    }
    $pageall = ceil($total/$max);
    $pre = '';
    if ($pn > 1)
    {
        $pre = ' / <a href="'.$url.'&'.$flag.'='.($pn-1).'">上一页</a>';
    }
    $nxt = ' / <a href="'.$url.'&'.$flag.'='.($pn+1).'">下一页</a>';
    $html = '<a href="'.$url.'">首页</a>'.$pre.$nxt.' / <a href="'.$url.'&'.$flag.'='.$pageall.'">尾页</a>';
    $__t_page_moyo_html = $html;
    return $sql;
}


在进行sql查询前用page_moyo处理一下sql语句
     $sql = page_moyo($sql);
然后在需要显示分页链接的地方直接调用page_moyo输出
     echo page_moyo();

如果要连续进行两次大数据量sql查询的话就要在第一次查询后存储下当时的分页代码,不然下个sql查询就会覆盖掉了

 

www.phpzy.comtrue/php/7602.htmlTechArticlephp 自动分页类函数 php教程 自动分页类函数 不想重复的写sql代码,就用下面的函数去自动处理了。 $__t_page_moyo_html = ''; /** * * 临时代码:分页处理函数 * @param string $sql */ function page_moy...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐