'm working on paging the results.. However, I got this problem that i can't solve.. I'm able to limit the results to 10 per page, but when i click on next in hope to display the other 10 results.. the whole page refreshes.. and all the values are gone.. That is to say.. it can't display the following results.. The other thing is.. when i search on field that would return only 2 results.. why is it that it displays page 1 page 2 page 3 links.. together with next and last page..
if im not wrong its due to this query..
$query = "SELECT COUNT(*) AS numrows FROM Report";
$resultAircraftDisplay = mysql_query($query)
or die("couldn't execute query.");
$row = mysql_fetch_array($resultAircraftDisplay, MYSQL_ASSOC);
$numrows = $row['numrows'];
how should i only count the number of rows of the results... just for your info.. i'm doing a optional search engine.. user can choose to do a search on any fields he like.. Any help would be appreciated..
// how many rows to show per page
$rowsPerPage = 10;
// 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) * $rowsPerPage;
$queryAircraftDisplay = "SELECT * from report where reportID != 0";
if (!empty($_GET['AircraftType']))
{
$queryAircraftDisplay .= " and aircraftType='".$_GET['AircraftType']."' ";
}
if (!empty($_GET['Keyword']))
{
$queryAircraftDisplay .= " and reportID in ($newarr)";
}
echo $queryAircraftDisplay;
$queryAircraftDisplay .= "LIMIT $offset, $rowsPerPage";
$resultAircraftDisplay = mysql_query($queryAircraftDisplay) or die ("couldn't execute query ".mysql_error());
$num= mysql_numrows($resultAircraftDisplay);
$query = "SELECT COUNT(*) AS numrows FROM Report";
$resultAircraftDisplay = mysql_query($query)
or die("couldn't execute query1.");
$row = mysql_fetch_array($resultAircraftDisplay, MYSQL_ASSOC);
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
// 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> ";
}
}
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;
?>