1

我正在创建一个在 Twitter 上列出名人的页面。该页面显示了第一个循环应该显示的 10 个不同结果,但它似乎没有更新 $result 以从数据库中提取接下来的 10 个值(用于下一组页面等)。

<?php

    // connect to the database
    $con = mysql_connect("localhost","someuid","somepwd");
      if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

    mysql_select_db("celebrity_twitter", $con);

    $result = mysql_query("SELECT * FROM celebrities");

    // number of results to show per page
    $per_page = 10;

    $total_results = mysql_num_rows($result);
    $total_pages = ceil($total_results / $per_page);

    // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
    if (isset($_GET['page']) && is_numeric($_GET['page']))
    {
            $show_page = $_GET['page'];

            // make sure the $show_page value is valid
            if ($show_page > 0 && $show_page <= $total_pages)
            {
                    $start = ($show_page -1) * $per_page;
                    $end = $start + $per_page; 
            }
            else
            {
                    // error - show first set of results
                    $start = 0;
                    $end = $per_page; 
            }               
    }
    else
    {
            // if page isn't set, show first set of results
            $start = 0;
            $end = $per_page; 
    }

    // display data in table
    echo "<table class='table table-striped'>";
    echo "<tr> <th>Avatar</th> <th>Celebrity Name</th> </tr>";

    // loop through results of database query, displaying them in the table
    for($i = $start; $i < $end; $i++ && $row = mysql_fetch_array($result) )
    {
            // make sure that PHP doesn't try to show results that don't exist
            if ($i == $total_results) { break; }

            // echo out the contents of each row into a table
            echo "<tr>";
            echo "<td><img height='73' width='73' src=" . "http://api.twitter.com/1/users/profile_image?screen_name=" . $row['avatar'] . "&size=bigger></td>";
            echo "<td><a href=" . $row['url'] . " target='_blank'>" . $row['uid'] . "</td></a>";
            echo "</tr>"; 
    }
    // close table>
    echo "</table>";

     // display pagination
    echo "<strong>Page: </strong>";
    for ($i = 1; $i <= $total_pages; $i++)
    {
            echo "<a href='index.php?page=$i'>$i</a> ";
    }
?>
4

2 回答 2

1

我现在正在使用它,它对我来说工作得很好,我希望它也对你有用:PHP Pagination

于 2012-10-21T15:10:29.550 回答
0

您没有将自己定位在数据库中,您总是阅读第一个元素:

// loop through results of database query, displaying them in the table
for($i = $start; $i < $end; $i++ && $row = mysql_fetch_array($result) )
{
        // make sure that PHP doesn't try to show results that don't exist
        if ($i == $total_results) { break; }

这样做的正确方法是使用LIMITMySQL 的子句,带有一个用于恢复真实记录数的特殊子句

SELECT SQL_CALC_FOUND_ROWS * FROM celebrities LIMIT $start, $resultsperpage;

SELECT FOUND_ROWS(); // This is number of celebrities

这意味着您需要在运行查询“之前”计算 $start,即“之前”您知道 $start 是否是一个有效的开始。所以你必须为没有返回行的情况做准备。

一个非常浪费的快速替代方法是检索所有内容并只显示想要的行:

// loop through results of database query, displaying them in the table
for ($i = 0; $i < $total_results; $i++)
{
    $row = mysql_fetch_array($result);
    if ($i < $start) continue;
    if ($i == $end) break;

限制实施

mysql_select_db("celebrity_twitter", $con);

// number of results to show per page
$per_page = 10;

// We REALLY ought to move on to PDO: mysql_* will be deprecated sooner or later
$query    = mysql_query('SELECT SQL_CALC_FOUND_ROWS * FROM celebrities');

// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
        $show_page = (int)$_GET['page'];
        // make sure the $show_page value is valid
        if ($show_page > 0)
                $start = ($show_page -1) * $per_page;
        else
                $start = 0;
        $query .= " LIMIT $start, $per_page;";
    $paged = True;
}
else
{
    $paged = False;
    // The query having no LIMIT, it will retrieve everything
    // if (!$paged), later we will display a link "Show Paged".
}

// Now run the query
$result = mysql_query($query);

// Also fetch REAL number of rows in table {{{
$exec   = mysql_query("SELECT FOUND_ROWS() AS results;");
$tuple  = mysql_fetch_assoc($exec);
$results= $tuple['results'];
mysql_free_result($exec);
unset($exec, $tuple);
// }}}

// If paging, calculate page number. Later, if ($paged), we'll display the pager.
if ($paged)
{
    $maxpages = ceil($results / $per_page);
    if ($page > $maxpages)
        $page = $maxpages+1; // We fetch nothing.
    else
        $page = floor($start / $per_page)+1;
}

// display data in table. It is best to calculate $html first
// and then output it all together at end if no error.

$html = "<table class='table table-striped'>";
$html .= "<tr> <th>Avatar</th> <th>Celebrity Name</th> </tr>";

// loop through results of database query, displaying them in the table
// Here, we get all and only good results, so we need not check anything
while($row = mysql_fetch_array($result))
{
        // echo out the contents of each row into a table
        $html .= "<tr>";
}
于 2012-10-21T15:27:52.507 回答