Reputation: 976
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> \n";
echo "<a href='?pg=".($pg+1)."' class='paginationButton'>NEXT</a> \n";
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?
==============================================================================
$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);
Upvotes: 2
Views: 1433
Reputation: 4334
You can erase pg from your query string and then replace it again. If it isn't there, you don't remove it, but still replace it.
preg_replace('/(^|&)pg=[0-9]+&/','\1',$_SERVER['QUERY_STRING'].'&').'pg='.($pg+1)
What this does is:
Why check for & before and after? I don't want to accidentally replace a variable like 'apg=54'
Upvotes: 0
Reputation: 4967
$q = http_build_query(array_merge($_GET, ["pg" => $pg+1]));
<a href="?$q">..</a>
Upvotes: 4
Reputation: 17289
try replace your:
href='?pg=".($pg+1)."&".$_SERVER['QUERY_STRING']."'
with something like:
$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=...
Upvotes: 0