Search and Pagination
Posted: Thu Dec 18, 2008 4:20 pm
I have some problem with pagination for search results for a photo gallery.
The pagination works fine with just showing the gallery, but not when searching.
It's a simple text string search for one field.
The pagination seems to be counting the pages correct and showing the first page, but the problem is passing the search with the "next" link etc. Then I get an empty page.
I'll paste some of the code below
1. PAGINATION INITIATION
2. GET RESULST AND DISPLAY
PAGINATION LINKS
I'd be most grateful if someone can spot what is wrong or what needs to be added to this. I guess it has to do with the pagination links and how to pass the search result to the next page?
Thanks
Bjorn
The pagination works fine with just showing the gallery, but not when searching.
It's a simple text string search for one field.
The pagination seems to be counting the pages correct and showing the first page, but the problem is passing the search with the "next" link etc. Then I get an empty page.
I'll paste some of the code below
1. PAGINATION INITIATION
Code: Select all
$search = $_POST['search']; // FROM SEARCH FORM
//PAGINATION INITIATE
$pagenum = $_GET['pagenum'];
if (!(isset($pagenum))) {
$pagenum = 1;
}
//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM photographs WHERE photo_title LIKE '%".$search."%'") or die(mysql_error());
$rows = mysql_num_rows($data);
//This is the number of results displayed per page
$page_rows = 6;
//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;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
Code: Select all
if ($search != '') {
//GET RESULTS
$QUERY = mysql_query("SELECT * FROM photographs WHERE photo_title LIKE '%".$search."%' $max");
$NUMROWS = mysql_num_rows($QUERY);
if (!$NUMROWS) {
echo ("No Search Result");
} else {
$I = 0;
while ($I < $NUMROWS) {
$photo_title = mysql_result($QUERY,$I,"photo_title");
echo $photo_title;
$I++;
}
}
} else {
echo ("No Search Result");
}
Code: Select all
// This shows the user what page they are on, and the total number of pages
echo "Page $pagenum of $last";
// 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) {
} else {
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=1'>FIRST PAGE</a> ";
echo " ";
$previous = $pagenum-1;
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'>PREVIOUS PAGE</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'>NEXT</a> ";
echo " ";
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>LAST</a> ";
}
//END PAGINATION
Thanks
Bjorn