Jump to content

Page Numbers - Pagination


Dysan

Recommended Posts

I have come up with the following code, that limits the amounts of data displayed per page to only 2 records. Depending on the number of records on a page, a NEXT and PREVIOUS link is displayed.

 

How do I add page number links to the code, to enable the user to navigate directly to a particular page, without the need of scanner through endless amounts of page until the required page is displayed?

 

The image at the following URL, is what I'm after!

 

http://notoon.free.fr/images/forum/20070103_pagination.gif

 

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
  die(mysql_error());
}

mysql_select_db("db", $con);

$perpage = 2;

$start = (isset($_GET['id'])) ? $_GET['id'] : 0;

$TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM person"), 0);

$select = "SELECT * FROM person LIMIT $start, $perpage";
$result = mysql_query($select) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
  echo $row['FirstName']." ";
  echo $row['LastName'].'<br />';
}

if($start == 0)
{
  echo "PREVIOUS";
}
else
{
echo '<a href="pagination.php?id=' . ($start - $perpage) . '">'."PREVIOUS".'</a>';
}

$page = ($_GET['id'] / $perpage) + 1;
$total = ceil($TotalRec / $perpage);

if($start + $perpage >= $TotalRec)
{
  echo "NEXT";
}
else
{
echo '<a href="pagination.php?id=' . ($start + $perpage) . '">'."NEXT".'</a>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/83966-page-numbers-pagination/
Share on other sites

This should help you.

 

I just created it in a few minutes so modifiying the colours etc to how you like will work.

 

<style type="text/css">
<!--

.tableoutline {

FONT-SIZE: 8pt; 
COLOR: #000000; 
FONT-FAMILY: Arial;
BORDER-RIGHT: #000000 1px solid; 
BORDER-TOP: #000000 1px solid; 
BORDER-LEFT: #000000 1px solid; 
BORDER-BOTTOM: #000000 1px solid;

}
-->
</style>
<table width="200" border="0" cellpadding="1" cellspacing="1">
<tr>
<td width="40" class="tableoutline" onmouseover='this.style.background="#c3c3c3"; return true' onmouseout='this.style.background="#FFFFFF"; return true'>Prev</td>
<td width="20" class="tableoutline" onmouseover='this.style.background="#c3c3c3"; return true' onmouseout='this.style.background="#FFFFFF"; return true'>1</td>
<td width="20" class="tableoutline" onmouseover='this.style.background="#c3c3c3"; return true' onmouseout='this.style.background="#FFFFFF"; return true'>2</td>
<td width="20" class="tableoutline" onmouseover='this.style.background="#c3c3c3"; return true' onmouseout='this.style.background="#FFFFFF"; return true'>3</td>
<td width="40" class="tableoutline" onmouseover='this.style.background="#c3c3c3"; return true' onmouseout='this.style.background="#FFFFFF"; return true'>Next</td>
</tr>
</table>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.