본문 바로가기
MySql

MySQL How to paginate a table of Mysql in PHP

by 베이스 공부 2020. 10. 1.
반응형

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. 해결책이 있습니까?

 

해결 방법

 


너무 간단합니다! 거대한 수업을 할 필요가 없습니다! 나는 행복하다 : 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..


$prev = $startrow - 10; //only print a "Previous" link if a "Next" was clicked if ( $prev >= 0 ) {


} ?> </form> </body> </html>

덧붙여서 rickyduck의 링크도 좋았습니다

 

참조 페이지 https://stackoverflow.com/questions/6677580

 

 

반응형

댓글