温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么实现从PostgreSQL数据库检索数据分页显示

发布时间:2021-05-27 16:33:02 来源:亿速云 阅读:128 作者:Leah 栏目:开发技术

使用PHP怎么实现从PostgreSQL数据库检索数据分页显示?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

主要功能是从postgreSql查询数据,并检索,由于自己刚开始接触,所以难点在于多条数据同时筛选并分页显示出来,写下自己的代码与大家共享。

<html>
<head>
<script type="text/javascript">
  /**
 * 分页函数
 * pno--页数
 * psize--每页显示记录数
 * 分页部分是从真实数据行开始,因而存在加减某个常数,以确定真正的记录数
 * 纯js分页实质是数据行全部加载,通过是否显示属性完成分页功能
 **/
function goPage(pno,psize){
  var itable = document.getElementById("idData");
  var num = itable.rows.length;//表格所有行数(所有记录数)
  console.log(num);
  var totalPage = 0;//总页数
  var pageSize = psize;//每页显示行数
  //总共分几页
  if(num/pageSize > parseInt(num/pageSize)){
      totalPage=parseInt(num/pageSize)+1;
    }else{
      totalPage=parseInt(num/pageSize);
    }
  var currentPage = pno;//当前页数
  var startRow = (currentPage - 1) * pageSize+1;//开始显示的行 31
    var endRow = currentPage * pageSize;//结束显示的行  40
    endRow = (endRow > num)? num : endRow;  40
    console.log(endRow);
    //遍历显示数据实现分页
  for(var i=1;i<(num+1);i++){
    var irow = itable.rows[i-1];
    if(i>=startRow && i<=endRow){
      irow.style.display = "block";
    }else{
      irow.style.display = "none";
    }
  }
  var pageEnd = document.getElementById("pageEnd");
  var tempStr = "共"+num+"条记录 分"+totalPage+"页 当前第"+currentPage+"页";
  if(currentPage>1){
    tempStr += "<a href=\"#\" onClick=\"goPage("+(1)+","+psize+")\">首页</a>";
    tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage-1)+","+psize+")\"><上一页</a>"
  }else{
    tempStr += "首页";
    tempStr += "<上一页";
  }
  if(currentPage<totalPage){
    tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage+1)+","+psize+")\">下一页></a>";
    tempStr += "<a href=\"#\" onClick=\"goPage("+(totalPage)+","+psize+")\">尾页</a>";
  }else{
    tempStr += "下一页>";
    tempStr += "尾页";
  }
  document.getElementById("barcon").innerHTML = tempStr;
}
</script>
  <style type="text/css">
    table
     {
     text-align:center;
     width:1000px;
     }
     th
     {
     width:100px;
     }
    input
     {
     width:100px;
     }
    td
     {
     width:100px;
     }
  </style>
  </head>
  <body onLoad="goPage(1,10);">
    <form method="post" action="browes.php">
      <table border="1">
        <tr>
          <th><h3>个人概况一览</h3></th>
        </tr>
        <tr>
          <table border="1" >
            <tr>
              <td>姓:</td>
              <td><input type="text" name="surname">  </td>
              <td>名:</td>
              <td><input type="text" name="name">  </td>
              <td>手机:</td>
              <td><input type="text" name="phone">  </td>
              <td>性别:</td>
              <td><select name="sex" id="select_k1" class="xla_k">
                  <option value=""> </option>
                  <option value="male">男</option>
                  <option value="female">女</option>
                </select>
              </td>
              <td>学校:</td>
              <td><input type="text" name="school">  </td>
            </tr>
          </table>
        </tr>
        <tr>
          <td><input type="submit" name="submit" value="提交"> </td>
          <td><input name=reset type=reset value=重置></td>
        </tr>
      </table>
    </form>
</body>
<body>
    <table id="idData" border="1" >
     <tr>
      <th>照片</th>
      <th>姓名</th>
      <th>性别</th>
      <th>生日</th>
      <th>邮箱</th>
      <th>电话</th>
      <th>学校</th>
      <th>技能</th>
      <th>选项</th>
     </tr>
    <?php
      include "../head.php";
      $s = $_POST["surname"];
      $a = $_POST["name"];
      $b = $_POST["phone"];
      $c = $_POST["sex"];
      $d = $_POST["school"];
/*
下面这段代码是PostgreSQL数据库多条数据检索编写数据库的通用方法
*/
      $field = "where 1 = 1 ";
        if($a){
          //magic_quotes_gpc=on,addslashes not used.
          $name = str_replace('\'', "''", $a);
          $field.= "and (name like '%".$name."%') ";
        }
         if(($s)!=NULL){
          $surname = str_replace('\'', "''", $s);
          $field.= "and (surname like '%".$surname."%') ";
        }
        if(($c)!=NULL){
          $field.= "and (sex = '".$c."') ";
        }
        if(($d)!=NULL){
          $school = str_replace('\'', "''", $d);
          $field.= "and (school like '%".$school."%') ";
        }
        if(($b)!=NULL){
          $tel = str_replace('\'', "''", $b);
          $field.= "and (phone = '".$tel."') ";
        }
        $sql = "select * from worker ".$field;
/*
上面这段代码是PostgreSQL数据库多条数据检索编写数据库的通用方法
*/
      $ret = pg_query($db, $sql);
    while($row=pg_fetch_row($ret)){
    ?>
     <tr>
      <td><?php echo $row[9];?></td>
      <td><?php echo $row[1].$row[2];?></td>
      <td><?php echo $row[3];?></td>
      <td><?php echo $row[4];?></td>
      <td><?php echo $row[5];?></td>
      <td><?php echo $row[6];?></td>
      <td><?php echo $row[7];?></td>
      <td><?php echo $row[8];?></td>
      <td><button><a href = "<?php echo 'change.php?id='.$row[0] ?>">change</button>
        <button><a href = "<?php echo 'delete.php?id='.$row[0] ?>">delete</button></td>
     </tr>
    <?php } ?>
    </table>
    <table >
    <div id="barcon" name="barcon"></div>
    </table>
  </body>
</html>

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI