One thing I hate doing in PHP, to this very day... pagination (and another is calenders). This is my first "real" attempt at pagination. The code gets the data from mysql correctly, displays things as it is supposed to BUT say there are an even number of records (4) returned, it displays a "Next" link, which when clicked doesn't return anything since there are no more rows, but if there is an odd number of records (5), everythhing works. How can I fix it so that this doesn't happen for even number of results? Or better yet, how can I improve the code? Thank you for your patience as you read through it:
function list_previews()
{
global $STAFF;
global $ERROR;
//Set max results per page
$max = $this->max_records;
//Total records in table
$total_records = mysql_num_rows(mysql_query("SELECT id FROM previews WHERE authorid = '" . $_SESSION['authorid'] . "'"));
//Create next and previous vars
if(!isset($_GET['p']))
{
$prev = 0;
$next = $max + $max;
}
else
{
$prev = $_GET['p'] - $max;
$next = $_GET['p'] + $max;
}
$rows_from = $prev;
//Get order by vars
if(!isset($_GET['od']) || $_GET['od'] == NULL)
{
$od = 'ASC';
$od_num = 2;
}
else
{
if($_GET['od'] == 1)
{
$od = 'ASC';
$od_num = 2;
}
elseif($_GET['od'] == 2)
{
$od = 'DESC';
$od_num = 1;
}
}
$sql = "SELECT ";
$sql.= "previews.id, previews.gid, DATE_FORMAT(previews.date_previewed, '%m/%d/%Y') AS date, games.title ";
$sql.= "FROM ";
$sql.= "previews, games ";
$sql.= "WHERE ";
$sql.= "previews.authorid = '" . $_SESSION['authorid'] . "' AND games.id = previews.gid ";
$sql.= "ORDER BY ";
$sql.= "games.title ";
$sql.= $od . " ";
$sql.= "LIMIT " . $rows_from . ", " . $max;
$query = mysql_query($sql);
//If the query didn't work
if(!$query)
{
$ERROR->db_error(3);
$ERROR->query = $sql;
error_mail('mysql', 'Failed to get previews to list');
echo $ERROR->message;
}
else
{
if(mysql_num_rows($query) > 0)
{
echo '<p>';
echo 'Now displaying all previews made by you. Showing ' . $max . ' previews per page.';
echo '</p>';
//total rows returned
$total_rows = mysql_num_rows($query);
//Get results
echo '<div style="width: 80%; padding: 3px;">';
echo '<span style="float: left;"><strong><a href="' . $_SERVER['PHP_SELF'] . '?act=previews&code=03&od=' . $od_num;
echo '">Preview Title (Game)</a></strong></span>';
echo '<span style="float: right;"><strong>Preview Date</strong></span>';
echo '</div>';
echo '<table width="80% align="middle" style="border: 1px dashed #999999;" cellpadding="3">' . "\n";
while($row = mysql_fetch_array($query))
{
echo '<tr>' . "\n";
echo '<td align="left" width="90%">' . stripslashes($row['title']) . '</td>' . "\n";
echo '<td align="right" width="10%">' . stripslashes($row['date']) . '</td>' . "\n";
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
//Make navigation
if($prev == 0)
{
if($total_records < $max)
{
$link1 = NULL;
$link2 = NULL;
}
else
{
$link1 = NULL;
$link2 = '<a href="' . $_SERVER['PHP_SELF'] . '?act=previews&code=03&p=' . $next . '&od=' . $_GET['od'] . '">Next</a>';
}
}
elseif(mysql_num_rows($query) < $max)
{
$link1 = '<a href="' . $_SERVER['PHP_SELF'] . '?act=previews&code=03&p=' . $prev . '&od=' . $_GET['od'] . '">Previous</a>';
$link2 = NULL;
}
else
{
$link1 = '<a href="' . $_SERVER['PHP_SELF'] . '?act=previews&code=03&p=' . $prev . '&od=' . $_GET['od'] . '">Previous</a>';
$link2 = '<a href="' . $_SERVER['PHP_SELF'] . '?act=previews&code=03&p=' . $next . '&od=' . $_GET['od'] . '">Next</a>';
}
//Show pages
$num_pages = round($total_records / $max);
if($num_pages > 1)
{
//Show Link
echo '<p>' . $link1 . ' (';
//Build pages
for($n = 1; $n <= $num_pages; $n++)
{
echo ' - ';
//If the $n is the selected page
if($n == ($_GET['p'] / $max))
{
echo $n;
}
//Otherwise
else
{
echo '<b><a href="' . $_SERVER['PHP_SELF'] . '?act=previews&code=03&p=' . $max * $n . '&od=' . $_GET['od'] . '">' . $n . '</a></b>';
}
if ($n == $num_pages)
{
echo ' - ';
}
}
echo ') ' . $link2 . '</p>';
}
else
{
echo '<p>Only one page of previews are available.</p>';
}
}
else
{
//ERROR!
echo '<p>';
echo 'There are no previews available for you to view.';
echo '</p>';
}
}
}