I have a paging problem.
My problem is when i search for a school, the first page will display correctly the schools . But when i click on other pages, it will display no data. Is it because i did not carry the variable of what i search for to other pages? Does any one know what is causing the problems?
I used the pagination tutorial example on : http://www.php-mysql-tutorial.com/php-mysql-paging-2.php?PHPSESSID=be9783bb9ad69b8014e02e4d92271e6c
code:
<html>
<head>
<title>Implementing Paging with next and prev</title>
</head>
<body>
<?php
$schoolname= $POST['schoolname'];
$schooltype= $POST['schooltype'];
$schoolgrade= $POST['schoolgrade'];
$schollgender= $POST['schollgender'];
@ $db = mysql_pconnect("localhost", "nobody", "");
if (!$db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}
mysql_select_db("schools");
// how many rows to show per page
$rowsPerPage = 3;
// by default we show first page
$pageNum = 1;
// if $GET['page'] defined, use it as page number
if(isset($GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * 3;
if ($schooltype=='Any')
{
$schooltype='%%';
}
if ($schoolgrade=='Any')
{
$schoolgrade='%%';
}
if ($schollgender=='Any')
{
$schollgender='%%';
}
$query = "select * from schools where school like '%$schoolname%' and type like '$schooltype' and grade like '$schoolgrade' and gender like '$schollgender' limit $offset, 3";
//echo $query;
$result = mysql_query($query) or die('Error, query failed');
// print the random numbers
// Display the data
echo"<table border=1 cellpadding=0 cellspacing=0 bordercolor=#111111>";
echo"<TR><TD width=50%> <B>School Name</B><TD width=20%><B>School Type</B><TD width=20%><B>Grade</B><TD><B>Gender</B></TR>";
while ( $row = mysql_fetch_array($result) ) {
echo ("<tr>");
$school=$row["school"];
$reportlink=$row["reportlink"];
//echo("<td>" . $row["id"] . "</td>");
//echo("<td>" . $row["school"] . "</td>");
echo "<td> <a href=\"$reportlink\">$school</a></td>";
echo("<td>" . $row["type"] . "</td>");
echo("<td>" . $row["grade"] . "</td>");
echo("<td>" . $row["gender"] . "</td>");
//echo("<td>" . $row["reportlink"] . "</td>");
echo ("</tr>");
}
echo("</table>");
// how many rows we have in database
$query = "SELECT * FROM schools where school like '%$schoolname%' and type like '$schooltype' and grade like '$schoolgrade' and gender like '$schollgender'";
//echo $query;
$result = mysql_query($query) or die('Error, query failed');
$numrows = mysql_num_rows($result);
echo "<br>";
// how many pages we have when using paging?
$maxPage = ceil($numrows/3);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
?>
</body>
</html>