php 自动分页类函数_PHP教程

WBOY
Release: 2016-07-20 11:09:43
Original
990 people have browsed it

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     {
        return $sql;
    }
    // step .4 获取当前页数
    $pn = isset($_get[$flag]) ? (int)$_get[$flag] : 1;
    if ($pn     // 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 = ' / 上一页';
    }
    $nxt = ' / 下一页';
    $html = '首页'.$pre.$nxt.' / 尾页';
    $__t_page_moyo_html = $html;
    return $sql;
}


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

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

 


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444794.htmlTechArticlephp教程 自动分页类函数 不想重复的写sql代码,就用下面的函数去自动处理了。 $__t_page_moyo_html = ''; /** * * 临时代码:分页处理函数 * @para...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!