列表分页(PHP)

来源:互联网 发布:node co模块 阮一峰 编辑:程序博客网 时间:2024/05/17 04:01

当网页显示的列表为数据库查询的数据时,总是会有很多内容,需要用到分页。利用bootstrap的样式写一个简单的页数显示效果。如图所示表示当前是第二页

<!DOCTYPE html>  <html xmlns="http://www.w3.org/1999/xhtml">  <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />  <link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">  <script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>  <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>  </head>  <?php  ///////////  //数据库连接  ///////////  //假设取出list表中所有数据  $perNumber = 15; // 每页显示的记录数  $page = $_GET ['page']; // 获得当前的页面值  $count = mysql_query ( "select count(*) from `list` "); // 获得记录总数    $rs = mysql_fetch_array ( $count );  $totalNumber = $rs [0];  $totalPage = ceil ( $totalNumber / $perNumber ); // 计算出总页数  if (! isset ( $page )) {      $page = 1;  } // 如果没有值,则赋值1  $startCount = ($page - 1) * $perNumber; // 分页开始,根据此方法计算出开始的记录    // 根据前面的计算出开始的记录和记录数  $sqq="select * from `list` limit $startCount,$perNumber";  $res=mysql_query($sqq);  $num=mysql_num_rows($res);    ?>  <body>  <div class="panel panel-default">      <div class="panel-body">          <table class="table table-hover">              <thead>                  <tr>                      <th>a</th>                      <th>b</th>                      <th>c</th>                      <th>d</th>                      <th>e</th>                      <th>f</th>                  </tr>              </thead>              <?php if($num>0){?>              <tbody>              <?php  while($row=mysql_fetch_array ( $res ) ){?>                  <tr>                      <td><?php echo $row['a'];?></td>                      <td><?php echo $row['b'];?></td>                      <td><?php echo $row['c'];?></td>                      <td><?php echo $row['d'];?></td>                      <td><?php echo $row['e'];?></td>                      <td><?php echo $row['f'];?></td>                      <!--<td><?php if($row['dingdan_status']==1){?><span class="label label-success">已付</span><?php }else{?><span class="label label-warning">未付</span><?php }?></td>-->                  </tr>              <?php }?>              <?php }else{?>              <tr><td colspan="6" class="text-center">暂无记录</td></tr>              <?php }?>              </tbody>          </table>          <ul class="pagination">          <?php          if ($page != 1) { //页数不等于1          ?>              <li><a href="List.php?page=<?php echo $page - 1;?>">«</a></li>          <?php          }          for ($i=1;$i<=$totalPage;$i++) {  //循环显示出页面          ?>              <li><a href="List.php?page=<?php echo $i;?>"><?php echo $i ;?></a></li>          <?php          }          if ($page<$totalPage) { //如果page小于总页数,显示下一页链接          ?>              <li><a href="List.php?page=<?php echo $page + 1;?>">»</a></li>          <?php          }           mysql_close( $link );          ?>          </ul>      </div>  </div>  <script>  $(function(){//$(".pagination a:contains('<?php echo $page;?>')").css("color", "red");//默认当前页数红色显示(如果当前页数为1,则10、11等也显示红色)var i=<?php echo $page;?>;if(i==1){$(".pagination a:eq(0)").css("color", "red");}else{$(".pagination a:eq(<?php echo $page;?>)").css("color", "red");}});</script>  </body>  </html>




0 0