Reputation: 38
Trying to create pagination with search query.Everything working fine,here error occurs,i'm not able to use $page
to search.
Want like this in browser:
search.php?page=what+i+search where it equals to searched query
Code working properly(without query):
for ($b=1;$b<=$a;$b++)
{
?><a href="search.php?page=<?php echo $b; ?>" style="text-decoration:none"><?php echo $b." ";?></a> <?php
}
?>
Tried like this:
for ($b=1;$b<=$a;$b++)
echo " <a href='search.php?page=“.stripslashes($page).”'> </a>";
?>
and like this:
for ($b=1;$b<=$a;$b++)
echo " <a href='search.php?page=$page&submit=$b'> </a>";
?>
Here $page
is name in simple html form,see:
<form action="search.php" method="GET">
<b>Enter Search Term:</b> <input type="text" name="page" >
<br>
<input type="submit" value="Search">
</form>
Pls help,i want to include search query in pagination.
Upvotes: 0
Views: 90
Reputation: 2844
Use PHP's built in function urlencode to encode $page value correctly.
Alternatively you can use http_build_query to build query string dynamicly:
$searchTerm = 'foo bar'; // it can be empty
$queryData = $searchTerm ? ['search' => $searchTerm] : [];
for ($i = 1; $i <= $maxPages; $i++) {
$queryData['page'] = $i;
$href = 'search.php?' . http_build_query($queryData);
echo " <a href='$href'>$i</a>";
}
Upvotes: 1
Reputation: 3338
There are two variables that you need to keep track of:
This means that your final url should look something like this:
search.php?search=words&page=2
If you loop through your $_GET
variables, you will see entries for both $_GET['search']
and $_GET['page']
. Your code should also have something that specifies the page a 1
if it is not set.
Your form can stay the same. However, when building the page links, you should do the following:
for($p=1;$p<=$maxPages;$p++) {
echo '<a href="search.php?search=';
echo urlencode($_GET['search']); // urlencode() escapes spaces and other special characters
echo '&page=' . $p;
echo '">' . $p . '</a>';
}
Upvotes: 0