0

Hi everyone,

I'm a beginner in working with PHP and I'm stuck with an annoying problem.
With the help of a tutorial I've made a pagination for database results that works great.

Now I'm trying to adapt the code and use the same pagination for a search-page on my website, but I can't get it to work. I do get the results in pages, but only the first page shows results, while page 2, 3, etc. are blank. I'm probably doing something completely wrong and hope you can help me find the solution. Thank you in advance!

<form method="post" action="zoeken.php">
<input type="hidden" name="submitted" value="true" />

<label>Zoek in database: <input type="text" name="criteria" /></label>
 
<input type="submit" value="zoeken" /> 

</form>
<?php 
MySQL_connect("***", "***", "****"); 
MySQL_select_db("***"); 

$criteria = $_POST['criteria'];
if (isset($_POST['submitted'])) {	


$data = mysql_query("SELECT * FROM data WHERE MATCH(titel, auteur, organisatie, categorie, jaar, tags, link, id) AGAINST('$criteria')") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

$pagenum = $_GET['pagenum'];
 if (!(isset($pagenum))) 
 { 
 $pagenum = 1; }


//This is the number of results displayed per page 

 $page_rows = 10; 

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

// Define your colors for the alternating rows 
    $color1 = "#EDFFFF";  
    $color2 = "#FFFFFF";  
    $row_count = 0; 

$data_p = mysql_query("SELECT * FROM data WHERE MATCH(titel, auteur, organisatie, categorie, jaar, tags, link, id) AGAINST('$criteria') ORDER BY jaar DESC $max") or die(mysql_error()); 

echo "<table>"; 
echo "<tr><th>Titel</th><th>Auteur</th><th>Organisatie</th><th>Categorie</th><th text align=\"center\">Jaar</th></tr>"; 

//This is where you display your query results
while($row = mysql_fetch_array( $data_p )) {

$row_color = ($row_count % 2) ? $color1 : $color2; 

echo
	"<tr><td class=\"titel\" bgcolor=\"$row_color\">" .
	"<a href=\"" . $row['link'] . "\">" .
 	$row['titel'] .  
	 "</td></a><td class=\"auteur\" bgcolor=\"$row_color\">" .
 	$row['auteur'] .
	 "</td><td class=\"instelling\" bgcolor=\"$row_color\">" .
 	$row['organisatie'] .
	 "</td><td class=\"categorie\" bgcolor=\"$row_color\">" .
 	$row['categorie'] . 
	"</td><td class=\"jaar\" bgcolor=\"$row_color\">" . 
	$row['jaar'] . 
	"</td></tr>";
	
	// Add 1 to the row count 
$row_count++;

 } 
echo "</table>";
echo "</br>";
 echo "<p>";

 
 // This shows the user what page they are on, and the total number of pages

 echo " Pagina $pagenum van de $last<p>";

 
 // 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</a> ";

 echo " ";

 $previous = $pagenum-1;

 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-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'>Next -></a> ";

 echo " ";

 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";

 } }
 ?>
3
Contributors
8
Replies
22
Views
5 Years
Discussion Span
Last Post by FBG
0

Database arrays does not understand where result came from (search or article, all it knows is it is from db). It seems your pagination code is tied to DB code which is quiet bad thing to do!

0

Database arrays does not understand where result came from (search or article, all it knows is it is from db). It seems your pagination code is tied to DB code which is quiet bad thing to do!

I still don't completely understand. I thought by using search criteria in the array I could link the pagination to my search results. But you're saying that is impossible?

So I shouldn't use this and make a completely different script?

1

I still don't completely understand. I thought by using search criteria in the array I could link the pagination to my search results. But you're saying that is impossible?

So I shouldn't use this and make a completely different script?

What I'm saying is, your pagination should be free from database issues. It should not care where data come from. For example if your script receives array of data and paginate them you can use it pretty anywhere than when it is tied to DB queries or other stuffs!

0

What I'm saying is, your pagination should be free from database issues. It should not care where data come from. For example if your script receives array of data and paginate them you can use it pretty anywhere than when it is tied to DB queries or other stuffs!

Thank you for your quick response. I understand what you mean now and will try to change it.

0

What I'm saying is, your pagination should be free from database issues. It should not care where data come from. For example if your script receives array of data and paginate them you can use it pretty anywhere than when it is tied to DB queries or other stuffs!

I believe I solved the problem you talked about by making a lot of changes. But still I have a problem:

When I search the database everything works, the tabel and the number of pages show up correctly. But page 2, 3, etc. do not show / load when I click previous/next. The webadres says it's on page 2, but the page doesn't appear. Can you help me find what's wrong? Or is it still the same mistake?

<h1>Zoeken</h1>
<form name="form" action="zoeken.php" method="post">
<input type="text" name="criteria" />
<input type="submit" name="Submit" value="Search" />
</form>

<?php

$criteria = $_POST['criteria'] ;



// check for an empty string and display a message.
if ($criteria == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
	MySQL_connect("*********", "*********", "*********"); 
    MySQL_select_db("*********"); 



$query="SELECT * FROM data WHERE MATCH(titel, auteur, organisatie, categorie, jaar, tags, link, id) AGAINST('$criteria')" ;

$numresults=mysql_query($query);
 $rows=mysql_num_rows($numresults);


if ($rows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $criteria . "&quot; returned zero results</p>";}

$pagenum = $_GET['pagenum'];
if (!(isset($pagenum))) 
 { 
 $pagenum = 1; 
 } 
 
  $page_rows = 10; 
 
 //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;

// Define your colors for the alternating rows 
    $color1 = "#EDFFFF";  
    $color2 = "#FFFFFF";  
    $row_count = 0; 

 
 $result = mysql_query("SELECT * FROM data WHERE MATCH(titel, auteur, organisatie, categorie, jaar, tags, link, id) AGAINST('$criteria') $max") or die("Couldn't execute query");

							
echo "<table>"; 
echo "<tr><th>Titel</th><th>Auteur</th><th>Organisatie</th><th>Categorie</th><th text align=\"center\">Jaar</th></tr>"; 

//This is where you display your query results
while($row = mysql_fetch_array($result)) {

$row_color = ($row_count % 2) ? $color1 : $color2; 

echo
	"<tr><td class=\"titel\" bgcolor=\"$row_color\">" .
	"<a href=\"" . $row['link'] . "\">" .
 	$row['titel'] .  
	 "</td></a><td class=\"auteur\" bgcolor=\"$row_color\">" .
 	$row['auteur'] .
	 "</td><td class=\"instelling\" bgcolor=\"$row_color\">" .
 	$row['organisatie'] .
	 "</td><td class=\"categorie\" bgcolor=\"$row_color\">" .
 	$row['categorie'] . 
	"</td><td class=\"jaar\" bgcolor=\"$row_color\">" . 
	$row['jaar'] . 
	"</td></tr>";
	
	// Add 1 to the row count 
$row_count++;

 } 
echo "</table>";
echo "</br>";
 echo "<p>";

 
 // This shows the user what page they are on, and the total number of pages

 echo " Pagina $pagenum van de $last<p>";

 
 // 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</a> ";

 echo " ";

 $previous = $pagenum-1;

 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-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'>Next -></a> ";

 echo " ";

 echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";

 }
?>
0

The problem is this: " Undefined index: criteria (line 9) "

When I click on the link "next" for the second page, I lose the post criteria.

I hope someone can help me here, because I'm lost. Can I change this script so that it will work, or should I make something else? And if so, what? All ideas and suggestions are welcome.

1

$_POST is not propagated by a link, so you will lose it.

Make the link (next, prev, numbers etc) hold the search terms too, like ...?pagenum=...&criteria=... Then test for $_GET as well as $_POST.

You could mess with session variables, but that would be added nonsense IMO. Better still, you send the search form via $_GET method so you only check for $_GET.

Most paginated searches work this way AFAIK, check out Google - make a search and you get a big url querystring.

Edited by diafol: n/a

Votes + Comments
Helped me create pagination for search results
0

$_POST is not propagated by a link, so you will lose it.

Make the link (next, prev, numbers etc) hold the search terms too, like ...?pagenum=...&criteria=... Then test for $_GET as well as $_POST.

You could mess with session variables, but that would be added nonsense IMO. Better still, you send the search form via $_GET method so you only check for $_GET.

Most paginated searches work this way AFAIK, check out Google - make a search and you get a big url querystring.

It works! I have put the criteria in the links and used $_GET like you said. Thank you very much. Your post was very helpful. Thread is solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.