PHP Realizes Retrieving Data from PostgreSQL Database Paging Display and Finding Data According to Conditions Example

  • 2021-10-15 10:00:15
  • OfStack

This article describes the example of PHP from PostgreSQL database retrieval data paging display and according to the conditions to find data. Share it for your reference, as follows:

The main function is to query data from postgreSql and retrieve it. Because I have just started to contact it, the difficulty lies in screening and paging multiple data at the same time, and writing down my own code to share with everyone.


<html>
<head>
<script type="text/javascript">
  /**
 *  Paging function 
 * pno-- Number of pages 
 * psize-- Number of records displayed per page 
 *  The paging part starts with the real data row, so there is an addition and subtraction constant to determine the real number of records 
 *  Pure js Paging essentially means that all data rows are loaded, and the paging function is completed by whether to display attributes or not 
 **/
function goPage(pno,psize){
  var itable = document.getElementById("idData");
  var num = itable.rows.length;// Number of all rows in the table ( Number of all records )
  console.log(num);
  var totalPage = 0;// Total pages 
  var pageSize = psize;// Number of rows per page 
  // How many pages are there altogether 
  if(num/pageSize > parseInt(num/pageSize)){
      totalPage=parseInt(num/pageSize)+1;
    }else{
      totalPage=parseInt(num/pageSize);
    }
  var currentPage = pno;// Current number of pages 
  var startRow = (currentPage - 1) * pageSize+1;// Line to start displaying  31
    var endRow = currentPage * pageSize;// End the displayed line   40
    endRow = (endRow > num)? num : endRow;  40
    console.log(endRow);
    // Traversing display data to realize paging 
  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 = " Altogether "+num+" A record   Points "+totalPage+" Page   Current "+currentPage+" Page ";
  if(currentPage>1){
    tempStr += "<a href=\"#\" onClick=\"goPage("+(1)+","+psize+")\"> Home page </a>";
    tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage-1)+","+psize+")\">< Upper 1 Page </a>"
  }else{
    tempStr += " Home page ";
    tempStr += "< Upper 1 Page ";
  }
  if(currentPage<totalPage){
    tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage+1)+","+psize+")\"> Under 1 Page ></a>";
    tempStr += "<a href=\"#\" onClick=\"goPage("+(totalPage)+","+psize+")\"> End page </a>";
  }else{
    tempStr += " Under 1 Page >";
    tempStr += " End page ";
  }
  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> Personal profile 1 View </h2></th>
        </tr>
        <tr>
          <table border="1" >
            <tr>
              <td> Last name: </td>
              <td><input type="text" name="surname">  </td>
              <td> Name: </td>
              <td><input type="text" name="name">  </td>
              <td> Mobile phone :</td>
              <td><input type="text" name="phone">  </td>
              <td> Gender: </td>
              <td><select name="sex" id="select_k1" class="xla_k">
                  <option value=""> </option>
                  <option value="male"> Male </option>
                  <option value="female"> Female </option>
                </select>
              </td>
              <td> Schools :</td>
              <td><input type="text" name="school">  </td>
            </tr>
          </table>
        </tr>
        <tr>
          <td><input type="submit" name="submit" value=" Submit "> </td>
          <td><input name=reset type=reset value= Reset ></td>
        </tr>
      </table>
    </form>
</body>
<body>
    <table id="idData" border="1" >
     <tr>
      <th> Photos </th>
      <th> Name </th>
      <th> Gender </th>
      <th> Birthday </th>
      <th> Mailbox </th>
      <th> Telephone </th>
      <th> Schools </th>
      <th> Skills </th>
      <th> Options </th>
     </tr>
    <?php
      include "../head.php";
      $s = $_POST["surname"];
      $a = $_POST["name"];
      $b = $_POST["phone"];
      $c = $_POST["sex"];
      $d = $_POST["school"];
/*
 The following code is PostgreSQL A general method of compiling database by retrieving multiple data in database 
*/
      $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;
/*
 The above code is PostgreSQL A general method of compiling database by retrieving multiple data in database 
*/
      $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>

For more readers interested in PHP related contents, please check the topics on this site: "Summary of PHP Database Operation Skills Based on pdo", "Summary of php+Oracle Database Programming Skills", "Encyclopedia of PHP+MongoDB Database Operation Skills", "Introduction to php Object-Oriented Programming", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: