我有一个链接到我可以过滤的MySQL查询的分页脚本。当结果未被过滤并且所有行都被显示时,我有一个下一个链接,表示“?pg = 2”等等(取决于有多少结果等)。一旦我过滤了我想要显示的内容,我的网址就会包含其他参数,例如?certifications = VALUE& state = VALUE。
这些行处理我的分页链接。
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参数添加到链接中。它工作但它每次点击时都会添加?pg参数(基本上如果点击两次它会这样做吗?认证= VALUE&amp; state = VALUE&amp; pg = 3&amp; pg = 2.
如果已经设置&amp; pg,我怎样才能将其设置为不在URL中复制?
=============================================== ===============================
$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);
答案 0 :(得分:4)
$q = http_build_query(array_merge($_GET, ["pg" => $pg+1]));
<a href="?$q">..</a>
答案 1 :(得分:0)
尝试替换你的:
href='?pg=".($pg+1)."&".$_SERVER['QUERY_STRING']."'
有类似的东西:
$allParams = $_GET;
$allParams['pg'] = $pg+1;
$queryString = '?';
foreach ($allParams as $param=>$param_val) {
if ($queryString != '?') $queryString .= '&';
$queryString .= $param.'='.urlencode($param_val);
}
...
echo "<a href='$queryString' class=...
答案 2 :(得分:0)
您可以从查询字符串中删除pg,然后再次替换它。如果它不在那里,你不会删除它,但仍然替换它。
preg_replace('/(^|&)pg=[0-9]+&/','\1',$_SERVER['QUERY_STRING'].'&').'pg='.($pg+1)
这是做什么的:
为什么要检查&amp;之前和之后?我不想意外地替换像'apg = 54'
这样的变量