Page 1 of 1

Search and Pagination

Posted: Thu Dec 18, 2008 4:20 pm
by cooldude
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

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; 
 
2. GET RESULST AND DISPLAY

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");  
}
 
PAGINATION LINKS

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 
 
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

Re: Search and Pagination

Posted: Thu Dec 18, 2008 8:06 pm
by califdon
I think your problem comes from not returning the search criterion after the first time. You could solve that either by setting a Session Variable, or by always passing the search criterion in the URL, as you do the page number, something like this:

Code: Select all

echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$next[color=#4000FF]&search=$search[/color]'>NEXT</a> ";
echo " ";
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$last[color=#4000FF]&search=$search[/color]'>LAST</a> ";
You'll probably have to change more than that, but that's the general approach.

Re: Search and Pagination

Posted: Thu Dec 18, 2008 8:34 pm
by cooldude
I actually tried that before, but it didn't work. It passed the value to the next page with string, but for some reason it was still an empty page with no results returned.

Maybe there is something else in the code that I've missed?

I was wondering if it could have anything to do with the following piece of code

I added an if statement incase someone clicks the submit button without filling in the form field.

So I was wondering if that is a problem when the page is reloaded again to go to the next page? I haven't checked it yet, but I will

Code: Select all

 
 
if ($search != '') {  
 
//GET RESULTS  
$QUERY = mysql_query("SELECT * FROM photographs WHERE photo_title LIKE '%".$search."%' $max");

Re: Search and Pagination

Posted: Fri Dec 19, 2008 3:30 pm
by cooldude
There is some problem or conflict with $_POST and $_GET or something

the first page seems to be ok, but it's not passing the value to the next page. Actually it seems to pass the value to the next page(I can see it in the web address), but it's not receiving the value with $_GET. I don't know if this has to do with the $_POST

I'm using this in the pagination links etc.

Code: Select all

 
"<a href='{$_SERVER['PHP_SELF']}?pagenum=$first&photo_title=$search'>FIRST</a>
 
then I have this to receive the posted value from the form and to get the number

Code: Select all

 
 $search = $_POST['search']; // FROM SEARCH FORM  
 
//PAGINATION INITIATE 
 
$pagenum = $_GET['pagenum']; 
 
But to get the passed value from the search on the second page if I do the following, it does not pass the value

Code: Select all

 
 
$search = $_GET['photo_title']; 
 
Because then you 2 $search.($_POST and $_GET)

Do you have a suggestion to this?

Thanks

Bjorn

Re: Search and Pagination

Posted: Fri Dec 19, 2008 4:42 pm
by califdon
You're using a different name when you send it as part of the URL (photo_title) than when you try to retrieve it (search). That'll do it every time! :? Use the same name.

Re: Search and Pagination

Posted: Fri Dec 19, 2008 6:20 pm
by cooldude
Yes, I saw that too. I changed the form field name and I also linked to a new page for the search result so that there would not be an issue with $POST.

And that worked fine.

Thanks for your input.

Bjorn