This is a simple example of mysql table pagination using PHP code.
Please see attached zip for refference.
This is the index.php file bellow:
[php]
/*
in config.php you define the database connection.
$limit defines the limit of items per page
$adjacents number of adiacent items on paginator
*/
include(‘config.php’);
$tbl_name="mapstories_stories";
$adjacents = 3;
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query)) or die(mysql_error());
$total_pages = $total_pages[‘num’];
$targetpage = "index.php";
$limit = 5;
$page = $_GET[‘page’];
if($page)
$start = ($page – 1) * $limit;
else
$start = 0;
$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
if ($page == 0) $page = 1;
$prev = $page – 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage – 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "
n";
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>Lat</th>
<th>Lng</th>
</tr>
<?php $i = 0;
while($row = mysql_fetch_array($result)) {?>
<tr>
<td><?php echo $i++;?></td>
<td><?php echo $row[‘lat’];?></td>
<td><?php echo $row[‘lng’];?></td>
</tr>
<?php }?>
</table>
<?=$pagination?>
</body>
</html>
[/php]
And the config.php:
[php]
mysql_connect(‘localhost’, ‘root’, ”) or die(mysql_error());
mysql_select_db(‘mapstories’) or die(mysql_error());
[/php]
And a small style.css – this one is using Cakephp’s default table styles 😉
[css]
body{
font-family:arial;
font-size:11px;
}
div.pagination {
padding: 3px;
margin: 3px;
}
div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;
zoom: 100%;
text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;
color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;
* zoom: 100%;
font-weight: bold;
background-color: #000099;
color: #FFF;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #EEE;
* zoom: 100%;
color: #DDD;
}
* span.elipsis {zoom:100%}
/** Tables **/
table {
background: #fff;
border-right:0;
clear: both;
color: #333;
margin-bottom: 10px;
width: 100%;
}
th {
border:0;
border-bottom:2px solid #555;
text-align: left;
padding:4px;
}
th a {
display: block;
padding: 2px 4px;
text-decoration: none;
}
th a.asc:after {
content: ‘ ⇣’;
}
th a.desc:after {
content: ‘ ⇡’;
}
table tr td {
background: #fff;
padding: 6px;
text-align: left;
vertical-align: top;
border-bottom:1px solid #ddd;
}
table tr:nth-child(2n) td {
background: #f5f5f5;
}
table .altrow td {
background: #f5f5f5;
}
td.actions {
text-align: center;
white-space: nowrap;
}
table td.actions a {
margin: 0px 6px;
padding:2px 5px;
}[/css]
Click the following link to download all this code: php_pagination