欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

分页代码 有大用

shiping1 的头像
1)把下面的内容保存为 Page.class.php

<?php
/**
 * Page class
 *
 * @author  chenyifei <chenyifei@altech-it.cn>
 * @version 20090609
*/
class Page 
{   
    static function escapeHtml($text)
    {
        if (is_array($text))
        {
            foreach ($text as $key => $value)
            {
                $text[$key] = Page::escapeHtml($value);
            }
        }
        else 
        {
            $text = htmlspecialchars($text, ENT_QUOTES);
        }
        return $text;
    }
    static function pager($nums, $current, $limit)
    {
        // init
        $pager = array(
            'all'       => 0, 
            'from'      => 0, 
            'to'        => 0, 
            'pages'     => array(), 
            'prev_page' => 0, 
            'next_page' => 0
        );
        
        // offset
        $offset = ($current * $limit) - $limit;
        // all nums
        $pager['all'] = $nums;
        // from
        $pager['from'] = $offset + 1;
        // to
        $pager['to'] = (($offset + $limit) > $pager['all']) ? $pager['all'] : $offset + $limit;
        // pages array
        $cnt = ceil($nums / $limit);
        for($i =1; $i <= $cnt; $i++)
        {
            $pager['pages'][] = $i;
        }
        // prev
        $pager['prev_page'] = $current - 1;
        // next
        $pager['next_page'] = $current + 1;
        
      // current
        $pager['curr_page'] = $current;
      
        $pager['from'] = $pager['from'] > $pager['to'] ?    $pager['to'] : $pager['from'];
        return $pager;
    }    
    
    static function foldFlag()
    {
        $dirArr = split('/', dirname($_SERVER['SCRIPT_FILENAME']));
        $foldName = $dirArr[count($dirArr)-1];
        switch ($foldName)
        {
            case 'account':
                $flag = 2;
                break;
            case 'corp':
                $flag = 3;
                break;
            case 'project':
                $flag = 4;
                break;
            case 'report':
                $flag = 5;
                break;
            case 'summary':
                $flag = 6;
                break;
            case 'user':
                $flag = 7;
                break;
            default:
                $flag = 1;
        }
        if ($flag == 1)
        {
            $fileName = basename($_SERVER['SCRIPT_FILENAME'], strrchr($_SERVER['SCRIPT_FILENAME'], '.'));
            if (strtolower($fileName) == 'logout')
            {
                $flag == 8;
            }
        }
        return $flag;
    }

    static function pagerHtml($nums, $current, $limit)
    {

        $pager =  Page::pager($nums, $current, $limit);
        $htmlStr = "<ul class='page'>";
        $htmlStr .="<li class='pageindex'><a href='?pa=1' >首页</a></li>";
        empty($pager['prev_page']) &&  $pager['prev_page']=1;
        $htmlStr .="<li class='pageprev'><a href='?pa=".$pager['prev_page']."' >上一页</a></li>";

        foreach($pager['pages'] as $key=>$value)
        {
            $value == $pager['curr_page'] ? $current='current':$current='';
            $htmlStr .="<li class='pagenum ".$current."'><a href='?pa=".$value."' >".$value."</a></li>";
        }
        $pager['next_page']>$value && $pager['next_page']=$value;
        $htmlStr .="<li class='pageprev'><a href='?pa=".$pager['next_page']."' >下一页</a></li>";
        $htmlStr .="<li class='pagelast'><a href='?pa=".$value."' >末页</a></li>";
        $htmlStr .= "</ul>";
        return $htmlStr;

    }

}

?>

2)在主php中引入 Page.class.php
主php中的代码大约为

<?php  
require_once './include/User.inc.php';//(这个User.inc.php里面引入Page.class.php及$net所需要的php文件)
$pa = intval($_GET['pa']) > 0 ? intval($_GET['pa']) : 1;
$nets = $net->getNetsByUserId($pa,PAGE_LIMIT,$table_prefix);
$Smarty->assign("nets", $nets);
$Smarty->assign("pagerHtml", Page::pagerHtml($net->getNetsCountByUserId($table_prefix), $pa, PAGE_LIMIT));
// display
$Smarty->display("netStatic.htm");
?>
3)在模板文件中 但使用 {$pagerHtml} 就得到分页的内容
普通分类: