<?php
// ============
// Show entries
// ============
// how many entries to show per page
$rowsPerPage = 6;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use the value as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset ( where to start fetching the entries )
$offset = ($pageNum - 1) * $rowsPerPage;
// prepare the query string
$query = "SELECT id, title, post, date ".
"FROM jcb_mainMenu ".
"WHERE sectionID = 'About Me'".
"ORDER BY id DESC ". // using ORDER BY to show the most current entry first
"LIMIT $offset, $rowsPerPage"; // LIMIT is the core of paging
// execute the query
$result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
// if the page is empty show a message
if(mysql_num_rows($result) == 0)
{
?>
<p>This Page Is Empty!</p>
<?php
}
else
{
// get all entries
while($row = mysql_fetch_array($result))
{
// list() is a convenient way of assign a list of variables
// from an array values
list($id, $title, $post, $date) = $row;
?>
<span class="mainMenu"><?=$post;?><br /><br /></span>
<hr/>
<?php
} // end while
// below is the code needed to show page numbers
// count how many rows we have in database
$query = "SELECT COUNT(id) AS numrows FROM jcb_mainMenu";
$result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
$nextLink = '';
// show the link to more pages ONLY IF there are
// more than one page
if($maxPage > 1)
{
// this page's path
$self = $_SERVER['PHP_SELF'];
// we save each link in this array
$nextLink = array();
// create the link to browse from page 1 to page $maxPage
for($page = 1; $page <= $maxPage; $page++)
{
$nextLink[] = "<a href=\"$self?page=$page\">$page</a>";
}
// join all the link using implode()
$nextLink = "Page : " . implode(' » ', $nextLink);
}
// close the database connection since
// we no longer need it
?>
<p align="center"><?=$nextLink;?></p>
<?php
}
?>