• PHP


    效果1:

    使用代码(page.class.php-分页代码,index.php-页面显示代码,page.css-css样式)

    page.class.php:

    <?php
        class Page {
        	private $total;    		//总记录
        	private $pagesize;  	//每页显示多少条
        	private $limit;   		//limit
        	private $page;   		//当前页码
        	private $pagenum; 		//总页码
        	private $url;    		//地址
        	private $bothnum; 		//两边保持数字分页的量
            private $orderby;       //排序字段,含正序或是倒序: CreateTime desc / CreateTime asc
          
          	//构造方法初始化
          	public function __construct($_total, $_pagesize, $_orderby, $_url) {
           		$this->total = $_total ? $_total : 1;
           		$this->pagesize = $_pagesize;
          		$this->pagenum = ceil($this->total / $this->pagesize);
           		$this->page = $this->setPage();
           		$this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";
           		$this->url = $_url;
           		$this->bothnum = 3;
                    $this->orderby = ' order by '.$_orderby;
          	}
    
          	//拦截器
          	private function __get($_key) {
          	    //如果所取属性名称部位limit则直接返回空
          	    if ($_key == 'limit') {
           		   return $this->$_key;
          	    }
    
                //排序字段
                if ($_key == 'orderby') {
                    return $this->$_key;
                }
          	}
          
          	//获取当前页码
          	private function setPage() {
           		if (!empty($_GET['page'])) {
            			if ($_GET['page'] > 0) {
             				if ($_GET['page'] > $this->pagenum) {
              					return $this->pagenum;
             				} else {
              					return $_GET['page'];
             				}
            			} else {
             				return 1;
            			}
           		} else {
            			return 1;
           		}
          	} 
          
        	//数字目录
          	private function pageList() {
           		for ($i=$this->bothnum;$i>=1;$i--) {
            		$_page = $this->page-$i;
            		if ($_page < 1) continue;
            			$_pagelist .= '<a href="'.$this->url.'?page='.$_page.'">'.$_page.'</a> ';
           		}
           		$_pagelist .= '<span class="current">'.$this->page.'</span> ';
           		for ($i=1;$i<=$this->bothnum;$i++) {
           	 		$_page = $this->page+$i;
            			if ($_page > $this->pagenum) break;
            			$_pagelist .= ' <a href="'.$this->url.'?page='.$_page.'">'.$_page.'</a> ';
           		}
           		return $_pagelist;
          	}
          
          	//首页
          	private function index()
          	{
                if ($this->page == 1) {
                    return '<span class="disabled">首页</span>';
           		} else {
           		    return '<a href="'.$this->url.'">首页</a>';
           		}
          	}
          	
          	//第一页
          	private function first() {
           		if ($this->page > $this->bothnum+1) {
            			return '<a href="'.$this->url.'">1</a><span>  ... </span>';
           		}
          	}
          
          	//上一页
          	private function prev() {
           		if ($this->page == 1) {
            			return '<span class="disabled">上一页</span>';
           		}
           		return '<a href="'.$this->url.'?page='.($this->page-1).'">上一页</a>';
          	}
          
          	//下一页
          	private function next() {
           		if ($this->page == $this->pagenum) {
            			return '<span class="disabled">下一页</span>';
           		}
           		return '<a href="'.$this->url.'?page='.($this->page+1).'">下一页</a>';
          	}
          
          	//最后一页
          	private function last() {
           		if ($this->pagenum - $this->page > $this->bothnum) {
            			return '<span>  ...  </span><a href="'.$this->url.'?page='.$this->pagenum.'">'.$this->pagenum.'</a> ';
           		}
          	}
          	
          	//尾页
          	private function end()
          	{
          	    if ($this->page == $this->pagenum) {
          	        return ' <span class="disabled">尾页</span>';
          	    } else {
          	        return ' <a href="'.$this->url.'?page='.$this->pagenum.'">尾页</a>';
          	    }
          	}
          
          	//分页信息
          	public function showpage() {
            $_page .= $this->index();
            $_page .= $this->prev();
       		$_page .= $this->first();
       		$_page .= $this->pageList();
       		$_page .= $this->last();
       		$_page .= $this->next();
       		$_page .= $this->end();
       		return $_page;
      	}
        }
     
    	#调用方法
    	#$_page = new Page($_total,$_pagesize); //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量.  
    ?>
    

    index.php:

    <?php
    	//引入数据库连接文件
    	require('conn.php');
    	//引入分页类
    	require('page.class.php');
    	
    	//每页显示页数
    	$_pagesize =1;
           //url
           $_url = 'News_List.php';
            //排序字段
            $_orderby = 'CreateTime desc';
    	//获取总记录数
    	$_datanum = mysql_num_rows(mysql_query("SELECT id FROM `tb_article`"));
    	//分页对象
    	$p = new Page($_datanum,$_pagesize,$_orderby,$_url);
    	//获取微博数据资源句柄
    	$queryhandle = @mysql_query("SELECT * FROM `tb_article` ".$p->orderby.$p->limit) or die('SQL执行失败!');
    ?>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>添加微博</title>
    		<link href="css/page.css" rel="stylesheet" type="text/css" />
    	</head>
    	<body>
            <hr/>
    		  <a href="add.php">添加微博</a>
    		<hr/>
    		
    		<!-- 循环显示数据库内容 -->
    		<?php while(!!$result = mysql_fetch_array($queryhandle,MYSQL_ASSOC)) { ?>
    			<h3>标题:<a href = "disinfo.php?id=<?php echo $result['id'] ?>"><?php echo $result['title'] ?></a>    
    			|  <a href = "update.php?id=<?php echo $result['id'] ?>"> 编辑 </a> 
    			| <a href = "delete.php?id=<?php echo $result['id'] ?>"> 删除 </a> |</h3>
    			<h3>时间:<?php echo $result['datetime'] ?> </h3>
    			<h3>点击量:<?php echo $result['click'] ?> </h3>
    			<p><?php echo $result['content'] ?> </p>
    			<hr/>
    		<?php } ?>
    		
    		<!-- 分页(包含此div,否则无法应用样式) -->
    		<div id="page">
    		  <?php echo $p->showpage(); ?>
    		</div>
    	</body>
    </html>
    

    page.css:

    @CHARSET "UTF-8";
    #page {
        FONT-SIZE: 13px;
        FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;
        MARGIN: 3px;
        TEXT-ALIGN: center;
        PADDING:3px;
    }
    #page A {
        PADDING:3PX 7PX;
        MARGIN: 2px; 
        COLOR: #333333; 
        TEXT-DECORATION: none;
        BORDER:#DBE5EE 1px solid;
    }
    #page A:hover {
        COLOR:#fff;
        BACKGROUND:#07519A;
        BORDER:#07519A 1px solid;
    }
    #page A:active { 
        COLOR: #fff;
        BORDER:#07519A 1px solid;
    }
    #page SPAN.current {
        FONT-WEIGHT: bold; 
        MARGIN: 2px;  
        COLOR: #fff; 
        BACKGROUND-COLOR: #07519A;
        BORDER:#07519A 1px solid; 
        PADDING:2px 5px;
    }
    #page SPAN.disabled { 
        MARGIN: 2px; 
        COLOR: #CDC9C9; 
        BORDER: #eee 1px solid;
        PADDING:2PX 5PX;
    }
    

    效果2:

     

    代码:

    html:

    <!-- 分页 -->
    <div id="page">
      <ul>
          <li>第<?php echo $_page; ?>/<?php echo $_pagenum; ?>页</li>
          <li>共有<?php echo $_datanum; ?>页</li>
          <?php 
              if ($_page == 1) {
                  //如果是第一页,则首页和第一页不可用
                  echo "<li>首页</li>";
                  echo "<li>第一页</li>";
              } else {
                  //如果不是第一页,则可用
                  echo "<li><a href='index.php'>首页</a></li>";
                  echo "<a href='index.php?page=".($_page - 1)."'><li>上一页</a></li>";
              }
              
              if ($_page == $_pagenum) {
                  //如果是最后也,则下一页与最后一页不可用。
                  echo "<li>下一页</li>";
                  echo "<li>尾页</li>";
              } else {
                  //如果不是,则可用
                  echo "<a href='index.php?page=".($_page + 1)."'><li>下一页</a></li>";
                  echo "<li><a href='index.php?page=".$_pagenum."'>尾页</a></li>";
              }
          ?>
      </ul>
    </div>
    

    php:

    <?php
    	//引入数据库连接文件
    	require('conn.php');
    	
    	//分页
    	//接收页数
    	$_page = $_GET['page'];
    	
    	//判断是否为第一页
    	if (!isset($_GET['page'])) {
    	    $_page = 1;
    	}
    	
    	//每页显示页数
    	$_pagesize = 3;
    	
    	//起始页数
    	$_startpage = ($_page - 1) * $_pagesize;
    	
    	//获取微博数据资源句柄
    	$queryhandle = @mysql_query("SELECT * FROM `tb_article` LIMIT ".$_startpage.','.$_pagesize) or die('SQL执行失败!');
    	
    	//获取总记录数
        $_datanum = mysql_num_rows(mysql_query("SELECT id FROM `tb_article`"));
        
        //计算总页数
        $_pagenum = ceil($_datanum / $_pagesize);
    ?>
    

    css:

    @CHARSET "UTF-8";
    #page ul{
    	680px; 
    	height:40px; 
    	margin:0 auto;
    }
    #page ul li{
    	list-style-type:none; 
    	float:left; 
    	100px; 
    	margin:0 5px; 
    	height:25px; 
    	line-height:25px; 
    	dispaly:inline-block; 
    	text-align:center; 
    	border:1px solid #000;
    }
    
  • 相关阅读:
    创建索引锁的表
    安装STS报错(三)
    安装STS报错(二)
    安装STS报错(一)
    Linux Top使用说明
    平实给力的写作指导入门手册——leo鉴书57
    Flex中配置FusionCharts
    Java Web项目中缺少Java EE 6 Libraries怎么添加
    Java Web项目结构
    MyEclipse中修改项目运行地址栏中项目名称
  • 原文地址:https://www.cnblogs.com/KTblog/p/5094028.html
Copyright © 2020-2023  润新知