A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Very Simple PHP & MySQL Pagination

Here is a very simple tutorial to implement pagination using PHP and MySQL. Although this is not a fully working code, hopefully, the information provided here will be enough for most intermediate PHP programmers.

$max = 10;
$page = $_GET['page'];
if (!$page) {
    $page=1;
}
 
$sql_query = 'SELECT * FROM table t WHERE t.fieldA = "Active" LIMIT '.$limits.','.$max.'';
//Code to execute the $sql_query here
 
$totalresult = 'SELECT * FROM table t WHERE t.fieldA = "Active"';
//Code to execute the $totalresult query
//then assign the total number of rows to $total variable
 
$totalpages = ceil($total / $max); //remember, we assigned $total the total number of results from the $totalresult query?

Now for the HTML part of your code you can use $totalpages to do a for-loop to generate the Page Numbers then assign links to $_GET['page'] using the counter from the for-loop.
For example:

<a href="http://locahost/pagination.php?page='1'">Page 1</a>
<a href="http://locahost/pagination.php?page='2'">Page 2</a>
<a href="http://locahost/pagination.php?page='3'">Page 3</a>

The HTML code above can be generated using the PHP code below:

for ($i=1; $i &lt; $totalpages; $i++) {
    echo "<a href="http://locahost/pagination.php?page=%22.$i.%22">".$i."</a>";
}
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

4 Responses to Very Simple PHP & MySQL Pagination

  1. idleboy says:

    niceeee so simple

  2. mike says:

    $limit variable got no value.. what supposed value is it??

  3. Wenbert says:

    $limit == total of the results per page

  4. a.r.bhuyan says:

    its a great tut5orial buddy .Lets try ….

Leave a Reply to mike Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>