Thanks to tons of great help by some of you one here, I've finally been successful in my first pagination project...well almost.
My script works wonderfully except one tiny hitch. There are 40 "rows' page. When there are no more "rows" to display, the pagination continues to paginate blank pages.
To be more specific. page 13 is the last page with rows on it, but you can still hit next and go onto blank pages.
The solution is obvious - to have the "next" button disappear if there are no rows left, but for some reason all of the if statements I've tried are failing to do so.
Here is the query:
$rowsperpage = 40; // THERE ARE 40 AIRWAVES PER PAGE
$currentpage = (int)$_GET['currentpage']; // This is getting which "page" the user wants to see, and putting it in $currentpage
if ($currentpage > 0) { // If $currentpage is greater than 0, we'll need to compensate by subtracting 1, so that we pull the correct set of results. Otherwise, we'll just start at 0 (see the else)
$offset = ($currentpage - 1) * $rowsperpage;
}
else {
$offset = 0;
}
$query = "SELECT * FROM `CysticAirwaves开发者_开发知识库` WHERE `FromUserID` = `ToUserID` AND `status` = 'active' ORDER BY `date` DESC, `time` DESC LIMIT $offset, $rowsperpage";
$request = mysql_query($query, $connection);
$counter = 0;
while ($result = mysql_fetch_array($request)) {
and here is the pagination links:
// find out how many rows are in the table
$query = "SELECT COUNT(*) FROM `CysticAirwaves`";
$result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 40;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int)$_GET['currentpage'];
}
else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage + 1) * $rowsperpage;
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
// echo data
echo $list['id'] . " : " . $list['number'] . "<br />";
} // end while
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
$prevpage = $currentpage - 1;
echo "<div id='all_page_turn'>
<ul>
<li class='PreviousPageBlog round_10px'>
<a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$prevpage'>Previous</a>
</li>
<ul>
</div>";
} // end if
// if not on last page, show forward and last page links
if ($currentpage != $totalpages)) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <div id='all_page_turn'>
<ul>
<li class='PreviousPageBlog round_10px'>
<a href='http://www.cysticlife.org/Airwave_build.php?currentpage=$nextpage'>Next</a>
</li>
</ul>
</div> ";
}
Thanks in advance
Change your
if ($currentpage != $totalpages)) {
to
if ($currentpage < $totalpages) {
For one, there's an extra parenthesesisesis, and you should never rely on exact matching an upper threshold.
Edit:
I think I see the problem. You need to use the same WHERE clause as in your actual select query when doing your COUNT:
$query = "SELECT COUNT(*) FROM `CysticAirwaves` " .
"WHERE `FromUserID` = `ToUserID` AND `status` = 'active'";
Otherwise, you're getting the count of all rows in the table, even ones you're not displaying.
精彩评论