Publishing System Settings Logout Login Register
Simple PHP + MySQL Paging System
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on June 13th, 2009
20364 views
PHP Coding
Hello everyone,

Yesterday, I searched a lot to find a simple PHP paging system form my site. I found many, but they scare the hell out me! There coding was to hard to understand. So I decided to write a simple PHP that creates paging from MySQL Query. So Let's Start!

The linking should be > page.php?page=NUMBER

 
<?php

$pages=$_GET['page']; //Get the current page


//Connect to your MySQL


    $mysql_link = mysql_connect("localhost", "namef", "pass");
    mysql_select_db("test") or die("Could not select database");

// This the full code which will generate a paging system
$limit=10; // Limit of result per page

$qresult = mysql_query("SELECT * FROM sample_db");  // Let's get the query
   
    $nrResults=mysql_num_rows($qresult); // Count the results
   
    if (($nrResults%$limit)<>0) {
        $pmax=floor($nrResults/$limit)+1;  // Divide to total result by the number of query you want
// to display per page($limit) and create a Max page
    } else {
        $pmax=floor($nrResults/$limit);
    }
    $qresult = mysql_query("SELECT * FROM sample_d DESC LIMIT ".(($_GET["page"]-1)*$limit).", $limit");
//Need to generate query considering your limit
    while($line = mysql_fetch_array( $qresult ))

{
// Now once we got the query from MySQL, we need to think what we to do it. You can do anything. This is just an example
echo "

Name: ".$line['name']." <br />
Age: ".$line['age']." <br />
Phone Number: ".$line['phone']." <br />

";

// Here comes the Real part of this Tutorial!!
       
 }                 echo "<div class='navpage'>"; // Make a simple css

// We need to create a Previous page, so we need $pages to be bigger than 1, otherwise at the first page we would get a 0.
//For the previous to show up we need at least to be at the second page.
    if($pages > 1) {
$prevp="<a href='http://mysite.com/page.php?page=".($pages-1)."' title='Previous Page'>Previous Page</a>"; } else {echo "";}   
echo $prevp;

// Here We want create a page from the results we got by dividing the total by the the limit. So let say you got
 //45 results and you want 5 results per page; these simple lines will create you 8 pages.

                                $pid=1;
                                while ($pid<=$pmax) {
$paging= "<a href='http://mysite.com/page.php?page=$pid' title='Page $pid of $pmax'> $pid</a>";

//So here, let say we are at the 3rd page and we want the 3 to be blank so the user can know where is he now.
//This will act as Current Page! We need to replace the url by a text

$newpaging=str_replace("<a href='http://mysite.com/page.php?page=$pages' title='Page $pages of $pmax'> $pages</a>", "<span>$pages</span>", $paging);
echo $newpaging;
                           
                                $pid++;  // create pages until reach the result

       
            } 
//We want to create a next page and a last page, $pages have to be less than $pmax.
    if($pages < $pmax) {
$nextp="<a href='http://mysite.com/page.php?page=".($pages+1)."' title='Next Page'>Next Page</a>"; } else {echo "";}   
echo $nextp;
echo  "<a href='http://mysite.com/page.php?page=$pmax' title='Last Page'>Last Page</a>";
echo "</div>";

?>


When linking to the first page it must have this form page.php?page=1 . The page=1 is important otherwise it will result in an error.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
AFG89

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top