php实现分页调取数据库记录

<html>
      <head>
      <title>学生信息管理</title>
      <link rel="stylesheet" type="text/css" href="bujv.css">
      <meta http-equiv="Content-Type" content="text/html; charset=gbk;"> 
      <script>
            function doDel(id){
                 if(confirm("确认要删除吗?")){
                        window.location="action.php?action=del&id="+id;
                     }
                }
      </script>
      <style type="text/css">
          img {border:0;}
          .tiao{width:30px;}
      </style>
      </head>
      <body> 
        <center>
         <?php include 'menu.php';
           session_start();
           这里用来判断输入的跳转页数是否能超范围 由于这些语句不能放到下边,故此借助session来完成
           if(isset($_POST['submit']))
           {
             $temp= @$_POST['pagenum'];
           //  var_dump($_SESSION['pages']);
             if($temp>=1&&$temp<=$_SESSION['pages'])
               echo "<script> window.location.href='index.php?page=".$temp."'</script>";
             else 
                 echo "<script> alert('输入的页码不存在');window.history.back();</script>";
           }
         ?>
 
         <table border="1">
         <tr>
               <th width="auto">ID</th>
               <th width="auto">姓名</th>
               <th width="auto">性别</th>
               <th width="auto">邮箱</th>
               <th width="auto">电话</th>
               <th width="100">联系地址</th>
               <th width="300">简介</th>
               <th width="150">操作</th>
         </tr>
         <?php 
         
         //1.连接数据库
         $dbhost='localhost';//数据库服务器名称
         $dbuser='root';// 连接数据库用户名
         $dbpass='';// 连接数据库密码
         $dbname='online';// 数据库的名字
         // 连接到数据库
         error_reporting(E_ALL ^ E_DEPRECATED);//解决报错问题
         $connect=mysql_connect($dbhost,$dbuser,$dbpass);
         if(!$connect) exit('数据库连接失败!');
         mysql_select_db($dbname,$connect);
         mysql_query('set names gbk');//设置编码
         
         //设置   
         $page=1;
         $pagesize=2;
         
         //计算一共多少记录,用于计算页数
         $rs = mysql_query("select count(*) from xinxi",$connect);
         $row = @mysql_fetch_array($rs);
         $numrows = $row[0];
         
         //计算页数
         $pages = intval($numrows / $pagesize);//求得整页
         if ($numrows % $pagesize)   //余下的按一页来算
         {
             $pages++;
         }
         //留存总页数
             $_SESSION['pages']=$pages;
         //留存总页数
         
         //设置页数
         if (isset($_GET['page']))  //获取地址传来的页数
         {
             $page = intval($_GET['page']);
         }
         else
         {
             $page = 1;        //其他情况,都指向第一页
         }
         //计算记录的偏移量
         $offset = $pagesize * ($page - 1);
         
         //读取指定记录
         $result = mysql_query("select * from xinxi order by id limit $offset,$pagesize",$connect);
          
         
         //2.执行sql语句
        // $sql="SELECT * FROM `xinxi`";
        // $result=mysql_query($sql);
         while($row=mysql_fetch_array($result)){
             echo "<tr>";
             echo "<td>{$row['id']}</td>";
             echo "<td>{$row['name']}</td>";
             echo "<td>{$row['sex']}</td>";
             echo "<td>{$row['mail']}</td>";
             echo "<td>{$row['tel']}</td>";
             echo "<td>{$row['adress']}</td>";
             echo "<td>{$row['introduce']}</td>";
             echo "<td>
                <a href='javascript:doDel({$row['id']})'><img style='margin-top:1px;' src='2.png' title='修改'>删除</a>
                <a href='edit.php?id={$row['id']}'><img style='margin-top:2px; margin-bottom:0px;' src='1.png' title='修改'>修改</a>
             </td>";
             echo "</tr>";
         }
         //只有首页,上一页,下一页,末页
             error_reporting(0);
             echo "<div align='center'> 每页 ".$pagesize." 调记录  共 ".$pages." 页(".$page."/".$pages.")";
             if($page==1)//处于首页的话
             {   echo "首页";
                 echo " ";
                 echo "上一页";
                 echo " ";
                 $tempx=$page+1;
                 echo "<a href='index.php?page=".$tempx." '>下一页</a>";
                 echo " ";
                 echo "<a href='index.php?page=".$pages."'>末页</a>";
             }
             else if($page==$pages){//处于末页的话
                 echo "<a href='index.php?page=1'>首页</a>";
                 echo " ";
                 $temps=$page-1;
                 echo "<a href='index.php?page=".$temps."'>上一页</a>";
                 echo " ";
                 echo "下一页";
                 echo " ";
                 echo "末页";
             }
             else {
                 echo "<a href='index.php?page='1''>首页</a>";
                 echo " ";
                 $temps=$page-1;
                 echo "<a href='index.php?page=".$temps."'>上一页</a>";
                 echo " ";
                 $tempx=$page+1;
                 echo "<a href='index.php?page=".$tempx." '>下一页</a>";
                 echo " ";
                 echo "<a href='index.php?page=".$pages."'>末页</a>";
             }
                echo "<form method='post'>跳转到<input class='tiao'type='text' name='pagenum'>页   <input type='submit' value='跳转' name='submit'></form>";   
         
         
         //点击页数进行选择的分页特点
//             echo "<div align='center'> 共".$pages."页(".$page."/".$pages.")";
//             for ($i = 1;$i < $page;$i++)
//             {
//              echo "<a href='index.php?page=".$i."'>[".$i."]</a>";
//              }
//              echo "[".$page."]";
//              for ($i = $page + 1;$i <= $pages;$i++)
//              {
//              echo "<a href='index.php?page=".$i."'>[".$i."]</a>";
//              }
          echo "</div>";
         ?>
         </table>
        </center>
        
           
      </body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值