免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2298 | 回复: 3
打印 上一主题 下一主题

发布一个分页代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-12 09:10 |只看该作者 |倒序浏览
<?php
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//应用示范
require("config.inc.php");
require("pagenav.php");
$where="where title<>''";
$db = new DB_MySQL;
$db->connect();
$db->selectdb();
$nav = new buildNav;

    $total = $db->fetch_one_array("SELECT COUNT(*) AS count FROM news AS article $where");

    $nav->total_result = $total[count];

    if (empty($total[count])) {
        pa_exit("还没有任何文章");
    }

    $nav->execute("SELECT * FROM ".$table_start."_list AS article $where ORDER BY id DESC");

    echo $nav->pagenav();

    echo "<table boder=\"0\" width=\"100%\" cellpadding=\"4\" cellspacing=\"1\" class=\"tableoutline\">
               
                <td width=\"60%\"> 标题 </td>
               
               </tr>\n";
    while ($rs = $db->fetch_array($nav->sql_result)){
         
           echo "<tr>
                     
                      <td>$rs[title]</td>
                     
                     </tr>\n";

    }
    echo "</table>\n";

    echo $nav->pagenav();

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
error_reporting(7);

class buildNav {

      var $limit;
      var $execute,$query;
      var $total_result = 0;
      var $offset = "offset";


      function execute($query){

               global $db,$db_prefix;
               $GLOBALS[$this->offset] = (!isset($GLOBALS[$this->offset]) OR $GLOBALS[$this->offset]<0) ? 0 : $GLOBALS[$this->offset];
               //$this->sql_result = $db->query($query);

               $GLOBALS[$this->offset] = ($GLOBALS[$this->offset]>$this->total_result) ? $this->total_result-10 : $GLOBALS[$this->offset];

               if (empty($this->limit)) {
                   $this->limit = 20;
               }

               if (isset($this->limit)) {
                   $query .= " LIMIT " . $GLOBALS[$this->offset] . ", $this->limit";
                   $this->sql_result = $db->query($query);
                   $this->num_pages = ceil($this->total_result/$this->limit);
               }
               if ($GLOBALS[$this->offset]+1 > $this->total_result) {
                   $GLOBALS[$this->offset] = $this->total_result-1;
               }

      }


      function show_num_pages($frew = "&laquo;", $rew = '上一页', $ffwd = '&raquo;', $fwd = '下一页', $separator = '') {
               $current_pg = $GLOBALS[$this->offset]/$this->limit+1;
               if ($current_pg > '5') {
                   $fgp = ($current_pg-5 > 0) ? $current_pg-5 : 1;
                   $egp = $current_pg+4;
                   if ($egp > $this->num_pages) {
                       $egp = $this->num_pages;
                       $fgp = ($this->num_pages-9 > 0) ? $this->num_pages-9 : 1;
                   }
               } else {
                   $fgp = 1;
                   $egp = ($this->num_pages >= 10) ? 10 : $this->num_pages;
               }
               if($this->num_pages > 1) {
                  // searching for http_get_vars
                  foreach ($GLOBALS[HTTP_GET_VARS] as $_get_name => $_get_value) {
                           if ($_get_name != $this->offset) {
                               $this->_get_vars .= "&$_get_name=$_get_value";
                           }
                  }
                  $this->listNext = $GLOBALS[$this->offset] + $this->limit;
                  $this->listPrev = $GLOBALS[$this->offset] - $this->limit;
                  $this->theClass = $objClass;
                  if (!empty($rew)) {                                                                                                                                                                                                                                              //$separator [$frew] $rew
                      $return .= ($GLOBALS[$this->offset] > 0) ? "<a href=\"$GLOBALS[PHP_SELF]?$this->offset=0$this->_get_vars\" $this->theClass title=\"第一页\">$frew</a> <a href=\"$GLOBALS[PHP_SELF]?$this->offset=$this->listPrev$this->_get_vars\" $this->theClass title=\"上一页\">$rew</a> $separator " : "";
                  }

                  // showing pages
                  if ($this->show_pages_number || !isset($this->show_pages_number)) {
                      for($this->a = $fgp; $this->a <= $egp; $this->a++) {
                          $this->theNext = ($this->a-1)*$this->limit;
                          if ($this->theNext != $GLOBALS[$this->offset]) {
                              $return .= " <a href=\"$GLOBALS[PHP_SELF]?$this->offset=$this->theNext$this->_get_vars\" $this->theClass> ";
                              if ($this->number_type == 'alpha') {
                                  $return .= chr(64 + ($this->a));
                              } else {
                                  $return .= $this->a;
                              }
                              $return .= "</a> ";
                          } else {
                              if ($this->number_type == 'alpha') {
                                  $return .= chr(64 + ($this->a));
                              } else {
                                  $return .= "<b>$this->a</b>";
                              }
                              $return .= ($this->a < $this->num_pages) ? " $separator " : "";
                          }
                      }
                      $this->theNext = $GLOBALS[$this->offset] + $this->limit;
                      if (!empty($fwd)) {
                          $offset_end = ($this->num_pages-1)*$this->limit;                                                                                                                                                                                                                                                       //$separator $fwd [$ffwd]
                          $return .= ($GLOBALS[$this->offset] + $this->limit < $this->total_result) ? "$separator <a href=\"$GLOBALS[PHP_SELF]?$this->offset=$this->listNext$this->_get_vars\" $this->theClass title=\"下一页\">$fwd</a> <a href=\"$GLOBALS[PHP_SELF]?$this->offset=$offset_end$this->_get_vars\" $this->theClass title=\"最后一页\">$ffwd</a>" : "";
                      }
                  }
               }
               return $return;
      }

