PHP實(shí)現(xiàn)從PostgreSQL數(shù)據(jù)庫(kù)檢索數(shù)據(jù)分頁(yè)顯示及根據(jù)條件查找數(shù)據(jù)示例
本文實(shí)例講述了PHP實(shí)現(xiàn)從PostgreSQL數(shù)據(jù)庫(kù)檢索數(shù)據(jù)分頁(yè)顯示及根據(jù)條件查找數(shù)據(jù)。分享給大家供大家參考,具體如下:
主要功能是從postgreSql查詢數(shù)據(jù),并檢索,由于自己剛開(kāi)始接觸,所以難點(diǎn)在于多條數(shù)據(jù)同時(shí)篩選并分頁(yè)顯示出來(lái),寫(xiě)下自己的代碼與大家共享。
<html> <head> <script type="text/javascript"> /** * 分頁(yè)函數(shù) * pno--頁(yè)數(shù) * psize--每頁(yè)顯示記錄數(shù) * 分頁(yè)部分是從真實(shí)數(shù)據(jù)行開(kāi)始,因而存在加減某個(gè)常數(shù),以確定真正的記錄數(shù) * 純js分頁(yè)實(shí)質(zhì)是數(shù)據(jù)行全部加載,通過(guò)是否顯示屬性完成分頁(yè)功能 **/ function goPage(pno,psize){ var itable = document.getElementById("idData"); var num = itable.rows.length;//表格所有行數(shù)(所有記錄數(shù)) console.log(num); var totalPage = 0;//總頁(yè)數(shù) var pageSize = psize;//每頁(yè)顯示行數(shù) //總共分幾頁(yè) if(num/pageSize > parseInt(num/pageSize)){ totalPage=parseInt(num/pageSize)+1; }else{ totalPage=parseInt(num/pageSize); } var currentPage = pno;//當(dāng)前頁(yè)數(shù) var startRow = (currentPage - 1) * pageSize+1;//開(kāi)始顯示的行 31 var endRow = currentPage * pageSize;//結(jié)束顯示的行 40 endRow = (endRow > num)? num : endRow; 40 console.log(endRow); //遍歷顯示數(shù)據(jù)實(shí)現(xiàn)分頁(yè) 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+"頁(yè) 當(dāng)前第"+currentPage+"頁(yè)"; if(currentPage>1){ tempStr += "<a href=\"#\" onClick=\"goPage("+(1)+","+psize+")\">首頁(yè)</a>"; tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage-1)+","+psize+")\"><上一頁(yè)</a>" }else{ tempStr += "首頁(yè)"; tempStr += "<上一頁(yè)"; } if(currentPage<totalPage){ tempStr += "<a href=\"#\" onClick=\"goPage("+(currentPage+1)+","+psize+")\">下一頁(yè)></a>"; tempStr += "<a href=\"#\" onClick=\"goPage("+(totalPage)+","+psize+")\">尾頁(yè)</a>"; }else{ tempStr += "下一頁(yè)>"; tempStr += "尾頁(yè)"; } 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>個(gè)人概況一覽</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>手機(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ù)庫(kù)多條數(shù)據(jù)檢索編寫(xiě)數(shù)據(jù)庫(kù)的通用方法 */ $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ù)庫(kù)多條數(shù)據(jù)檢索編寫(xiě)數(shù)據(jù)庫(kù)的通用方法 */ $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>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《php+Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫(kù)操作技巧大全》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php保存二進(jìn)制原始數(shù)據(jù)為圖片的程序代碼
得到post過(guò)來(lái)的二進(jìn)制原始數(shù)據(jù),選擇一個(gè)生成路徑及圖片的名字,之后寫(xiě)入,思路很顯而易見(jiàn),需要的朋友可以收藏下2014-10-10PHP使用反向Ajax技術(shù)實(shí)現(xiàn)在線客服系統(tǒng)詳解
這篇文章主要介紹了PHP使用反向Ajax技術(shù)實(shí)現(xiàn)在線客服系統(tǒng),簡(jiǎn)單描述了反向ajax的概念、原理及使用反向ajax實(shí)現(xiàn)在線客服的相關(guān)操作技巧,需要的朋友可以參考下2019-07-07php過(guò)濾所有的空白字符(空格、全角空格、換行等)
這篇文章主要介紹了php替換過(guò)濾所有的空白字符,包括空格、全角空格、換行等,感興趣的小伙伴們可以一起學(xué)習(xí)學(xué)習(xí)。2015-10-10