PHP功能強大的分頁程序

webphp 12年前發布 | 1K 次閱讀 PHP

分頁類代碼 page.Class.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
/*
 -------------------------?----------------------
 /
class PageClass
{
    private $myde_count;    // 總記錄數
    var $myde_size;         // 每頁記錄數
    private $myde_page;     // 當前頁
    private $myde_page_count;   // 總頁數
    private $page_url;      // 頁面url
    private $page_i;        // 起始頁
    private $page_ub;       // 結束頁
    var $page_limit;

// 構造函數
function __construct($myde_count=0, $myde_size=1, $myde_page=1,$page_url)
{   

    $this -> myde_count = $this -> numeric($myde_count);
    $this -> myde_size  = $this -> numeric($myde_size);
    $this -> myde_page  = $this -> numeric($myde_page);
    $this -> page_limit = ($this -> myde_page * $this -> myde_size) - $this -> myde_size; 

    $this -> page_url       = $page_url;

    if($this -> myde_page < 1) $this -> myde_page =1;

    if($this -> myde_count < 0) $this -> myde_page =0;

    $this -> myde_page_count  = ceil($this -> myde_count/$this -> myde_size);

    if($this -> myde_page_count < 1) $this -> myde_page_count = 1;

    if($this -> myde_page > $this -> myde_page_count) $this -> myde_page = $this -> myde_page_count;

    $this -> page_i = $this -> myde_page - 2;

    $this -> page_ub = $this -> myde_page + 2;

    if($this -> page_i < 1){

        $this -> page_ub = $this -> page_ub + (1 - $this -> page_i);

        $this -> page_i = 1;
    }

    if($this -> page_ub > $this -> myde_page_count){

        $this -> page_i = $this -> page_i - ($this -> page_ub - $this -> myde_page_count);

        $this -> page_ub = $this -> myde_page_count;

        if($this -> page_i < 1) $this -> page_i = 1;
    }
}

// 判斷是否為數字
private function numeric($id)
{
    if (strlen($id)){
        if (!ereg("^[0-9]+$",$id)){
            $id = 1;
        }else{
            $id = substr($id,0,11);
        }
    }else{
        $id = 1;
    }
    return $id;
}

// 地址替換
private function page_replace($page) 
{
    return str_replace("{page}", $page, $this -> page_url);
}

// 首頁
private function myde_home() 
{
    if($this -> myde_page != 1){

        return "    <li class=\"page_a\"><a href=\"".$this -> page_replace(1)."\"  title=\"首頁\" >首頁</a></li>\n";

    }else{

        return "    <li>首頁</li>\n";

    }
}

private function myde_prev() // 上一頁
{
    if($this -> myde_page != 1){

        return "    <li class=\"page_a\"><a href=\"".$this -> page_replace($this->myde_page-1) ."\"  title=\"上一頁\" >上一頁</a></li>\n";

    }else{

        return "    <li>上一頁</li>\n";

    }
}

private function myde_next() // 下一頁
{
    if($this -> myde_page != $this -> myde_page_count){

            return "    <li class=\"page_a\"><a href=\"".$this -> page_replace($this->myde_page+1) ."\"  title=\"下一頁\" >下一頁</a></li>\n";

    }else{

        return "    <li>下一頁</li>\n";

    }
}

private function myde_last() // 尾頁
{
    if($this -> myde_page != $this -> myde_page_count){

            return "    <li class=\"page_a\"><a href=\"".$this -> page_replace($this -> myde_page_count)."\"  title=\"尾頁\" >尾頁</a></li>\n";

    }else{

        return "    <li>尾頁</li>\n";

    }
}

function myde_write($id='page') // 輸出
{
    $str  = "<div id=\"".$id."\" class=\"pages\">\n  vul>\n  ";

    $str .= "  <li>總記錄:<span>".$this -> myde_count."</span></li>\n";

    $str .= "    <li><span>".$this -> myde_page."</span>/<span>".$this -> myde_page_count."</span></li>\n";

    $str .= $this -> myde_home();

    $str .= $this -> myde_prev();

    for($page_for_i = $this -> page_i;$page_for_i <= $this -> page_ub; $page_for_i++){

        if($this -> myde_page == $page_for_i){

            $str .= "    <li class=\"on\">".$page_for_i."</li>\n";

        }

        else{

            $str .= "    <li class=\"page_a\"><a href=\"".$this -> page_replace($page_for_i)."\" title=\"第".$page_for_i."頁\">";

            $str .= $page_for_i . "</a></li>\n";

        }
    }
    $str .= $this -> myde_next();

    $str .= $this -> myde_last();

    $str .= "    <li class=\"pages_input\"><input type=\"text\" value=\"".$this -> myde_page."\"";

    $str .= " onkeydown=\"javascript: if(event.keyCode==13){ location='";

    $str .= $this -> page_replace("'+this.value+'")."';return false;}\"";

    $str .= " title=\"輸入您想要到達的頁碼\" /></li>\n";

    $str .= "  </ul>\n  <iv class=\"page_clear\"></div>\n</div>";

    return $str;
}

} /-------------------------實例-------------------------------- $page = new PageClass(1000,5,$_GET['page'],'?page={page}');//用于動態 $page = new PageClass(1000,5,$_GET['page'],'list-{page}.html');//用于靜態或者偽靜態 $page -> myde_write();//顯示 */ ?>

<?php

/* -------------------------數據庫操作類-----------------------------/ class mySql_Class { function __construct($host, $user, $pass) { @mysql_connect($host,$user,$pass) or die("數據庫連接失敗!"); mysql_query("SET NAMES 'utf-8'"); }

function select_db($db)//連接表
{
    return @mysql_select_db($db);
}

function query($sql)//執行SQL語句
{
    return @mysql_query($sql);
}

function fetch_array($fetch_array)
{
    return @mysql_fetch_array($fetch_array, MYSQL_ASSOC);
}

function num_rows($sql)
{
    return @mysql_num_rows($sql);
}

}

?> </pre>

前端頁面調用

<?php
include '../library/page.Class.php';
$page = $_GET['page'];
?>
  <?php

    include '../library/conn.php';


    $count = $mysql -> num_rows($mysql -> query("select *  from article where article_category = 'php' "));
    $pages = new PageClass($count,25,$_GET['page'],$_SERVER['PHP_SELF'].'?page={page}');
    $sql  = "select * from article where article_category = 'php' order by ";
    $sql .= "article_date desc limit ".$pages -> page_limit.",".$pages -> myde_size;
    $result = $mysql -> query($sql);
    ?>

<table class="table">

  <?php
  while($myrow = $mysql -> fetch_array($result)){
  ?>

<tr> <td width="30"><img src="../images/topicnew.gif" /></td>

<td width="90" style="font-family:Georgia"><? echo $myrow["article_date"] ?></td>
<td width="480"><a href="../<? echo $myrow["article_category"] ?>/<? echo $myrow["article_filename"] ?>"><? echo $myrow["article_title"] ?></a></td>

</tr> <?php } ?> </table> <?php echo $pages -> myde_write(); ?> </pre>

 本文由用戶 webphp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!