Page 1 of 1

Pagination!

Posted: Fri Apr 08, 2005 4:26 am
by vittelite
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:


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
}

Posted: Fri Apr 08, 2005 6:02 am
by Drachlen
The problem is the fact that after you're clicking the links, you're losing the values of the original search.
Try modifying all the links to carry the values over to the next page.

Code: Select all

echo '<a href="results.php?s=' . ($start + $display) . '&np=' . $num_pages . '&searchtype=' . $searchtype . '&searchterm=' . $searchterm . '">Next</a>';
Also note this means you will have to modify your original search form from "POST" to "GET"

Posted: Fri Apr 08, 2005 6:43 am
by timvw
or use a session to store that sort of data in..

Posted: Fri Apr 08, 2005 7:38 am
by vittelite
Thanks,
that works! Only thing now is that I pass my results onto the following pages, but the total number of results disappears! In other words lets say as soon as I search it gives me: Number of results: 24, on page 2 or 3 it'll say: Number of results:
Not sure why it's not keeping the value or if I go back to page 1 it's gone.
Thanks!