- <?php
- //数据库连接
- $my_db = mysql_connect("localhost","root","");
- mysql_select_db("model", $my_db);
- mysql_query("set names 'utf8'");
- $page = isset($_GET['page'])?intval($_GET['page']):0;//第几页值
- $shownum = 2;//每页显示数量
- $limit = $page * $shownum;//查询开始数
- $sql = "select * from web_news order by id desc limit $limit,$shownum";
- $query = mysql_query($sql);
- $rows = array();
- while($row = mysql_fetch_assoc($query))
- {
- $rows[] = $row;
- }
- $sql_count = "select * from web_news";
- $total = mysql_num_rows(mysql_query($sql_count));//总数量
- $pagenum = ceil($total/$shownum);//总共页数
- $pages = '';
- $pages .='总共'.$total.'信息'.' 共'.$pagenum.'页,每页'.$shownum.'条信息';
- $pages .='<a href="page.php?page=0">'.'首页'.'</a>';
- if($page <= 0){
- $pages .='<a href="page.php?page='.$page.'">'.'上一页'.'</a>';
- }else{
- $pages .='<a href="page.php?page='.($page-1).'">'.'上一页'.'</a>';
- }
- for($i=0;$i<$pagenum;$i++){
- if($page == $i){
- $pages .='<a class="hover">'.($i+1).'</a>';
- }else{
- $pages .='<a href="page.php?page='.$i.'">'.($i+1).'</a>';
- }
- }
- if($page >= ($pagenum-1)){
- $pages .='<a href="page.php?page='.($pagenum-1).'">'.'下一页'.'</a>';
- }else{
- $pages .='<a href="page.php?page='.($page+1).'">'.'下一页'.'</a>';
- }
- $pages .='<a href="page.php?page='.($pagenum-1).'">'.'末页'.'</a>';
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml"><head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>分页</title>
- </head>
- <style>
- .pages a{ padding:5px 8px 5px 8px; border:#999999 1px solid; margin-left:5px;}
- .pages a.hover{ color:#FF0000;}
- </style>
- <body>
- <div>
- <ul>
- <?php foreach($rows as $val){?>
- <li><?php echo $val['title'];?></li>
- <?php }?>
- </ul>
- <div class="pages"><?php echo $pages;?></div>
- </div>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/1108201513401.html
来源: http://www.codesnippet.cn/detail/1108201513401.html