PHP实现从PostgreSQL数据库检索数据分页显示及根据条件查找数据示例

时间:2022-08-24 15:18:58

本文实例讲述了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><h2>个人概况一览</h2></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>

希望本文所述对大家PHP程序设计有所帮助。