龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php分页

时间:2014-07-22 14:50来源: 作者: 点击:
分享到:
<无详细内容>
分页类
<?php
define("FRONT_AND_BEHIND_LINK_CNT",  10);

class PageUtil{

    var $data_cnt    = 0;
    var $now_page    = 0;
    var $total_page  = 0;
    var $next_link   = FALSE;
    var $prev_link   = FALSE;
    var $disp_begin  = 0;
    var $disp_end    = 0;
    var $link_array  = NULL;


    /**
     * constructor
     *
     * @param    int    data_cnt    データ件数
     * @param    int    disp_cnt    1ページ当り表示件数
     * @param    int    now_page    現ページ
     */
    function PageUtil($data_cnt, $disp_cnt, $now_page = NULL){
        $this->data_cnt = $data_cnt;
        $this->setPageInfo($disp_cnt, $now_page);
    }

    /**
     * ページに関する設定を行います
     *
     * @param    int    disp_cnt    1ページ当り表示件数
     * @param    int    now_page    現ページ
     *
     * @access    public
     */
    function setPageInfo($disp_cnt, $now_page = NULL){

        // now page
        $this->now_page = (NULL == $now_page) ? 1 : $now_page;

        // total page
        $this->total_page = ceil($this->data_cnt / $disp_cnt);

        // link array
        if(($this->now_page - FRONT_AND_BEHIND_LINK_CNT) > 0){
            $start = $this->now_page - FRONT_AND_BEHIND_LINK_CNT;
        }else{
            $start = 1;
        }
        if(($this->now_page + FRONT_AND_BEHIND_LINK_CNT) > $this->total_page){
            $end = $this->total_page;
        }else{
            $end = $this->now_page + (FRONT_AND_BEHIND_LINK_CNT - 1);
        }
        $i = 0;
        for($i = $start; $i <= $end; $i++){
            $this->link_array[] = $i;
        }

        // next link
        if($disp_cnt < $this->data_cnt){
            if($now_page < $this->total_page){
                $this->next_link = TRUE;
            }
        }

        // prev link
        if(1 < $now_page){
            $this->prev_link = TRUE;
        }

        // begin disp number
        $this->disp_begin = ($this->now_page - 1) * $disp_cnt;

        // end disp number
        $tmp_cnt = $this->now_page * $disp_cnt;
        $this->disp_end = ($this->data_cnt < $tmp_cnt) ? $this->data_cnt : $tmp_cnt;
/**********************************************
debug("データ数 :" . (string)$this->data_cnt);
debug("表示件数 :" . (string)$disp_cnt);
debug("現在ページ:" . (string)$now_page);
debug("次へリンク:" . (string)$this->next_link);
debug("前へリンク:" . (string)$this->prev_link);
debug("開始INDEX :" . (string)$this->disp_begin);
debug("終了INDEX :" . (string)$this->disp_end);
***********************************************/
    }
}
?>
*************************;list页面
//ページの処理
$total_num = $db->getTotalNum($sql,$param_data);

$now_page  =  $_GET['now_page'];
$page_num  = 20;
$finish_page = ceil($total_num/$page_num);

if ($now_page == "") {
    $page = new PageUtil($total_num, $page_num, 1);
}else {
    $page = new PageUtil($total_num, $page_num, $now_page);
}
//データの取得
$list_data = $db->getListData($page->disp_begin, $page->disp_end,$param_data);
传参:
$smarty->assign('page',         $page);
$smarty->assign('finish_page',  $finish_page);
$smarty->assign('now_page',     $now_page);
$smarty->assign('list_data',    $list_data);
$smarty->assign('total_num',    $total_num);

smarty中文件包含:
样式一:
{if $page->prev_link}|&nbsp;<a href="JavaScript:goPage('{math equation="x - 1" x=$page->now_page}');">前へ</a>&nbsp;|{/if}
  {section name=cnt loop=$page->total_page}
    {if $page->now_page==$smarty.section.cnt.iteration}
      <font color=red><b>{$page->now_page}</b></font>
    {else}
      {if $smarty.section.cnt.iteration <= $page->now_page + 4 && $smarty.section.cnt.iteration >= $page->now_page - 4}
        <a href="javascript:goPage('{$smarty.section.cnt.iteration}');">{$smarty.section.cnt.iteration}</a>
      {/if}
    {/if}
  {/section}
{if $page->next_link}
   |&nbsp;<a href="JavaScript:goPage('{math equation="x + 1" x=$page->now_page}');">次へ</a>&nbsp;|
{/if}

样式二:
<ul class="pagenate">
{if $page->prev_link}
<li class="nextback"><a href="JavaScript:goPage('{math equation="x - 1" x=$page->now_page}');">< 前のページへ</a></li>
{/if}
{section name=cnt loop=$page->total_page}
{if $page->now_page==$smarty.section.cnt.iteration}
    <li><font color=red><b>{$page->now_page}</b></font></li>
{else}
    {if $smarty.section.cnt.iteration <= $page->now_page + 4 && $smarty.section.cnt.iteration >= $page->now_page - 4}
        <li><a href="javascript:goPage('{$smarty.section.cnt.iteration}');">{$smarty.section.cnt.iteration}</a></li>
     {/if}
{/if}
{/section}
{if $page->next_link}
<li class="nextback"><a href="JavaScript:goPage('{math equation="x + 1" x=$page->now_page}');">次のページへ ></a></li>
{/if}
</ul>

精彩图集

赞助商链接