PHP Pagination

PHP Pagination

Here is some php code which could be used to paginate rss feeds or database queries.

<div class="myBody">
<ul>
<?php

if (!(isset($page_number))) {
$page_number = 1;
}

//Need to get the page number
$page_number=$_GET['pagenumber'];

$command = "select table1.category_id, table1.seo_fname, table2.post_id, table2.6, table2.post_date, table2.2, table2.5, table2.8, table2.9, table2.15 from cats as table1, p_table as table2 WHERE table1.category_id=table2.6 AND (table2.2 LIKE '$keywords%' OR table2.2 LIKE '%$keywords%' OR table2.5 LIKE '%$keywords%') ORDER by table2.post_date DESC";
$result = mysqli_query($db, $command);

$rows = mysqli_num_rows($result);
//command below tells us how many entries there actually are
//echo $rows;

//how many entries we want
$number_entries = 3;
//echo $number_entries;
//This tells us the page number of our last page
$last_page = ceil($rows/$number_entries);

//this makes sure the page number isn't below one, or more than our maximum pages

if ($page_number < 1) {
$page_number = 1;
}
elseif ($page_number > $last_page) {
$page_number = $last_page;
}

//This is the query. It could be any query
$command2 = "select table1.category_id, table1.seo_fname, table2.post_id, table2.6, table2.post_date, table2.2, table2.5, table2.8, table2.9, table2.15 from cats as table1, part_table as table2 WHERE table1.category_id=table2.6 AND (table2.2 LIKE '$string%' OR table2.2 LIKE '%$string%' OR table2.5 LIKE '%$string%')";

//This sets the range to display in our query
//$command2 .= " LIMIT ".(($page_number - 1) * 4).",4";
$command2 .= 'limit ' .($page_number - 1) * $number_entries .',' .$number_entries;

//This is where you display your query results
$result2 = mysqli_query($db, $command2);
while($row = mysqli_fetch_array($result2)) {

$even_odd = ( 'odd' != $even_odd ) ? 'odd' : 'even';
?>
<div style="display:inline-block;">
<li style="min-height:80px; width:99%; height:100%;" class="myRow <?php echo $even_odd; ?>">
<h4><?php echo "<a href=/jobs/job/$row[post_id] title='$row[2]' >$row[2]</a>"; ?></h4>
<div style="width:600px; display:inline-block; "><p class="myDesc" ><?php echo substr($row[5], 0, 120)."..."; ?></p></div>
<div style="display:inline-block;" class="mySource" ><span><?php echo "Name"; ?></span></div>
<div style="display:inline-block;" class="myDate"><?php echo date('l, jS<br/> F Y', strtotime($row[post_date]));?></div>
</li></div>
<div style="clear:both;"></div>
<?php

}

//starting with the first page, make a loop for all pages and links to those pages
for($i = 0; $i<$last_page; $i++) {
$page_link = $i + 1;
//create an array of links
$links_pages[] = " <a style=\"font-size:13px;\" href='http://localhost/page.php?pagenumber=".$page_link."' >".$page_link."</a>";
}

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.

if ($page_number == 1) {
}
else if ($page_number > 1)
{
echo " <a style=\"font-size:13px;\" href='{$_SERVER['PHP_SELF']}?pagenumber=1'> <<-First</a> ";
echo " ";

$previous_page = $page_number-1;

echo " <a style=\"font-size:13px;\" href='{$_SERVER['PHP_SELF']}?pagenumber=$previous_page'> <-Previous</a> "; ?>

<?php
}

if ($page_number == $last_page) {
}
else {
$next_page = $page_number+1;
echo "<a style=\"font-size:13px;\" href='{$_SERVER['PHP_SELF']}?pagenumber=$next_page'>Next -></a> ";
echo " ";
echo " <a style=\"font-size:13px;\" href='{$_SERVER['PHP_SELF']}?pagenumber=$last_page'>Last ->></a>";
}

// This shows the user what page they are on, and the total number of pages
echo "<span style=\"font-size:13px;\"> Page $page_number of $last_page</span>";
?>
<div class="mypageclass">
Page:
<?php
if (count($links_pages))  {
foreach($links_pages as $links){
echo $links;
}
}
else {
echo "1";
}
?>
</div>
</ul></div>