How to paginate a table of Mysql in PHP

29,896

Solution 1

It was really really awesome http://www.phpsimplicity.com/tips.php?id=1

it is so simple! no need to work with huge classes! I'm Happy:D

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Paginate</title>
    </head>
    <body>
    <form method='get'>
        <?php
        $connection = Mysql_connect( 'server', 'user', 'pass' );
        if ( ! $connection ) {
            echo 'connection is invalid';
        } else {
            Mysql_select_db( 'DB', $connection );

        }
        //Check if the starting row variable was passed in the URL or not
        if ( ! isset( $_GET['startrow'] ) or ! is_numeric( $_GET['startrow'] ) ) {

            //We give the value of the starting row to 0 because nothing was found in URL
            $startrow = 0;

            //Otherwise we take the value from the URL
        } else {
            $startrow = (int) $_GET['startrow'];
        }
        //This part goes after the checking of the $_GET var
        $fetch = mysql_query( "SELECT * FROM sample LIMIT $startrow, 10" ) or
        die( mysql_error() );
        $num = Mysql_num_rows( $fetch );
        if ( $num > 0 ) {
            echo '
        <table border=2>';
            echo '
            <tr>
                <td>ID</td>
                <td>Drug</td>
                <td>quantity</td>
            </tr>
            ';
            for ( $i = 0; $i < $num; $i ++ ) {
                $row = mysql_fetch_row( $fetch );
                echo '
            <tr>';
                echo "
                <td>$row[0]</td>
                ";
                echo "
                <td>$row[1]</td>
                ";
                echo "
                <td>$row[2]</td>
                ";
                echo '
            </tr>
            ';
            }//for
            echo '
        </table>
        ';
        }

        //Now this is the link..
        echo '<a href="' . $_SERVER['PHP_SELF'] . '?startrow=' . ( $startrow + 10 ) . '">Next</a>';

        $prev = $startrow - 10;

        //only print a "Previous" link if a "Next" was clicked
        if ( $prev >= 0 ) {
            echo '<a href="' . $_SERVER['PHP_SELF'] . '?startrow=' . $prev . '">Previous</a>';
        }
        ?>
    </form>
    </body>
</html>

By the way link of rickyduck was good too

Solution 2

I suggest checking out this link : http://php.about.com/od/phpwithmysql/ss/php_pagination.htm for basic pagination. Furthermore, if you have knowledge of javascript, you could use jQuery.

Share:
29,896
Nickool
Author by

Nickool

Web Developer specializing in PHP, JavaScript, Ajax, JQuery and CSS3 with 5 years of experience creating and maintaining professional web applications. Experience developing technical specifications, documenting code and procedures. Effectively develop well structured, easily maintainable applications, web components, pages, style sheets. https://www.linkedin.com/in/neginnickparsa

Updated on June 14, 2020

Comments

  • Nickool
    Nickool almost 4 years

    I have a table in mysql. Because it has many rows I want to put each 10 rows in a page and by clicking a link show me next 10 rows. Is there any solution?

  • Nickool
    Nickool almost 13 years
    Thank you,I know javascript but I'm newbie in JQuery library
  • rickyduck
    rickyduck almost 13 years
    I suggest just using the first link, much easier and exactly what you want.
  • Dejan Marjanović
    Dejan Marjanović almost 13 years
    No the way it's meant to be played :)
  • Mr. Jo
    Mr. Jo almost 5 years
    @Nickool This is what I've did just for you