Now, we have it all done. Hopefully you understand it all. Here is the code, in full.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| <?php
if($_GET['page'])
{
$page = $_GET['page'];
}else{
$page = 1;
}
$max = 10;
$cur = (($page * $max) - $max);
$getdata = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT $cur, $max") or die(mysql_error());
$data = mysql_fetch_array($getdata);
$counttotal = mysql_query("SELECT * FROM `table` ") or die(mysql_error());
$counttotal = mysql_num_rows($counttotal);
$total_pages = ceil($counttotal / $max);
if($page > 1){
$prev = ($page - 1);
echo '<a href="?page=' . $prev . '">« Previous</a>';
}
for($i = 1; $i <= $total_pages; $i++)
{
if($page == $i)
{
echo'<b>' . $i .'</b> ';
} else {
echo '<a href="?page=' . $i . '">' . $i . '</a> ';
}
}
if($page < $total_pages){
$next = ($page + 1);
echo '<a href="?page=' . $next . '">Next »</a>';
}
?> // end php script |
And thats it. :)
Hope you enjoyed this tutorial, and learnt something if you did not before know how to do this. Please comment this tutorial.
If you need help, post a topic in the
help forum and someone will gladly help you!