      // [Function : Showing the Information for the Offset]
      function show_info() {
               $return .= "共: ".$this->total_result." , ";
               $list_from = ($GLOBALS[$this->offset]+1 > $this->total_result) ? $this->total_result : $GLOBALS[$this->offset]+1;
               $list_to = ($GLOBALS[$this->offset]+$this->limit >= $this->total_result) ? $this->total_result : $GLOBALS[$this->offset]+$this->limit;
               //$return .= 'Showing Results from ' . $list_from . ' - ' . $list_to . '<br>';
               $return .= "显示: ".$list_from ." - ".$list_to;
               return $return;
      }

      function pagenav() {
               $return = "
                           <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"4\">
                             <tr>
                               <td>".$this->show_info()."</td>
                               <td align=\"right\">".$this->show_num_pages()."</td>
                             </tr>
                           </table>";

               return $return;
      }
}
?>

论坛徽章:
0
2 [报告]
发表于 2007-07-12 12:47 |只看该作者
比较一下吧:


  1. function page ($totalPage , $currentPage,$url ,$halfPer,$imagePath)
  2. {
  3.     $total=$totalPage;
  4.     $currentPage=($currentPage<1)?1:$currentPage;
  5.     $currentPage=($currentPage>$totalPage)?$totalPage:$currentPage;

  6.     //echo $currentPage;
  7.     $re="";
  8.     $re .= ( $currentPage > 1 )
  9.         ? "<td><a href=\"$url".($currentPage-1)."\">上一页</a></td>\n"
  10.         : "<td></td>\n";
  11.     for ( $i = $currentPage - $halfPer,$i >= 1 || $i = 1 ,$j = $currentPage + $halfPer, $j < $totalPage || $j = $totalPage;$i <= $j ;$i++ )
  12.     {
  13.         $re .= ($i == $currentPage)
  14.             ? "<td><b class=currentPage> $i </b></td>\n"
  15.             : "<td><a href=\"$url$i\" >$i</a></td>\n";
  16.     }
  17.     $re .= ( $currentPage < $total )
  18.         ? "<td><a href=\"$url" . ( $currentPage + 1 ) . "\">下一页</a></td>\n"
  19.         : "<td></td>\n";
  20.     $re="<table style=\"text-align:center\" align=\"center\"><tr><td>结果页码:</td>$re</tr></table>";
  21.     return $re;
  22. }

复制代码

论坛徽章:
0
3 [报告]
发表于 2007-07-12 19:25 |只看该作者
楼主,你的太复杂了吧~~~~~~~~~~

论坛徽章:
0
4 [报告]
发表于 2007-07-12 22:29 |只看该作者
太复杂了吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP