I’m have a search field on a page. I also have pagination with the search results. I’m trying to get this to work by loading the same page($_SERVER[‘PHP_SELF’]) instead of having the pagination results on a new page .
My problem is to get the pagination to work correctly.
Since I have a $search = $_POST[‘search’] for receiving the search there is a problem with the pagination for the second pagination page because I somehow need $search = $_GET[‘search’] when I click on the next links for pagination.
I know I could avoid this by creating a new page that receives the linked pagination values
I have a lot of code on this page but I’m displaying some here. Maybe you can have some input about this. Wether it can be done by using one page(search.php) or if I should just create a second page for the pagination results(ie search2.php)
search.php page
POSTING FORM
<form action=“search.php” method=“post”>
<input name=“search” type=“text” />
<input type=“submit” value=“Find” />
GETTING SEARCH RESULST INCLUDING PAGINATION
//Getting posted search value
$search = clean($_POST['search']);
//***PAGINATION***
//Getting the pagenumber value from the links
$pagenum = $_GET['pagenum'];
//PAGINATION INITIATE_____________
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM members WHERE studio_name LIKE '%".$search."%'") or die(mysql_error());
$rows = mysql_num_rows($data);
//This is the number of results displayed per page
$page_rows = 25;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$counting = ($page_rows * $pagenum) - ($page_rows) + 1;
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//SELECT MEMBERS AND GET RESULTS
$QUERY = mysql_query("SELECT * FROM members WHERE studio_name LIKE '%".$search."%' ORDER BY studio_name $max");
$NUMROWS = @mysql_num_rows($QUERY);
if (!$NUMROWS) {
echo "No results found";
} else {
$I = 0;
while ($I < $NUMROWS) {
$member_id = mysql_result($QUERY,$I,"member_id");
$studio_name = mysql_result($QUERY,$I,"studio_name");
echo $studio_name;
$I++;
}
}
PAGINATION LINKS
// First we check if we are on page one. If we are then we don't need a link to the previous page
// or the first page so we do nothing. If we aren't then we generate links to the first page, and to
// the previous page.
if ($pagenum == 1)
{
//nothing here
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&studio_name=$search'><img src='images/buttonfirst.png' border='0' title='First'</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&studio_name=$search'><img src='images/buttonprevious.png' border='0' title='Previous'></a> ";
}
//just a spacer
echo " ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&studio_name=$search'><img src='images/buttonnext.png' border='0' title='Next'></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&studio_name=$search'><img src='images/buttonlast.png' border='0' title='Last'></a> ";
}