【问题标题】:How to paginate a table of Mysql in PHP如何在 PHP 中对 Mysql 的表进行分页
【发布时间】:2011-10-04 09:21:06
【问题描述】:

我在 mysql 中有一个表。 因为它有很多行,所以我想将每 10 行放在一个页面中,然后单击链接显示接下来的 10 行。 有什么解决办法吗?

【问题讨论】:

标签: php mysql html-table


【解决方案1】:

真的太棒了http://www.phpsimplicity.com/tips.php?id=1

就是这么简单!无需处理大量课程!我很高兴: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>

顺便说一下rickyduck的链接也不错

【讨论】:

  • 不是它的播放方式:)
  • @Nickool 这就是我为你做的事情
【解决方案2】:

我建议查看此链接:http://php.about.com/od/phpwithmysql/ss/php_pagination.htm 了解基本分页。此外,如果您了解 javascript,则可以使用 jQuery。

【讨论】:

  • 谢谢,我知道 javascript 但我是 JQuery 库的新手
  • 我建议只使用第一个链接,更简单,正是你想要的。
猜你喜欢
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多