Pagination!
Posted: Fri Apr 08, 2005 4:26 am
Hi guys, I'm having a small pagination problem when trying to display my results. I have a search engine where a user enters keywords and then retrieves results. I have set a maximum display of 10 items per page. What happens is my results page correctly shows how many results there are in the database, splits the results in the right number of pages but I can only see results on the first page. If I click on page 2 or 3 it says "You have not entered search details. Please go back and try again."
Any help would be greatly appreciated! Thank you.
My script looks like this:
Any help would be greatly appreciated! Thank you.
My script looks like this:
Code: Select all
$searchtype=$HTTP_POST_VARS['searchtype'];
$searchterm=$HTTP_POST_VARS['searchterm'];
$searchterm= trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details. Please go back and try again.';
exit;
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
$display = 10;
if (isset($_GET['np'])) {
$num_pages = $_GET['np'];
} else {
$query = "SELECT * FROM books, course WHERE ".$searchtype." like '%".$searchterm."%' AND books.isbn = course.isbn";
$query_result = mysql_query ($query) or die(mysql_error());
$num_results = @mysql_num_rows($query_result);
if ($num_results > $display) { // more than 1 page
$num_pages = ceil ($num_results/$display);
} else {
$num_pages = 1;
}
}
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
//Run Query
$query = "SELECT * FROM books, course WHERE ".$searchtype." like '%".$searchterm."%' AND books.isbn = course.isbn LIMIT $start, $display";
$result = @mysql_query ($query) or die(mysql_error()); // Run the query
$num = mysql_num_rows ($result);
if ($num >= 0) { // If everything was ok, display records.
echo '<p align="center"><font size="4">Number of books found: '.$num_results.'</font></p>';
//Make links to the other pages, if needed.
if ($num_pages > 1) {
echo '<p align="center">';
$current_page = ($start/$display) + 1;
//If you are on the second page, make a Previous link.
if ($current_page != 1) {
echo '<a href="results.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
}
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="results.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
} else {
echo $i . ' ';
}
}
//If it's not the last page, make a Next link.
if ($current_page != $num_pages) {
echo '<a href="results.php?s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a>';
}
echo '</p><br />';
} //End of links
}