douya6606 2015-04-30 19:08
浏览 121
已采纳

获取所有URL参数除了一个

I have a pagination script linked to a MySQL query that I can filter. When the results are not filtered and all rows are being shown I have a next link that says "?pg=2" and so on(depending on how many results and such). Once I filter what I want shown my URL contains other parameters like ?certifications=VALUE&state=VALUE.

These lines handle my pagination links.

echo "<a href='?pg=".($pg-1)."' class='paginationButton'>PREVIOUS</a> 
";

echo "<a href='?pg=".($pg+1)."' class='paginationButton'>NEXT</a> 
";

I added $_SERVER['QUERY_STRING'] to each to try to add the url parameters to the links. It works BUT it also adds the ?pg parameter each time it is clicked(basically if next is clicked twice it does this ?certifications=VALUE&state=VALUE&pg=3&pg=2.

How can I make it to where if &pg is already set it does not duplicate it in the URL?

==============================================================================

FULL CODE HERE

$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 "
 <table border='0' class='resultTable' width='75%'> 
";
echo "<tr> 
";
echo "<td width='120px'>ID: </td> 
";
echo "<td>" . $row['id'] . "</td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>Name: </td> 
";
echo "<td>" . $row['name'] . "</td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>Phone: </td> 
";
echo "<td>" . $row['phone'] . "</td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>Alt. Phone: </td> 
";
echo "<td>" . $row['alt_phone'] . "</td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>Fax: </td> 
";
echo "<td>" . $row['fax'] . "</td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>Email: </td> 
";
echo "<td>" . $row['email'] . "</td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>Website: </td> 
";
echo "<td><a href='" . $row['website'] . "' target='_blank'>" . $row['website'] . "</a></td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>City: </td> 
";
echo "<td>" . $row['city'] . "</td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>State: </td> 
";
echo "<td>" . $row['state'] . "</td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>Certifications: </td> 
";
echo "<td>" . $row['certifications'] . "</td> 
";
echo "</tr> 
";
echo "<tr> 
";
echo "<td>Top Sign: </td> 
";
echo "<td>";
if($row['signage'] = 1) {
 echo "Has Top Sign";
}
else {
 echo "Top Sign Not Listed";
}
echo "</td> 
";
echo "</tr> 
";
echo "</table> 

";
}

$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'> 
";
if($pg>1)
{
echo "<a href='?pg=".($pg-1)."&".$_SERVER['QUERY_STRING']."' class='paginationButton'>PREVIOUS</a> 
";
}
if($pg!=$total)
{
echo "<a href='?pg=".($pg+1)."&".$_SERVER['QUERY_STRING']."' class='paginationButton'>NEXT</a> 
";
}

echo "<ul class='page'> 
";
for($i=1;$i<=$total;$i++)
{
if($i==$pg) { echo "<li class='current'>".$i."</li> 
"; }

else { echo "<li><a href='?id=".$i."'>".$i."</a></li> 
"; }
}
echo "</ul> 
";
echo "</div> 
";
mysqli_close($con);
  • 写回答

3条回答 默认 最新

  • douzhannao5357 2015-04-30 19:24
    关注
    $q = http_build_query(array_merge($_GET, ["pg" => $pg+1]));
    
    <a href="?$q">..</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM