I'm trying to paginate some query results but when I click on "next" to go to the next set of results, the query fails. Here's what I'm using to pull the data from the database.
$sql="SELECT id, student_id, first_name, last_name, address, city, state, zip, DOB, gender, grade, aka FROM student WHERE last_name LIKE '".$_GET['last']."%' and current_student='yes' ORDER BY last_name LIMIT $offset, $rowsPerPage";
$conn = db_security_connect();
$result = mysql_query($sql,$conn);
$query = "SELECT COUNT(*) AS numrows FROM student WHERE last_name LIKE '".$_GET['last']."%' and current_student='yes'";
Here is my pagination code...
$oddrow=true;
for ($i = 0; $i < mysql_num_rows($result); $i++) {
$row_array = mysql_fetch_assoc($result);
if ($oddrow) {
$oddrow=false;
$classtype="evenrow";
} else {
$oddrow=true;
$classtype="oddrow";
}
echo '<tr class="'. $classtype. '">';
echo '<TD>'.$row_array['student_id'].'</a></TD>';
echo '<TD><a href="notes.php?id='.$row_array['id'].'">'.$row_array['last_name'].', '.$row_array['first_name'].'</a>';
if (!empty($row_array['aka'])){
echo ' aka ("'.$row_array['aka'].'")';
}
echo '<br>'.$row_array['address'].' '.$row_array['city'].', '.$row_array['state'].' '.$row_array['zip'].'</TD>';
echo '<TD>'.$row_array['DOB'].'</TD>';
echo '<TD>'.$row_array['gender'].'</TD>';
echo '<TD>'.$row_array['grade'].'</TD>';
echo("</TR>");
}
// how many rows we have in database ($query ran above)
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link
// print 'previous' link only if we're not
// on page one
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?".$urlVariable."&page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?".$urlVariable."&page=1\">[First Page]</a> ";
}
else
{
$prev = ' [Prev] '; // we're on page one, don't enable 'previous' link
$first = ' [First Page] '; // nor 'first page' link
}
// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?".$urlVariable."&page=$page\">[Next]</a> ";
$last = " <a href=\"$self?".$urlVariable."&page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' [Next] '; // we're on the last page, don't enable 'next' link
$last = ' [Last Page] '; // nor 'last page' link
}
// print the page navigation link
echo "<tr><td colspan=5><hr color=gray size=6></td></tr><tr><td><a href=\"index.php\">Return To Main Page</a></td><td colspan=2 align=center>";
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
echo "</td><td colspan=2> </td></tr>";
I'm thinking the problem may have something to do with the persistence of this variable...
$_GET['last']
This is being pulled from the search on the previous page.
Am I on the right track here? Does this maybe need to be a hidden form element.
As always, any and all help is appreciated.
Thanks,
Scott