+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Simple Pagination

  1. #1
    Jaan Guest

    Simple Pagination

    Hey there.. It has been a long time since I wrote a tutorial.. but now I thought I'll do it.. So let's start

    First we have to connect to our database:

    Code:
    // Connect to your database
    $mysql_con mysql_connect("localhost""Username""Password");

    // Let's check if there were any errors
    if(!$mysql_con){
        
        
    // If there was ań error, if there was, lets display an error
        
    die(mysql_error());
        
    }

    // Select your database
    $mysql_db mysql_select_db("MyDatabase");

    // If there were any errors with selecting your database, display an error
    if(!$mysql_db){
        
        die(
    mysql_error());
        

    Now we are connected to database.. Let's get our information.
    First we need to get our page number:

    Code:
    // If the page wasn't set, lets set $page to number 1 for the first page
    if($page == ""){
        
        
    $page "1";
        
    }else{
        
        
    // If page is set, lets get it
        
    $page $_GET['page'];
        

    Now we need to get all messages from database and count them:

    Code:
    // Now lets get all messages from your database
    $sql "SELECT * FROM messages";
    $query mysql_query($sql);

    // Lets count all messages
    $num mysql_num_rows($query); 
    When these things are done.. lets set our numbers:

    Code:
    // Lets set how many messages we want to display
    $per_page "10";

    // Now we must calculate the last page
    $last_page ceil($num/$per_page);

    // And set the first page
    $first_page "1"
    Now it's time for "Next" , "Previous" First and last page.. :

    Code:
    // Here we are making the "First page" link
    echo "<a href='?page=".$first_page."'>First page</a> ";

    // If page is 1 then remove link from "Previous" word
    if($page == $first_page){
        
        echo 
    "Previous ";
        
    }else{
        
        if(!isset(
    $page)){
            
            echo 
    "Previous ";
            
        }else{
            
            
    // But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
            
    $previous $page-1;
            echo 
    "<a href='?page=".$previous."'>Previous</a> ";
        
        }
        
    }

    // If the page is last page.. lets remove "Next" link
    if($page == $last_page){
        
        echo 
    "Next ";    
        
    }else{
        
        
    // If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
        
    if(!isset($page)){
            
            
    $next $first_page+1;
            echo 
    "<a href='?page=".$next."'>Next</a> ";
            
        }else{
        
            
    $next $page+1;
            echo 
    "<a href='?page=".$next."'>Next</a> ";
        
        }
        
    }

    // And now lets add the "Last page" link
    echo "<a href='?page=".$last_page."'>Last page</a>"


    Now lets do some math and get our messages :

    Code:
    // Math.. It gets us the start number of message that will be displayed
    $start = ($page-1)*$per_page;

    // Now lets set the limit for our query
    $limit "LIMIT $start, $per_page";

    // It's time for getting our messages
    $sql "SELECT * FROM messages $limit";
    $query mysql_query($sql);

    echo 
    "<br /><br />";

    // And lets display our messages
    while($row mysql_fetch_array($query) or die(mysql_error())){
        
        echo 
    $row['message']."<br />";
        

    Voila.. it wasn't that hard right ?

    Here's the full code with comments:

    Code:
    <?php

    // Connect to your database
    $mysql_con mysql_connect("localhost""Username""Password");

    // Let's check if there were any errors
    if(!$mysql_con){
        
        
    // If there was ań error, if there was, let's display an error
        
    die(mysql_error());
        
    }

    // Select your database
    $mysql_db mysql_select_db("MyDatabase");

    // If there were any errors with selecting your database, display an error
    if(!$mysql_db){
        
        die(
    mysql_error());
        
    }

    // If the page wasn't set, lets set $page to number 1 for the first page
    if($page == ""){
        
        
    $page "1";
        
    }else{
        
        
    // If page is set, let's get it
        
    $page $_GET['page'];
        
    }

    // Now lets get all messages from your database
    $sql "SELECT * FROM messages";
    $query mysql_query($sql);

    // Lets count all messages
    $num mysql_num_rows($query);

    // Lets set how many messages we want to display
    $per_page "10";

    // Now we must calculate the last page
    $last_page ceil($num/$per_page);

    // And set the first page
    $first_page "1";

    // Here we are making the "First page" link
    echo "<a href='?page=".$first_page."'>First page</a> ";

    // If page is 1 then remove link from "Previous" word
    if($page == $first_page){
        
        echo 
    "Previous ";
        
    }else{
        
        if(!isset(
    $page)){
            
            echo 
    "Previous ";
            
        }else{
            
            
    // But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
            
    $previous $page-1;
            echo 
    "<a href='?page=".$previous."'>Previous</a> ";
        
        }
        
    }

    // If the page is last page.. lets remove "Next" link
    if($page == $last_page){
        
        echo 
    "Next ";    
        
    }else{
        
        
    // If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
        
    if(!isset($page)){
            
            
    $next $first_page+1;
            echo 
    "<a href='?page=".$next."'>Next</a> ";
            
        }else{
        
            
    $next $page+1;
            echo 
    "<a href='?page=".$next."'>Next</a> ";
        
        }
        
    }

    // And now lets add the "Last page" link
    echo "<a href='?page=".$last_page."'>Last page</a>";

    // Math.. It gets us the start number of message that will be displayed
    $start = ($page-1)*$per_page;

    // Now lets set the limit for our query
    $limit "LIMIT $start, $per_page";

    // It's time for getting our messages
    $sql "SELECT * FROM messages $limit";
    $query mysql_query($sql);

    echo 
    "<br /><br />";

    // And lets display our messages
    while($row mysql_fetch_array($query) or die(mysql_error())){
        
        echo 
    $row['message']."<br />";
        
    }

    ?>
    I hope it helped you a little

    Regards,
    Jaan
    Last edited by Alexander; 04-18-2011 at 10:19 PM. Reason: Restoration

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Simple Pagination

    Good Tutorial +rep

  4. #3
    Jaan Guest

    Re: Simple Pagination

    Thank you

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Simple Pagination

    Very nice. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Jordan Guest

    Re: Simple Pagination

    Very handy! +rep

  7. #6
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Simple Pagination

    Epic failure. (just kidding, but in all seriousness)
    Code:
    // Now lets get all messages from your database 
    $sql "SELECT * FROM messages"
    $query mysql_query($sql); 

    // Lets count all messages 
    $num mysql_num_rows($query); 
    Jordan did this and crippled the CodeCall server.
    Code:
    $num mysql_query("SELECT COUNT(*) FROM messages"); 
    is the preferred method of counting the rows in a database.

  8. #7
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Simple Pagination

    nice tutorial +rep
    @john: is theres a performance difference between these 2:
    -"select count(`some_field_name`) from table"
    -"select count(*) from table"
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  9. #8
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Simple Pagination

    Quote Originally Posted by amrosama View Post
    nice tutorial +rep
    @john: is theres a performance difference between these 2:
    -"select count(`some_field_name`) from table"
    -"select count(*) from table"
    Yes assuming the MyISAM engine is being used (and not Innodb - MyISAM is default) COUNT(*) will have the best performance. MyISAM caches the table row number so COUNT(*) is instantaneous. COUNT(`col`) could cause MySQL to perform a table scan depending on the column attributes.

  10. #9
    Jordan Guest

    Re: Simple Pagination

    Quote Originally Posted by John View Post
    Epic failure. (just kidding, but in all seriousness)
    Code:
    // Now lets get all messages from your database 
    $sql "SELECT * FROM messages"
    $query mysql_query($sql); 

    // Lets count all messages 
    $num mysql_num_rows($query); 
    Jordan did this and crippled the CodeCall server.
    Code:
    $num mysql_query("SELECT COUNT(*) FROM messages"); 
    is the preferred method of counting the rows in a database.

    I believe, if we look hard enough, we can find a flaw in everyone's tutorial. I looked over some of your tutorials from the past they also have errors.

    Quote Originally Posted by amrosama View Post
    nice tutorial +rep
    @john: is theres a performance difference between these 2:
    -"select count(`some_field_name`) from table"
    -"select count(*) from table"
    Check this out: COUNT(*) vs COUNT(col) | MySQL Performance Blog

    Good performance information there.

  11. #10
    Jaan Guest

    Re: Simple Pagination

    Well.. What can I say..
    both ways work.. so..

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PHP Pagination
    By sachinsharma1507 in forum PHP Development
    Replies: 2
    Last Post: 10-31-2011, 12:53 PM
  2. php pagination
    By kwahedi in forum PHP Development
    Replies: 1
    Last Post: 04-02-2011, 09:26 AM
  3. Pagination
    By kwahedi in forum PHP Development
    Replies: 2
    Last Post: 03-18-2011, 02:43 PM
  4. Pagination - need help
    By amir2000 in forum PHP Development
    Replies: 2
    Last Post: 01-31-2011, 10:43 AM
  5. Pagination Techniques
    By mikelbring in forum PHP Development
    Replies: 0
    Last Post: 10-06-2008, 01:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts