程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php mysql分頁實現代碼

php mysql分頁實現代碼

編輯:關於PHP編程

這算是一款比較完美的分頁類了,支持表單輸入跳轉,也支持select跳轉分頁,同時還支持上10頁,下10頁偏移分頁哦。 */

這算是一款比較完美的分頁類了,支持表單輸入跳轉,也支持select跳轉分頁,同時還支持上10頁,下10頁偏移分頁哦。
*/

class wind_page 
{
    var $page;
      //現在所在頁
    var $total;
      //記錄總條數
    var $totalpage;
     //總頁樹
    var $pagesize;
      //每頁顯示條數
    var $offset;
      //偏移量
    var $result;
      //記錄集合
    var $thispage;
      //記錄總條數 //記錄總條數 //記錄總條數
    var $link;
      //連接(記錄$_get變量)
    var $bar_mun;
      //bar顯示的頁數
    var $starttime;
      //開始時間
    var $bar_last;
      //導航條的持續頁數
    var $bar_mid;
      //導航條中間頁
    var $minpage;
      //導航條最小頁
    var $maxpage;
      //導航條最大頁
    var $key;
      //標識分頁(一個頁面多個分頁時用與區分)
    var $style;
      //輸出的風格
    var $pre_page_char;
      //上一頁
    var $pre_page_image;
      //上一頁圖形
    var $pre_page_char_color;
      //上一頁字體顏色
    var $next_page_char;
      //下一頁
    var $next_page_image;
      //下一頁圖形
    var $next_page_char_color;
      //下一頁字體顏色
    var $pre_groud_char;
      //上翻中間頁數
    var $pre_groud_char_color;
      //上翻中間頁數字體顏色
    var $next_groud_char;
      //下翻中間頁數
    var $next_groud_char_color;
    //下翻中間頁數字體顏色
    var $first_page_char;
    //首頁
    var $first_page_char_color;
    //首頁字體顏色
    var $last_page_char;
    //尾頁
    var $last_page_char_color;
    //尾頁字體顏色
    var $html_page_val;
    //html當前的頁碼值
    
    function wind_page($sql, $pagesize = 20, $bar_mun = 10, $style = 1,$this_page = 1,$html_page_val=1,$key = "") //構造函數()
    {
        $this->starttime = microtime();
        $this->pagesize = $pagesize;
          //每頁顯示條數
        $this->key = trim($key);
        $this->bar_mun = $bar_mun;
        $this->style = $style;
        $this->html_page_val = $html_page_val;
        //html當前的頁碼值
        $this->bar_last = $bar_mun-1;
        $this->bar_mid = floor($bar_mun/2);
        $this->sql = $sql;
        $result = mysql教程_query($this->sql);
        $this->total = mysql_num_rows($result);
        //記錄總條數
        $this->totalpage = ceil($this->total/$this->pagesize);
        //總頁數
        //$this->page = ceil($_get[$this->key."page"]);
        $this->page = $this_page;
        //現在所在頁
        if ($this->page == "" || $this->page < 1 || !is_numeric($this->page))$this->page = 1;
        $this->page = min($this->page, $this->totalpage);
        $this->thispage = $this->pagesize;
        if ($this->page * $this->pagesize > $this->total) {
            $this->thispage = $this->total-($this->page-1) * $this->pagesize;
        };
        $this->sql .= " limit ".($this->pagesize * ($this->page-1)).", ".$this->pagesize;
          //開始讀取的條數
       //echo "<br>";
        $this->result = mysql_query($this->sql);
        $this->getvar();
       
        $this->pre_page_char = "上一頁";
        $this->pre_page_image = "<img src="/images/b_s_prev.gif" border="0" />";
        $this->next_page_char = "下一頁";
        $this->next_page_image = "<img src="/images/b_s_next.gif" border="0" />";
        $this->pre_groud_char = "上一組";
        $this->next_groud_char = "下一組";

    }
   
    function getvar() //取得除page外的其他get變量
    {
        $this->link = "";
        foreach($_get as $key => $vaule) {
            if (strtolower($key)  !== $this->key."page") $this->link  .= "&$key=$vaule";
        }
    }
    
    function pre_page($color="#909090",$sign=0) //上一頁
    {
        if ($this->page > 1) { 
            return "<a href="index_".sprintf("%02d",$this->page-1).".html" class="ab"><font color="".$this->pre_page_char_color."">".$this->pre_page_char."</font></a>";
        } else
        {
           return "<font color="$color">".$this->pre_page_char."</font>";
        }
      
    }
    
    function next_page($color="#909090",$sign=0) //下一頁
    {
        if ($this->page < $this->totalpage) {
          
              return "<a href="index_".sprintf("%02d",$this->page+1).".html" class="ab">".$this->next_page_char."</a>";
           
        } else {
          
             return "<font color="$color">".$this->next_page_char."</font>";
        }
    }
    
    function pre_groud($char = "<<", $color = "#909090") //上一組
    {
        if ($this->page <= ($this->bar_mid+1)) {
            return "<font color="".$color."">".$this->pre_groud_char."</font>";
            //return $this->pre_groud_char;
        } else {
            $pre_gpage = ($this->page-$this->bar_mid < 0)?1:
            $this->page-$this->bar_mid;
            return "<a href="index_".sprintf("%02d",$pre_gpage).".html" title="上一組">".$this->pre_groud_char."</a>";
        }
    }
    
    function next_groud($char = ">>", $color = "#909090") //下一組
    {
        if (($this->totalpage-$this->page) <= ($this->bar_mid-1)) {
            return "<font color="".$color."">".$this->next_groud_char."</font>";
        } else {
            $next_gpage = ($this->page+$this->bar_mid < $this->totalpage)?$this->page+$this->bar_mid:
            $this->totalpage;
            return "<a href="index_".sprintf("%02d",$next_gpage).".html"  title="下一組" >".$this->next_groud_char."</a>";
        }
    }
    
    function mun($lcolor = "#ff6633", $acolor = "#ff6633", $left = "&nbsp", $right = "&nbsp") //數字導航欄
    {
        $link = "";
        $this->minpage = ($this->page-$this->bar_mid < 1) ? 1:($this->page-$this->bar_mid);
        $this->maxpage = $this->minpage+$this->bar_last;
        if ($this->maxpage > $this->totalpage) {
            $this->maxpage = $this->totalpage;
            $this->minpage = ($this->maxpage-$this->bar_last < 1) ? 1: $this->maxpage-$this->bar_last;
        }
       for($i = $this->minpage; $i <= $this->maxpage; $i++)
        {
         /* 循環輸出頁碼 */
            $i = sprintf("%02d",$i);
            //不足兩位的前面補0
            $char = $left.$i.$right;
            //導航條左右兩邊加窄
            if ($i == $this->page)
            {
             /* 假如是當前頁則不加鏈接 */
                $link.= "<font color="".$acolor."">".$char."</font>";
            }
            else
             {
                //$link  .= "<a href="".$_server['php教程_self']."?".$this->key."page=".$i.$this->link."" >".$char."</a>";
                $link.= "<a href="index_".$i.".html" >".$char."</a>";
                //關鍵把$link 加在新資料加入之後
            }
        }
        echo "<br>";
        return $link;
    }
    
    
    function jump_bar($class = "jump_bar") //下拉跳轉
    {
        $link = "<select name="menu1" onchange="mm_jumpmenu('parent',this,0)" class="$class">";
        for($i = $this->minpage; $i <= $this->maxpage; $i++) {
            if ($i < 10)$i = "0".$i;
            //定義選擇不超過10個
            if($this->page == $i)
            {
             /* 假如為當前頁碼,則選中 */
             $link  .= "<option value="index_".$i.".html" selected>第".$i."頁</option>";
            }
            else
            {
              $link  .= "<option value="index_".$i.".html">第".$i."頁</option>";
            }
        }
        $link  .= "</select>";
        return $link;
    }
    
    function mun_bar() //整條數字導航欄  [<<][<][01][02][03][04][05][06][07][08][09][10][>][>>]
    {
        //return $this->first_groud().$this->pre_groud().$this->pre_page().$this->mun().$this->next_page().$this->next_groud().$this->last_groud();
        return $this->pre_groud()."&nbsp;".$this->pre_page().$this->mun().$this->next_page()."&nbsp;".$this->next_groud();
    }
    function page_button()
    { //整條數字導航欄  [<][01][02][03][04][05][06][07][08][09][10][>]
        //return $this->first_groud().$this->pre_groud().$this->pre_page().$this->mun().$this->next_page().$this->next_groud().$this->last_groud();
        return $this->pre_page('#909090',1).$this->mun().$this->next_page('#909090',1);
    }
    
    function total_bar($coloro = "#000000", $colorn = "red") //統計數字  頁次:1/4310 每頁:20 共計:4310頁 本頁:20
    {
        return "<font color=$coloro>頁次:<font color=$colorn>$this->page</font>/$this->totalpage 每頁:<font color=$colorn>$this->pagesize</font> 共計:<font color=$colorn>$this->totalpage</font>頁  本頁:<font color=$colorn>$this->thispage</font></font>";
    }
    
    // 整條導航欄
    //頁次:1/4310 每頁:20 共計:4310頁 本頁:20 [<<][<][01][02][03][04][05][06][07][08][09][10][>][>>]
    function page_bar($coloro = "#000000", $colorn = "red") {
        return "<table width='100%'  border='0' cellspacing='0'>
            <tr>
            <td width='5%'></td>
            <td width='35%'>".$this->total_bar()."</td>
            <td width='40%'align='right'>".$this->mun_bar()."</td>
            <td width='10%'>".$this->jump_bar()."</td>
            <td width='5%'></td>
            </tr>
            </table>".$this->mm_jumpmenu();
    }
    
    function taketime($color = "#000000") //計算執行時間
    {
        return "<div align='center'><font color=$color>本頁執行時間".abs((microtime()-$this->starttime) * 1000)."毫秒</font></div>";
    }
    
    function style() //輸出分頁的樣式
    {
        $style_num = $this->style;
        
        if ($this->totalpage  != 0) //如果總頁數=0 ,表示無分頁
        {
            switch($style_num) {
                case 1:
                return $this->page_bar();
                //頁次:1/4310 每頁:20 共計:4310頁 本頁:20 [<<][<][01][02][03][04][05][06][07][08][09][10][>][>>]
                break;
                case 2:
                echo $this->mun_bar();
                //整條數字導航欄  [<<][<][01][02][03][04][05][06][07][08][09][10][>][>>]
                break;
                case 3:
                echo $this->page_button();
                //整條數字導航欄  [<][01][02][03][04][05][06][07][08][09][10][>]
                break;
            }
        }
    }
    
    function mm_jumpmenu() //網頁特效的跳轉
    {
        //window.open(selobj.options[selobj.selectedindex].value,targ);
        return "<script language="javascript">
            <!--
            function mm_jumpmenu(targ,selobj,restore){
            if (selobj.selectedindex==0) return;
            window.location.href=selobj.options[selobj.selectedindex].value,targ;
            if (restore) selobj.selectedindex=0;
            }
            //-->
            </script>";
    }
    
    function first_groud($char = "&nbsp;<<", $color = "#000000") {
        if ($this->page == 1) {
            return "<font color="".$color."">".$char."</font>";
        } else {
            //$pre_gpage=($this->page-$this->bar_mid<0)?1:$this->page-$this->bar_mid;
            $pre_gpage = 1;
            return "<a href="".$_server['php_self']."?".$this->key."page=".$pre_gpage.$this->link."" title="上一組"><font color="".$color."">".$char."</font></a>";
        }
    }
    
    function last_groud($char = "&nbsp;>>", $color = "#000000") {
        if ($this->page == $this->totalpage) {
            return "<font color="".$color."">".$char."</font>";
        } else {
            //$pre_gpage=($this->page-$this->bar_mid<0)?1:$this->page-$this->bar_mid;
            $pre_gpage = $this->totalpage;
            return "<a href="".$_server['php_self']."?".$this->key."page=".$pre_gpage.$this->link."" title="上一組"><font color="".$color."">".$char."</font></a>";
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved