PHP Pagination help needed

Hi,

I’ve recently started learning PHP thru different online sources. At the moment I’m trying on Pagination and I’ve come across a problem which I couldn’t solve myself yet. So I thought of posting it in your Forum with the hope of getting some help.!!

The problem is I’ve 4 variables in my search query. And when I do the search it shows the results, according to the search options and with pagination…upto that point everything is okay, just as I wanted. But when I click the link for “2nd” page or “next” of results it just loosing the query and instead printing randomly from the database, irrespective of query search.

Can you just give me an idea, what and where I have to make changes in the code, so that the rest of the pages also shows result according to the query.

Any help would be greatly appreciated.

Thank you

Here’s the full code

<? 
 
$page_name="paging1.php"; 
if(!isset($start)) { // This variable is set to zero for the first page 
$start = 0; 
} 
 
$eu = ($start -0); 
$limit = 5; // No of records to be shown per page. 
$this = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 
 
$name2=$_POST['name2']; 
$class2=$_POST['class2']; 
$mark2=$_POST['mark2']; 
$school2=$_POST['school2']; 
/////////////// WE have to find out the number of records in our table. We will use this to break the pages/////// 
 
$query2=" SELECT * FROM students WHERE (name LIKE '%$name2%') AND (class LIKE '%$class2%') AND (mark LIKE '%$mark2%') AND (school LIKE '%$school2%') "; 
$result2=mysql_query($query2); 
echo mysql_error(); 
 
$nume=mysql_num_rows($result2); 
echo"&nbsp;&nbsp; Number of results: $nume<br>"; 
 
/////////// Now let us print the table headers //////////////// 
$bgcolor="#f1f1f1"; 
$bgcolor2="#E2E2E2"; 
echo "<TABLE width=100% align=center cellpadding=0 cellspacing=0> <tr>"; 
echo "<td bgcolor='990000' >&nbsp;<font face='arial,verdana,helvetica' color='#ffffff' size='2'><b>Surname</b><br></font></td>"; 
echo "<td bgcolor='880000' >&nbsp;<font face='arial,verdana,helvetica' color='#ffffff' size='2'><b>Firstname</b></font></td>"; 
echo "<td bgcolor='990000' >&nbsp;<font face='arial,verdana,helvetica' color='#ffffff' size='2'><b>Class</b></font></td>"; 
echo "<td bgcolor='880000'>&nbsp;<font face='arial,verdana,helvetica' color='#ffffff' size='2'><b>Mark</b></font></td>"; 
echo "<td bgcolor='880000'>&nbsp;<font face='arial,verdana,helvetica' color='#ffffff' size='2'><b>School</b></font></td></tr>"; 
 
////////////// Now let us start executing the query with variables $eu and $limit set at the top of the page/////////// 
 
 
$query=" SELECT * FROM students WHERE (name LIKE '%$name2%') AND (class LIKE '%$class2%') AND (mark LIKE '%$mark2%') AND (school LIKE '%$school2%') limit $eu, $limit"; 
$result=mysql_query($query); 
echo mysql_error(); 
 
while($row = mysql_fetch_array( $result )) 
{ 
echo "<tr>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='arial,verdana,helvetica' size='2'><a href='stud-profile.php?name=$row[name]'>$row[name]</a> </font></td>"; 
echo "<td align=left bgcolor=$bgcolor2 id='title'>&nbsp;<font face='arial,verdana,helvetica' size='2'>$row[firstname]</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='arial,verdana,helvetica' size='2'>$row[class]</font></td>"; 
echo "<td align=left bgcolor=$bgcolor2 id='title'>&nbsp;<font face='arial,verdana,helvetica' size='2'>$row[mark]</font></td>"; 
echo "<td align=left bgcolor=$bgcolor2 id='title'>&nbsp;<font face='arial,verdana,helvetica' size='2'>$row[school]</font></td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
 
// how many rows we have in database 
$query = "SELECT COUNT(*) AS numrows FROM students WHERE students WHERE (name LIKE '%$name2%') AND (class LIKE '%$class2%') AND (mark LIKE '%$mark2%') AND (school LIKE '%$school2%') limit $eu, $limit"; 
$result = mysql_query($query) or die('Error, query failed'); 
$row = mysql_fetch_array($result, MYSQL_ASSOC); 
$nume = $row['numrows']; 
 
echo "<table align = 'center' width='50%'><tr><td align='left' width='30%'>"; 
if($back >=0) { 
print "<a href='$page_name?start=$back&name=$name1&class=$class2&mark=$mark2&school=$school2'><font face='Verdana' size='2'>PREV</font></a>"; 
} 
echo "</td><td align=center width='30%'>"; 
$i=0; 
$l=1; 
for($i=0;$i < $nume;$i=$i+$limit){ 
if($i <> $eu){ 
echo " <a href='$page_name?start=$i&name=$name1&class=$class2&mark=$mark2&school=$school2'><font face='Verdana' size='2'>$l</font></a> "; 
} 
else { echo "<font face='Verdana' size='4' color=red>$l</font>";} /// Current page is not displayed as link and given font color red 
$l=$l+1; 
} 
echo "</td><td align='right' width='30%'>"; 
if($this < $nume) { 
print "<a href='$page_name?start=$next&name=$name1&class=$class2&mark=$mark2&school=$school2'><font face='Verdana' size='2'>NEXT</font></a>"; 
} 
echo "</td></tr></table>"; 
 
?>