我有一個分頁腳本鏈接到一個MySQL查詢,我可以過濾。當結果未被過濾並且所有行都顯示時,我有下一個鏈接,指出「?pg = 2」等等(取決於多少結果等)。一旦我過濾了我想要顯示的內容,我的URL包含其他參數,如?certifications = VALUE & state = VALUE。獲取所有URL參數除了一個
這些行處理我的分頁鏈接。
echo "<a href='?pg=".($pg-1)."' class='paginationButton'>PREVIOUS</a> \n";
echo "<a href='?pg=".($pg+1)."' class='paginationButton'>NEXT</a> \n";
我向每個添加了$ _SERVER ['QUERY_STRING']以嘗試將url參數添加到鏈接。它的工作原理,但它也每次被點擊時(基本上如果下一個被點擊了兩次它呢?認證= VALUE &狀態= VALUE & PG = 3 & PG = 2。
我怎樣才能使增加的?PG參數如果& pg已經設置,它不會在URL中複製它?
============================ ==================================================
FULL代碼在這裏
$start=0;
$limit=3;
if(isset($_GET['pg']))
{
$pg=$_GET['pg'];
$start=($pg-1)*$limit;
}
else {
$pg = 1;
}
$sql = mysql_query($query);
$conditions = "SELECT * FROM pilotOperators WHERE 1=1";
# append condition for signage (if required)
if(isset($_GET['signage'])) {
$conditions .= " AND signage='1'";
}
# append condition for certifications (if required)
if(isset($_GET['certifications'])) {
$certifications = mysqli_real_escape_string($conn,$_GET['certifications']);
$conditions .= " AND certifications='$certifications'";
}
# append condition for state (if required)
if(isset($_GET['state'])) {
if($_GET['state'] != "Select State") {
$state = mysqli_real_escape_string($conn,$_GET['state']);
$conditions .= " AND state='$state'";
}
}
$conditions .= " Limit $start, $limit";
$result = mysqli_query($conn, $conditions);
while($row = mysqli_fetch_array($result))
{
echo "\n <table border='0' class='resultTable' width='75%'> \n";
echo "<tr> \n";
echo "<td width='120px'>ID: </td> \n";
echo "<td>" . $row['id'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Name: </td> \n";
echo "<td>" . $row['name'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Phone: </td> \n";
echo "<td>" . $row['phone'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Alt. Phone: </td> \n";
echo "<td>" . $row['alt_phone'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Fax: </td> \n";
echo "<td>" . $row['fax'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Email: </td> \n";
echo "<td>" . $row['email'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Website: </td> \n";
echo "<td><a href='" . $row['website'] . "' target='_blank'>" . $row['website'] . "</a></td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>City: </td> \n";
echo "<td>" . $row['city'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>State: </td> \n";
echo "<td>" . $row['state'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Certifications: </td> \n";
echo "<td>" . $row['certifications'] . "</td> \n";
echo "</tr> \n";
echo "<tr> \n";
echo "<td>Top Sign: </td> \n";
echo "<td>";
if($row['signage'] = 1) {
echo "Has Top Sign";
}
else {
echo "Top Sign Not Listed";
}
echo "</td> \n";
echo "</tr> \n";
echo "</table> \n\n";
}
$countconditions = "SELECT * FROM pilotOperators WHERE 1=1";
# append condition for signage (if required)
if(isset($_GET['signage'])) {
$countconditions .= " AND signage='1'";
}
# append condition for certifications (if required)
if(isset($_GET['certifications'])) {
$certifications = mysqli_real_escape_string($conn,$_GET['certifications']);
$countconditions .= " AND certifications='$certifications'";
}
# append condition for state (if required)
if(isset($_GET['state'])) {
if($_GET['state'] != "Select State") {
$state = mysqli_real_escape_string($conn,$_GET['state']);
$countconditions .= " AND state='$state'";
}
}
$rows = mysqli_num_rows(mysqli_query($conn, $countconditions));
$total=ceil($rows/$limit);
echo "<div id='paginationLinks'> \n";
if($pg>1)
{
echo "<a href='?pg=".($pg-1)."&".$_SERVER['QUERY_STRING']."' class='paginationButton'>PREVIOUS</a> \n";
}
if($pg!=$total)
{
echo "<a href='?pg=".($pg+1)."&".$_SERVER['QUERY_STRING']."' class='paginationButton'>NEXT</a> \n";
}
echo "<ul class='page'> \n";
for($i=1;$i<=$total;$i++)
{
if($i==$pg) { echo "<li class='current'>".$i."</li> \n"; }
else { echo "<li><a href='?id=".$i."'>".$i."</a></li> \n"; }
}
echo "</ul> \n";
echo "</div> \n";
mysqli_close($con);
看看'parse_str'它是一個PHP函數,用於將querystrings解析爲關聯數組。當你有這個時,你可以簡單地替換數組中的pg條目,然後使用'http_build_query'將其字符串化回查詢字符串。看看: https://php.net/parse_str – EJTH