Help with pagination!

Hi all!

So, I have a website for a database which contains biological data (proteins). In this website, there is a text search page,
where the user can specify one or more search terms in order to get data from the database. The user can enter up to 5 search terms in each of the
available text boxes, as shown below


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Text search</title>
<link rel="shortcut icon" type="image/x-icon" href="css/images/fav.ico">
<link rel ="stylesheet" href ="css/style.css">
<script type="text/javascript">
function setReadOnly(obj)
{
	if(obj.value == "specific")
	{
		document.forms[0].mytext.style.backgroundColor = "#ffffff";
		document.forms[0].mytext.readOnly = 0;
		document.forms[0].mytext.value = "";
	}
	else
	{
		document.forms[0].mytext.style.backgroundColor = "#eeeeee";
		document.forms[0].mytext.readOnly = 1;
		document.forms[0].mytext.value = "50.00";
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
}
addLoadEvent(prepareInputsForHints);

</script>
</head>
<body>
<div align="center">
<table class="whole">
	<tbody>
		<tr>
  			<td> <?php include 'css/header.html';?> </td>
      		</tr>
      		<tr>
			<td> <?php include 'css/navbar.php';?> </td>
         	</tr>
         	<form enctype="multipart/form-data" method="post" action="retrieve_text.php">
			 <tr>
			 	<td>
			 		<div align="center">
				 	<p class="headcell"><br/>Text search</p>
				 	
					<dl>
						<dt>
							Protein Name/Description
						</dt>
						<dd>
							<input type="text" name="prot_name" size="35" />
							<span class="hint">e.g. ompa_ecoli, transmembrane, porin, adhesion<span class="hint-pointer">&nbsp;</span>
						</dd>

						<dt>
							Gene name
						</dt>
						<dd>
							<input type="text" name="prot_gene" size="35" />
							<span class="hint">e.g. ompA, porB, oprF, PMSV_1498<span class="hint-pointer">&nbsp;</span>
						</dd>

						<dt>
							Organism name
						</dt>
						<dd>
							<input type="text" name="org_name" size="35" />
							<span class="hint">e.g. Escherichia coli, Vibrio sp. (strain Ex25)<span class="hint-pointer">&nbsp;</span>
						</dd>

						<dt>
							Organism taxonomy
						</dt>
						<dd>
							<input type="text" name="taxid" size="35" />
							<span class="hint">e.g. 595536, 1051646<span class="hint-pointer">&nbsp;</span>
						</dd>

						<dt>
							Cross-references
						</dt>
						<dd>
							<input type="text" name="crossref" size="35" />
							<span class="hint">e.g. F2HUT9, PF01389, IPR006665, 1A0S<span class="hint-pointer">&nbsp;</span>
						</dd>

						<dt>
							Literature references (PMID)
						</dt>
						<dd>
							<input type="text" name="pmid" size="35" />
							<span class="hint">e.g. 9294435, 17114266, 8759855<span class="hint-pointer">&nbsp;</span>
						</dd>

						<dt>
							Signal peptide information
						</dt>
						<dd>
							<select name="signal">
								<option value="ALL" selected>All available</option>
								<option value="Experimentally verified">Experimentally verified</option>
								<option value="Experimentally verified [by similarity]">Experimentally verified [by similarity]</option>
								<option value="Prediction methods">SignalP 4.0</option>
								<option value="No information">No information</option>
							</select>
						</dd>
						
						<dt>
							Perform search in
						</dt>
						<dd>
							<select name="family">
								<option value="0" selected>All families</option>
								<?php
				 					require_once("css/connection.php");                       		
				 					$query = "SELECT families.families_id, families.families_name FROM families";
									$result = mysql_query($query) or die("<br><br><br>Please contact the administrator of the website.<br><br><br><br><br><br><br><br></td></div></tr>");

									while ($line = mysql_fetch_row($result))
									{echo "<option value=".$line[0].">".$line[1]."</option>\
";}
								?>
							</select>
						</dd>
						<dt>
							Combine search criteria using	
						</dt>
						<dd>
							<input type="radio" name="operator" value="combined" checked /> AND
						      	&nbsp;&nbsp;&nbsp;<input type="radio" name="operator" value="separate" /> OR
						</dd>

						<dt>
							Incude protein fragments in the results	
						</dt>
						<dd>
							<input type="checkbox" class="check" name="fragment" checked />
						</dd>
						<dd>
							<input type="hidden" name="order" value="id" />
						</dd>

					</dl>
			 		</div>
			 	</td>
			 </tr>
			 <tr>           	
			 	<td><br>
			     		<input type="submit" value="Perform search">
					<input type="reset" value="Clear fields">
				 </td>
			</tr>
		</form>
 		<tr><td><br/></td></tr>
          	<tr>
   		 	<td height="40" class="note"><u><b><i>Note</i></b></u> : </b>
			You can specify up to 5 search terms in each text field, provided that they are separated by a comma.</td>
            	</tr>
   	</tbody>
</table>
</div>
</body>
</html>

The retrieve_text.php page gets the data submitted by the user and fetches the results. Also, if the user clicks on 1 or more of the
checkboxes next to each record, the respective entries are downloaded as raw format archives.
However, because in some cases there were more than 200 results
I decided to implement pagination… I searched around and asked for help, and managed to get it as far as you see below.


&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Text search results&lt;/title&gt;
&lt;link rel="shortcut icon" type="image/x-icon" href="css/images/fav.ico"&gt;
&lt;link rel ="stylesheet" href ="css/style.css"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div align="center"&gt;
&lt;table class="whole"&gt;
	&lt;tbody&gt;
	&lt;!-- arxikos pinakas --&gt;
		&lt;tr&gt;
  			&lt;td&gt; &lt;?php include 'css/header.html';?&gt; &lt;/td&gt;
      		&lt;/tr&gt;
      		&lt;tr&gt;
			&lt;td&gt; &lt;?php include 'css/navbar.php';?&gt; &lt;/td&gt;
        	&lt;/tr&gt;
        	&lt;tr&gt;
         		&lt;td&gt;
         		&lt;div align="center"&gt;
         		&lt;table class="included"&gt;
				&lt;tbody&gt;
				&lt;?php
				session_start();
 				require_once("css/connection.php");

print "POST_ARRAY:";print_r($_POST);print "&lt;br&gt;&lt;br&gt;";
print "SESSION_CHOICE_ARRAY:";print_r($_SESSION['choice']);print "&lt;br&gt;&lt;br&gt;";

				/*&lt;--- Download raw entries in zip form BEGIN---&gt;*/
		       		if ($_POST['submit'] == 'Retrieve')
		        	{
					if($count_selected&lt;1000)
				        {
					        $add = rand();
					
						$ids_selected=array_keys($_SESSION['choice']);
						foreach ( $ids_selected as $entry_selected)
						{
							$file_fasta = 'FASTA_FILES/'.$entry_selected.'.fasta';
							$file_fasta_all = "TEMP_FTP_FILES/".$add.".fasta";
						        $create_fasta = `cat $file_fasta >>$file_fasta_all`;chmod($file_fasta_all,0777);
						
						}
						$zip_fasta=`gzip $file_fasta_all`;
						$final_fasta=$file_fasta_all.'.gz';
						
						$file_xml_all = "TEMP_FTP_FILES/".$add.".xml";
						$start_xml='&lt;?xml version="1.0" encoding="utf-8"?&gt;'."\
"."&lt;Entries&gt;\
";
						$fh = fopen($file_xml_all, 'w');
						fwrite($fh,$start_xml);
						fclose($fh);
						foreach ( $ids_selected as $entry_selected)
						{				
						        $file_xml = 'XML_DOWN_FILES/'.$entry_selected.'.xml';
						        $holdcontents = file_get_contents($file_xml);
							$fh2 = fopen($file_xml_all, 'a');
						        fwrite($fh2,$holdcontents);
						}
						$end_xml = "&lt;/Entries&gt;\
";
						fwrite($fh2,$end_xml);
						fclose($fh2);chmod($file_xml_all,0777);
						$zip_xml=`gzip $file_xml_all`;
						$final_xml=$file_xml_all.'.gz';
						
						foreach ( $ids_selected as $entry_selected)
						{
							$file_flat = 'FLAT_FILES/'.$entry_selected.'.flat';
						        $file_flat_all = "TEMP_FTP_FILES/".$add.".flat";
						        $create_flat = `cat $file_flat>>$file_flat_all`; chmod($file_flat_all,0777);
						}	
						$zip_flat=`gzip $file_flat_all`;	
						$final_flat=$file_flat_all.'.gz';
						
						if(file_exists($final_fasta) && file_exists($final_flat) && file_exists($final_xml))
						{
							echo "&lt;tr&gt;&lt;td class=\\"headcell\\"&gt;Download selected entries&lt;/td&gt;&lt;/tr&gt;\
";
							echo "&lt;tr&gt;&lt;td class=\\"searches_cell\\"&gt;&lt;br&gt;Select one of the following formats:&lt;/td&gt;&lt;/tr&gt;\
";
						 	echo "&lt;tr&gt;&lt;br&gt;&lt;td&gt;";
						        echo "&lt;ul&gt;".
						             "&lt;li&gt;&lt;a href=\\"".$final_fasta."\\"&gt;FASTA format&lt;/a&gt;&lt;/li&gt;".
						             "&lt;li&gt;&lt;a href=\\"".$final_flat."\\"&gt;Text format&lt;/a&gt;&lt;/li&gt;".
						             "&lt;li&gt;&lt;a href=\\"".$final_xml."\\"&gt;XML format&lt;/a&gt;&lt;/li&gt;".
						             "&lt;/ul&gt;".
						             "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;\
";
						}
				        }
				        else
				        {
				     		echo 	"&lt;div align=\\"center\\"&gt;&lt;table&gt;".
				         		"&lt;tr&gt;&lt;td&gt;&lt;div align=\\"center\\"&gt;".
				         		"You selected ".$counter." entries for download, which is more than the allowed limit of 1000. &lt;br&gt;For large barch searches, please download and search the total database files from the &lt;a target=_blank href = \\"download.php\\"&gt;download&lt;/a&gt; page. ".
				         		"&lt;p&gt;Go &lt;a href=\\"javascript: window.history.go(-1);\\"&gt;back&lt;/a&gt; to selection list&lt;/p&gt;"; exit;
				        }
					echo "&lt;p&gt;Go &lt;a href=\\"javascript: window.history.go(-1);\\"&gt;back&lt;/a&gt; to selection list&lt;/p&gt;";
					exit;
				}
				/*&lt;--- Download raw entries in zip form END ---&gt;*/

 				function cleanQuery($string)
				{
					  if (get_magic_quotes_gpc())  // prevents duplicate backslashes
					  { $string = stripslashes($string); }

					  $string = mysql_real_escape_string($string);
					  $trimmed = trim($string);
					  $final=str_replace("%","\\%",$trimmed);
					  return $final;
				}

           	 		$name=$gene=$organism=$taxonomy=$references=$transmembrane="";
            			$family=0;
				$coverage=0;
            			$combination='combined';
            			$frag='';
				
    				if (isset($_REQUEST['prot_name'])) 					{$name = cleanQuery($_REQUEST['prot_name']);}
				if (isset($_REQUEST['prot_gene'])) 					{$gene = cleanQuery($_REQUEST['prot_gene']);}
				if (isset($_REQUEST['org_name']))  					{$organism = cleanQuery($_REQUEST['org_name']);}
				if (isset($_REQUEST['taxid']))     					{$taxonomy = cleanQuery($_REQUEST['taxid']);}
				if (isset($_REQUEST['crossref']))  					{$references = cleanQuery($_REQUEST['crossref']);}
				if (isset($_REQUEST['pmid']))  						{$references_pmid = cleanQuery($_REQUEST['pmid']);}
				if (isset($_REQUEST['operator']))  					{$combination = $_REQUEST['operator'];}
				if (isset($_REQUEST['fragment']))  					{$frag='Y';}
				if (isset($_REQUEST['family']))    					{$family = $_REQUEST['family'];}
				if (isset($_REQUEST['signal']))    					{$signal = $_REQUEST['signal'];}

		        	if ( !$name && !$gene && !$organism && !$taxonomy && !$references && !$references_pmid && $family==0 && $signal=="ALL")
		        	{
		            	echo 	"&lt;div align=\\"center\\"&gt;&lt;table&gt;".
		                 	"&lt;tr&gt;&lt;td&gt;&lt;div align=\\"center\\"&gt;".
		                 	"&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;No values in the text fields specified. ".
		                 	"Please go &lt;a href = \\"text_search.php\\"&gt;back&lt;/a&gt; to text search page.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/td&gt;&lt;/div&gt;&lt;/tr&gt;"; exit;
		        	}

		        	else
		        	{
		                	$wclause = array();
	
		                	if($name!="")  	
		   		   	{
						if(strstr($name, ',', true))
						{
							$array_name = explode(",", $name);
							$count_name = count ($array_name);
							
							if ($count_name&gt;5) {print "&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;You specified more than 5 terms in one or more text fields. Please go &lt;a href = \\"text_search.php\\"&gt;back&lt;/a&gt; to text search page and review your submission.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;";}
							$all_name='';
							foreach ($array_name as $value_name) { $value_name=trim($value_name); $all_name = $all_name."protein.protein_name LIKE '%".$value_name."%' OR "; }
							$final_name_addition = substr($all_name, 0, -4);   // remove the ' OR ' in the end
							
							$wclause[]= $final_name_addition;
 						}
						
						else
						{ $wclause[]="protein.protein_name LIKE '%".$name."%' "; }
					}

		   			if($gene!="")
					{
						if(strstr($gene, ',', true))
						{
							$array_genes = explode(",", $gene);
							$count_genes = count ($array_genes);
							
							if ($count_genes&gt;5) {print "&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;You specified more than 5 terms in one or more text fields. Please go &lt;a href = \\"text_search.php\\"&gt;back&lt;/a&gt; to text search page and review your submission.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;";}
							$all_genes='';
							foreach ($array_genes as $value_genes) { $value_genes=trim($value_genes); $all_genes = $all_genes."protein.protein_gene LIKE '%".$value_genes."%' OR "; }
							$final_genes_addition = substr($all_genes, 0, -4);   // remove the ' OR ' in the end
							
							$wclause[]= $final_genes_addition;
 						}
						
						else
						{ $wclause[]="protein.protein_gene LIKE '%".$gene."%' "; }
					}

		               	 	if($organism!="")
		   		   	{
						if(strstr($organism, ',', true))
						{
							$array_organism = explode(",", $organism);
							$count_organism = count ($array_organism);
							
							if ($count_organism&gt;5) {print "&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;You specified more than 5 terms in one or more text fields. Please go &lt;a href = \\"text_search.php\\"&gt;back&lt;/a&gt; to text search page and review your submission.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;";}
							$all_organism='';
							foreach ($array_organism as $value_organism) { $value_organism=trim($value_organism); $all_organism = $all_organism."protein.protein_organism LIKE '%".$value_organism."%' OR "; }
							$final_organism_addition = substr($all_organism, 0, -4);   // remove the ' OR ' in the end
							
							$wclause[]= $final_organism_addition;
 						}
						
						else
						{ $wclause[]="protein.protein_organism LIKE '%".$organism."%' "; }
					}
		
		                	if($taxonomy!="")
		   		   	{
						if(strstr($taxonomy, ',', true))
						{
							$array_taxonomy = explode(",", $taxonomy);
							$count_taxonomy = count ($array_taxonomy);
							
							if ($count_taxonomy&gt;5) {print "&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;You specified more than 5 terms in one or more text fields. Please go &lt;a href = \\"text_search.php\\"&gt;back&lt;/a&gt; to text search page and review your submission.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;";}
							$all_taxonomy='';
							foreach ($array_taxonomy as $value_taxonomy) { $value_taxonomy=trim($value_taxonomy); $all_taxonomy = $all_taxonomy."protein.protein_ncbi ='".$value_taxonomy."' OR "; }
							$final_taxonomy_addition = substr($all_taxonomy, 0, -4);   // remove the ' OR ' in the end
							
							$wclause[]= $final_taxonomy_addition;
 						}
						
						else
						{ $wclause[]="protein.protein_ncbi ='".$taxonomy."' "; }
					}
		 	
		                	if($references!="")
		   		   	{
						if(strstr($references, ',', true))
						{
							$array_references = explode(",", $references);
							$count_references = count ($array_references);
							
							if ($count_references&gt;5) {print "&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;You specified more than 5 terms in one or more text fields. Please go &lt;a href = \\"text_search.php\\"&gt;back&lt;/a&gt; to text search page and review your submission.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;";}
							$all_references='';
							foreach ($array_references as $value_references) { $value_references=trim($value_references); $all_references = $all_references."reference.reference_code ='".$value_references."' OR "; }
							$final_references_addition = substr($all_references, 0, -4);   // remove the ' OR ' in the end
							
							$wclause[]= $final_references_addition;
 						}
						
						else
						{ $wclause[]="reference.reference_code ='".$references."' "; }
					}
		   						
		                	if($references_pmid!="")
		   		   	{
						if(strstr($references_pmid, ',', true))
						{
							$array_references_pmid = explode(",", $references_pmid);
							$count_references_pmid = count ($array_references_pmid);
							
							if ($count_references_pmid&gt;5) {print "&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;You specified more than 5 terms in one or more text fields. Please go &lt;a href = \\"text_search.php\\"&gt;back&lt;/a&gt; to text search page and review your submission.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;";}
							$all_references_pmid='';
							foreach ($array_references_pmid as $value_references_pmid) { $value_references_pmid=trim($value_references_pmid); $all_references_pmid = $all_references_pmid."families.families_pubmed LIKE '%".$value_references_pmid."%' OR "; }
							$final_references_pmid_addition = substr($all_references_pmid, 0, -4);   // remove the ' OR ' in the end
							
							$wclause[]= $final_references_pmid_addition;
 						}
						
						else
						{ $wclause[]="families.families_pubmed LIKE '%".$references_pmid."%' "; }
					}
		   						
		                	if($family&gt;0)   	{ $wclause[]="protein.protein_families_id ='".$family."' "; }	

					if($signal!="ALL" && $signal!="")  	{ $wclause[]="protein.protein_sp_quality ='".$signal."' "; }

					if($frag == "N") 	{ $wclause[]="protein.protein_isfragment ='".$frag."' "; }
		                	
					switch($combination)
				        {
				        	case "combined":
				                $wclause_sql = implode(" AND ", $wclause); break;
				
				                case "separate":
				                $wclause_sql = implode(" OR ", $wclause); break;
				        }

					//if(isset($_POST['sql']))
					//{
					//$query_total = "SELECT DISTINCT protein.protein_id, protein.protein_name, ".
					//		"protein.protein_seq_len, protein.protein_organism, protein_ncbi FROM protein ".
 					//		"LEFT JOIN protein_reference ON protein_reference.prot_ref_protein_id = protein.protein_id ".
					//		"LEFT JOIN reference ON reference.reference_id = protein_reference.prot_ref_reference_id ".
					//		"LEFT JOIN families ON protein.protein_families_id = families.families_id ".
	 			     	//		"WHERE ".$_POST['sql'];
					//}

					//else
					//{
					$query_total = 	"SELECT DISTINCT protein.protein_id, protein.protein_name, ".
							"protein.protein_seq_len, protein.protein_organism, protein_ncbi FROM protein ".
 							"LEFT JOIN protein_reference ON protein_reference.prot_ref_protein_id = protein.protein_id ".
							"LEFT JOIN reference ON reference.reference_id = protein_reference.prot_ref_reference_id ".
							"LEFT JOIN families ON protein.protein_families_id = families.families_id ".
	 			     			"WHERE ".$wclause_sql;
				     	//}		
		                	
					print "QUERY: $query_total&lt;br&gt;";
					
					$text_result = mysql_query($query_total) or die("&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;Non-acceptable query. Please go &lt;a href = \\"text_search.php\\"&gt;back&lt;/a&gt; to text search page.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/td&gt;&lt;/div&gt;&lt;/tr&gt;");
					$number_results = mysql_num_rows($text_result);
					
					if ($number_results==0)
					{ echo "&lt;tr&gt;&lt;td colspan=\\"5\\"&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;i&gt;No records matching your query were found&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/i&gt;&lt;/td&gt;&lt;/tr&gt;\
"; }

					else
				        {
						// number of rows to show per page
						$rowsperpage = 25;
						// find out total pages
						$totalpages = ceil($number_results / $rowsperpage);

						// get the current page or set a default
						if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
							// cast var as int
							$newpage = (int)$_GET['currentpage'];
						} else {
							// default page num
							$newpage = 1;
						} // end if

						// modify the requested page based on any pagination form buttons, set/clear any remembered session checkbox data
						$page = isset($_POST['page']) ? trim($_POST['page']) : false;
						if($page){
							// values of &lt;&lt;, &lt;, x, &gt;, or &gt;&gt;
							switch($page){
								case '&lt;&lt; First':
									$newpage = 1;
								break;
								case '&lt; Previous':
									$newpage--;
								break;
								case 'Next &gt;':
									$newpage++;
								break;
								case 'Last &gt;&gt;':
									$newpage = $totalpages;
								break;
								default:
									// not one of the symbols, should be a number
									$page = intval($page);
									if($page &gt; 0){
										$newpage = $page;
									}
								break;
							}
							// set or clear the state of the check boxes in $_SESSION['choice']
							// $_SESSION['ids'] = array of ids on the page that submitted
							foreach($_SESSION['ids'] as $key){
								if(isset($_POST['choice'][$key])){
									$_SESSION['choice'][$key] = 1;
								} elseif (isset($_SESSION['choice'][$key])) {
									unset($_SESSION['choice'][$key]);
								}
							}
							header("location: {$_SERVER['SCRIPT_NAME']}?currentpage=$newpage"); // clear post data
							exit;
						}

						// if current page is greater than total pages...
						if ($newpage &gt; $totalpages) {
							// set current page to last page
							$newpage = $totalpages;
						} // end if
						// if current page is less than first page...
						if ($newpage &lt; 1) {
							// set current page to first page
							$newpage = 1;
						} // end if

						/******  build the pagination links ******/
						// range of num links to show
						$range = 3;
						$links = '';
						// if not on page 1, don't show back links
						if ($newpage &gt; 1) {
							// show &lt;&lt; link to go back to page 1
							$links .= "&lt;input type='submit' name='page' value='&lt;&lt; First'&gt;";
							// show &lt; link to go back 1 page
							$links .= "&lt;input type='submit' name='page' value='&lt; Previous'&gt;";
						} // end if

						// loop to show links to range of pages around current page
						for ($x = ($newpage - $range); $x &lt; (($newpage + $range) + 1); $x++) {
							// if it's a valid page number...
							if (($x &gt; 0) && ($x &lt;= $totalpages)) {
								// if we're on current page...
								if ($x == $newpage) {
									// 'highlight' it but don't make a link
									$links .= " [&lt;b&gt;$x&lt;/b&gt;] ";
									// if not current page...
								} else {
									$links .= "&lt;input type='submit' name='page' value='$x'&gt;";
								} // end else
							} // end if
						} // end for

						// if not on last page, show forward and last page links
						if ($newpage != $totalpages) {
							// echo forward link for next page
							$links .= "&lt;input type='submit' name='page' value='Next &gt;'&gt;";
							// echo forward link for lastpage
							$links .= "&lt;input type='submit' name='page' value='Last &gt;&gt;'&gt;";
						} // end if
						/****** end build pagination links ******/

						/****** get the actual database content for the requested page ******/
						// the offset of the list, based on current page
						$offset = ($newpage - 1) * $rowsperpage;

						/* LIMITED SQL QUERY */
						$query_limited = $query_total." LIMIT $offset, $rowsperpage";
						$result_limited = mysql_query($query_limited);

						echo "&lt;form name=\\"srs_form\\" action='?currentpage=$newpage' method='post'&gt;";
				
						$counter_color=0;
						echo 	"&lt;tr&gt;&lt;td class=\\"headcell\\" colspan=\\"6\\"&gt;Text search results&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;\
";
						echo 	"&lt;tr&gt;\
";
						echo	"&lt;td width=\\"10%\\" class=\\"browse_cell\\"&gt;Select entries&lt;/td&gt;".
							"&lt;td width=\\"15%\\" class=\\"browse_cell\\"&gt;View Entry&lt;/td&gt;".
						        "&lt;td width=\\"30%\\" class=\\"browse_cell\\"&gt;Description&lt;/td&gt;".
						        "&lt;td width=\\"33%\\" class=\\"browse_cell\\"&gt;Organism&lt;/td&gt;&lt;td width=\\"12%\\" class=\\"browse_cell\\"&gt;Length&lt;/td&gt;";
						echo 	"&lt;/tr&gt;\
";

						$ids_this_page = array(); // a list of the checkbox id's on this page
						while ($list = mysql_fetch_assoc($result_limited))
						{
							echo "&lt;tr&gt;\
";
						        $counter++;
						        if ($counter % 2) {$colour_class="browse_cell_change1";}
						        else    	  {$colour_class="browse_cell_change2";}
						
						        $code = $list['protein_id'];
						        $name_cell = $list['protein_name'];
						        $length_cell = $list['protein_seq_len'];
						        $organism_cell = $list['protein_organism'];
							$taxonomy_cell = $list['protein_ncbi'];

							$checked = isset($_SESSION['choice'][$list['protein_id']]) ? " checked='checked'" : '';
							$ids_this_page[] = $list['protein_id'];
							echo "&lt;td width=\\"10%\\" class=".$colour_class."&gt;&lt;input type='checkbox' name='choice[{$list['protein_id']}]' value=".$list['protein_id']."&gt;&lt;br /&gt;";
							 echo "&lt;td width=\\"15%\\"  class=".$colour_class."_left&gt;&lt;a href=\\"entry.php?code=".$code."\\"&gt;DBID: ".$code."&lt;/a&gt;";
						
						        echo "&lt;td width=\\"30%\\" class=".$colour_class."_left&gt;".$name_cell."&lt;/td&gt;\
";
						        echo "&lt;td width=\\"33%\\" class=".$colour_class."_left&gt;&lt;a href=\\"retrieve_text.php?taxid=".$taxonomy_cell."\\"&gt;".$organism_cell."&lt;/a&gt;";
						        echo "&lt;td width=\\"12%\\" class=".$colour_class."&gt;".$length_cell."&lt;/td&gt;\
";
						        echo "&lt;/tr&gt;\
";
						}
						$_SESSION['ids'] = $ids_this_page;
						$total_selected=count($_SESSION['choice']);
						
						echo "&lt;tr&gt;&lt;td&gt;&lt;br&gt;&lt;input type = \\"submit\\" value = \\"Retrieve\\"&gt;&lt;/td&gt;";
		 				echo "&lt;td colspan='4'&gt;&lt;br&gt;".$links."&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;";
						
						echo "&lt;tr&gt;&lt;td&gt;&lt;input type=\\"hidden\\" name=\\"sql\\" value=\\"".$wclause_sql."\\"&gt;&lt;/td&gt;&lt;/tr&gt;";
						echo "&lt;/form&gt;";
					}
				}
				?&gt;
				&lt;/tbody&gt;
         		&lt;/table&gt;
			&lt;/div&gt;
         		&lt;/td&gt;
         	&lt;/tr&gt;
  	&lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

My problem is that, although the state of the selected entries is maintained as the user browses from page to page, this is NOT the
case for the $wclause_sql part, which is actually the “WHERE” clause of the sql query. So I can only see the first page, and not the others…

I used sessions in order to keep track which records are selected by the user, so as when the user finally hits ‘Retrieve’
he can get all the desired entries, but I cannot transfer the $wclause and therefore, in all other pages except page 1, the SQL query is empty, thus no
entries are shown.

What am I missing here?

that’s… quite the wordy pagination.
Either use the session to hold the where clause, and then call for it at the top of the script (before your search boxes would overwrite it), or rework your pagination to send actual form tags instead of just a solitary input tag, and put a hidden field into the form.