溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用PHP怎么實(shí)現(xiàn)從PostgreSQL數(shù)據(jù)庫檢索數(shù)據(jù)分頁顯示

發(fā)布時間:2021-05-27 16:33:02 來源:億速云 閱讀:134 作者:Leah 欄目:開發(fā)技術(shù)

使用PHP怎么實(shí)現(xiàn)從PostgreSQL數(shù)據(jù)庫檢索數(shù)據(jù)分頁顯示?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

主要功能是從postgreSql查詢數(shù)據(jù),并檢索,由于自己剛開始接觸,所以難點(diǎn)在于多條數(shù)據(jù)同時篩選并分頁顯示出來,寫下自己的代碼與大家共享。

<html>
<head>
<script type="text/javascript">
  /**
 * 分頁函數(shù)
 * pno--頁數(shù)
 * psize--每頁顯示記錄數(shù)
 * 分頁部分是從真實(shí)數(shù)據(jù)行開始,因而存在加減某個常數(shù),以確定真正的記錄數(shù)
 * 純js分頁實(shí)質(zhì)是數(shù)據(jù)行全部加載,通過是否顯示屬性完成分頁功能
 **/
function goPage(pno,psize){
  var itable = document.getElementById("idData");
  var num = itable.rows.length;//表格所有行數(shù)(所有記錄數(shù))
  console.log(num);
  var totalPage = 0;//總頁數(shù)
  var pageSize = psize;//每頁顯示行數(shù)
  //總共分幾頁
  if(num/pageSize > parseInt(num/pageSize)){
      totalPage=parseInt(num/pageSize)+1;
    }else{
      totalPage=parseInt(num/pageSize);
    }
  var currentPage = pno;//當(dāng)前頁數(shù)
  var startRow = (currentPage - 1) * pageSize+1;//開始顯示的行 31
    var endRow = currentPage * pageSize;//結(jié)束顯示的行  40
    endRow = (endRow > num)? num : endRow;  40
    console.log(endRow);
    //遍歷顯示數(shù)據(jù)實(shí)現(xiàn)分頁
  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+"頁 當(dāng)前第"+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>手機(jī):</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>學(xué)校:</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>學(xué)校</th>
      <th>技能</th>
      <th>選項(xiàng)</th>
     </tr>
    <?php
      include "../head.php";
      $s = $_POST["surname"];
      $a = $_POST["name"];
      $b = $_POST["phone"];
      $c = $_POST["sex"];
      $d = $_POST["school"];
/*
下面這段代碼是PostgreSQL數(shù)據(jù)庫多條數(shù)據(jù)檢索編寫數(shù)據(jù)庫的通用方法
*/
      $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數(shù)據(jù)庫多條數(shù)據(jù)檢索編寫數(shù)據(jù)庫的通用方法
*/
      $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>

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI