Blogger Information
Blog 34
fans 0
comment 0
visits 26568
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分页封装
罗盼的博客
Original
780 people have browsed it

实例

<?php
/*分页*/
class Page
{
    public $data;//数据库取出数据
    public $totalpage;//总页数
    public $pages;//总页数
    public $currentPage;//当前页数
    private $pdo;//当前页数
    private $offset;//偏移量
    public function __construct() {
    //连接数据库
     try
    {
       $this->pdo = new PDO('mysql:host=127.0.0.1;dbname=test','root','root');     
    }
    catch(PDOException $e)
    {
        die($e->getMessage());
    }
    
    //计算数据总条数$total
    $sql = " SELECT COUNT(*) FROM `userinfo`  "; 
    $stmt=  $this->pdo->prepare($sql);    
    if($stmt->execute()){
      $result = $stmt->fetchColumn(); 
      $this->totalpage  =ceil($result/5);     
    }
    }


public function munbers(){
    
        //当前页获取$currentPage
    $this->currentPage=$_GET['a']?$_GET['a']:1;
   //计算分页数量$pages
    $this->offset = ($this->currentPage-1)*5;
     //取出数据并处理 
    $sql = " SELECT * FROM `userinfo` LIMIT {$this->offset},5 "; 
    $stmt=  $this->pdo->prepare($sql);    
    if($stmt->execute()){
      $this->data = $stmt->fetchAll(PDO::FETCH_ASSOC); 
           
    }

}


}
?>
 <table width=30%  align="center" cellpadding="5px" border="1px soilds" cellspacing="0" >
    <caption style="margin-left:20px;margin-bottom:20px;font-size:1.5em;">用户信息</caption>
    <tr style="text-align:center;background-color: lightblue"> <td>用户ID</td> <td>用户名</td> <td>用户级别</td>
    
    <?php
    if(!$_GET){
       $_GET['a']='';
    }
     $pages= new Page;
     $pages->munbers();
     foreach($pages->data as $v){
      echo '<tr style="text-align:center"> <td>'.$v['id'].'</td> <td>'.$v['account'].'</td> <td>'.$v['head'].'</td>';          
    } 
    ?> 
    </table>  
    
<ul>
<li><a href="http://mybloger.com?a=1" >首页</a></li>
<li><a href="http://mybloger.com?a=<?php echo $pages->currentPage-1<1?1:$pages->currentPage-1 ?>" >上一页</a></li>

<?php for($i=1;$i<=$pages->totalpage;$i++){?>
<li><a class="<?php echo ($i==$pages->currentPage)?'act':'' ?>"  href="http://mybloger.com?a=<?php echo $i ?>" ><?php echo $i ?></a></li><?php } ?>

<li><a href="http://mybloger.com?a=<?php echo $pages->currentPage+1>$pages->totalpage?$pages->totalpage:$pages->currentPage+1 ?>" >下一页</a></li>
<li><a href="http://mybloger.com?a=<?php echo $pages->totalpage ?>" >尾页</a></li>
<li>
<form enctype="text/plain" action=" " method="get">
<select name="a" id="" >
<?php for($i=1;$i<=$pages->totalpage;$i++){?>
    <option value="<?php echo $i ?>" > <?php echo $i ?> </option>
<?php }?>
</select>
<button>跳转</button>
</form>
</li>
</ul>

<style>
ul{ 
    text-align: center;
    margin-top: 30px;
}

li{
  display: inline-block; 
   margin: 0 5px;
  
}

a{
    border:1px solid #000;
    padding: 2px 5px;
    text-decoration: none;
    color: black;
    font-weight:bold;
}
a.act{
   background: red; 
   color:white;
}
</style>

运行实例 »

点击 "运行实例" 按钮查看在线实例

效果

1e33d775b3d8d31c21f61780bd96861.png

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments