Since i seen someone request one. I thought i would write a quick one
You can view demo
HERE
Firstly connect to your database using the following
PHP Code:
mysql_connect("host","dbuser","dbpass") or die("ERROR:".mysql_error());
mysql_select_db("dbname") or die("ERROR DB:".mysql_error());
Now we can start sorting out things like setting how many values we want on each page.
Now we have done that we need to get the current page. If there is no page set, we must set it to page 1.
PHP Code:
$p = $_GET['p'];
if(empty($p))
{
$p = 1;
}
Ok now we want to set the limit of values of each page ready for the MySQL query.
PHP Code:
$limits = ($p - 1) * $max;
Now we can use the query to select from the database with limits and maximum on each page.
PHP Code:
$sql = mysql_query("SELECT * FROM table LIMIT $limits,$max") or die(mysql_error());
Now we want to work out just how many results we have all together.
PHP Code:
$totalres = mysql_query("SELECT * FROM table");
$count = mysql_num_rows($totalres);
There are easier ways to work that out, but i am using that method for now
Ok now we have the total number of values we now need to work out just how many pages thats going to use.
PHP Code:
$totalpages = ceil($count / $max); //Returns the next highest integer value by rounding up value if necessary.
Now we can start constructing the table and display the results
PHP Code:
echo "<table border=1 width=500><tr><td><b>Title</b></td><td><b>Type</b></tr>";
while($r = mysql_fetch_array($sql))
{
echo "<tr><td>$r[name]</td><td>$r[type]</td></tr>";
}
echo "</table>";
Now its time to show all the links for all the pages to go from results to results
PHP Code:
for($i = 1; $i <= $totalpages; $i++)
{
echo "<a href=database.php?p=$i'> $i </a>|";
}
This was a simple pagination, of course you can add NEXT > PREVIOUS and so on.
Full Code:
PHP Code:
<?php
mysql_connect("host","dbuser","dbpass") or die("ERROR:".mysql_error());
mysql_select_db("dbname") or die("ERROR DB:".mysql_error());
$max = 25; //amount of articles per page. change to what to want
$p = $_GET['p'];
if(empty($p))
{
$p = 1;
}
$limits = ($p - 1) * $max;
$sql = mysql_query("SELECT * FROM table LIMIT $limits,$max") or die(mysql_error());
//the total rows in the table
$totalres = mysql_query("SELECT * FROM table");
$count = mysql_num_rows($totalres);
$totalpages = ceil($count / $max);
//the table
echo "<table border=1 width=500><tr><td><b>Title</b></td><td><b>Type</b></tr>";
while($r = mysql_fetch_array($sql))
{
echo "<tr><td>$r[name]</td><td>$r[type]</td></tr>";
}
echo "</table>";
for($i = 1; $i <= $totalpages; $i++)
{
echo "<a href=dvds.php?p=$i'> $i </a>|";
}
?>
As request...a google like not exact, but the same concept style buttons on the pagination. I have updated the link code below.
PHP Code:
$show = 10; // How many links to show
echo "<br><br>";
if($p > 1) // If p > then one then give link to first page
{
echo "<a href=?p=1> [FIRST] </a> ";
}
else{ // else show nothing
echo "";
}
if($p != 1){ // if p aint equal to 1 then show previous text
$previous = $p-1;
echo "<a href=?p=$previous> [ PREVIOUS ] </a>";
}
else{ //else show nothing
echo "";
}
for($i =1; $i <= $show; $i++) // show ($show) links
{
if($p > $totalpages){ // if p is greater then totalpages then display nothing
echo "";
}
else if($_GET["p"] == $p){ //if p is equal to the current loop value then dont display that value as link
echo " $p ";
}
else{
echo " <a href=?p=$p> ( $p ) </a>"; // else display the rest as links
}
$p++; //increment $p
}
echo "....."; // display dots
if($_GET["p"] == $totalpages){// if page is equal to totalpages then dont display the last page at the end of links
echo "";
}
else // else display the last page link after other ones
{
echo "<a href=?p=$totalpages> ( $totalpages ) </a>";
}
if($_GET["p"] < $totalpages)// if p is less then total pages then show next link
{
$next = $_GET["p"] + 1;
echo "<a href=?p=$next> [ NEXT >] </a>";
}
echo "<br><br>";