分页 - 从0开始计数,没有mysql_result(显示错误)

时间:2015-06-18 00:44:14

标签: php mysql pagination

我正在将我的MySQL结果分页到每页10个。到目前为止,我已经能够计算列中有多少结果,计算如果有10个结果/页面将会有多少页,显示页数并添加每个页面的链接(例如:Page1,第2页,第3页,第4页......)

目前,所有38个结果都显示在一个页面上。当我点击页码时,我被带到了正确的链接,但是带有38个结果的内容是相同的。我知道我需要将38个结果分成四页。这是我坚持的地方:

要查找页数,我有变量:

$pages = ceil($items_number / $per_page )

然而,在我看过的多个教程中:

$pages = ceil(mysql_results($items_number,0) / $per_page )

允许数据计数从0开始。当我尝试这个时,我收到错误

Fatal error: Call to undefined function mysql_results()

所以我没有选择从0开始计算结果(使用mysql_result)并将其分成每页10个。

如何绕过mysql_result并能够为每个页码显示相应的数据/项目数?

这是一个链接,可点击的页码位于顶部: http://test.ishabagha.com/classic_cars/pag_test.php

代码:

<?php
require_once("./includes/database_connection.php");

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    $page = "";

    $per_page = 10;
    $query = "SELECT productCode, productName, productLine, productScale, productVendor, productDescription, buyPrice FROM products WHERE `productLine` = 'Classic Cars'";
                $result = mysqli_query($dbc, $query)
                or die(mysqli_error($dbc));

    $query_count = "SELECT count(productLine) FROM products WHERE productLine = 'Classic Cars'";
    $items = mysqli_query($dbc, $query_count)
                or die(mysqli_error($dbc));
    $row = mysqli_fetch_row($items);

    $items_number = $row[0];

    $pages = ceil($items_number / $per_page )


?>

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <title>Home</title>
    <link type="text/css" rel="stylesheet" href="classic_cars.css" />
</head>

<body>
    <?php
        require_once("./includes/navigation.php");
    ?>

    <?php

    for($number = 1; $number <= $pages; $number++) {
        echo "<a href='?page=$number'>$number</a>";
                }

        while ($row = mysqli_fetch_array($result)) { 
            $product_code = $row['productCode'];
            $product_name = $row['productName'];
            $product_line = $row['productLine'];
            $product_vendor = $row['productVendor'];
            $product_description = $row['productDescription'];
            $buy_price = $row['buyPrice'];

                echo "<tr>
                <td><p>$product_name</p></td>
                </tr>";

        } // end while ($row = mysqli_fetch_array($result))

    ?>

    <?php
        require_once("./includes/footer.php");
    ?>      
</body>
</html>

1 个答案:

答案 0 :(得分:0)

注意:

  • 这意味着您将deprecated mysql_* API与mysqli_*混合在一起。您应该只在项目中使用一个API,我建议仅使用mysqli_*
  • 您在;
  • 中忘记了ceil($items_number / $per_page )
  • 显示所有结果,因为您未在LIMIT
  • 内的查询中设置$query
  • 为了做到这一点,你应该知道你当前在你的分页页面

首先获取总行数(替换上一行):

$items_number = mysqli_num_rows($items);

让我们使用$_GET["page"]确定您当前的页面。如果您的网址中未找到任何网页,则显示的默认网页是第一个。

将此信息放在$pages = ceil($items_number / $per_page);

之后
if (isset($_GET['page']) && is_numeric($_GET['page'])) { /* IF PAGE NUMBER IS VALID */
  $currentpage = (int) $_GET['page']; /* STORE THE CURRENT PAGE */
} else {
  $currentpage = 1; /* DEFAULT PAGE IS SET TO 1 */
} 

if ($currentpage > $pages) { /* IF CURRENT PAGE IS 2 OR MORE */
  $currentpage = $pages;
} 
if ($currentpage < 1) { /* IF CURRENT PAGE IS LESS THAN 1, WHICH WE SHOULD PREVENT */
  $currentpage = 1; /* DEFAULT PAGE IS SET TO 1 */
} 

$offset = ($currentpage - 1) * $per_page; /* SET AN OFFSET FOR YOUR QUERY LATER */

您的查询应该是这样的:

$query = "SELECT productCode, productName, productLine, productScale, productVendor, productDescription, buyPrice
          FROM products
          WHERE `productLine` = 'Classic Cars'
          LIMIT $offset, $per_page"; /* LIMITS THE NUMBER TO SHOW IN A PAGE */

但我们应该移动这个查询,以及我上面给出的第一个代码之后的执行。所以安排是:

$query_count = "SELECT productLine FROM products WHERE productLine = 'Classic Cars'";
$items = mysqli_query($dbc, $query_count) /* EXECUTE THE QUERY */
            or die(mysqli_error($dbc)); 
$items_number = mysqli_num_rows($items); /* GET THE NUMBER OF RESULTS */
$items_number = number_format($items_number); /* SECURE THE FORMAT THAT IT IS A NUMBER */

$per_page = 10; /* NUMBER OF ROWS TO SHOW PER PAGE */
$pages = ceil($items_number / $per_page );

/* THE CODE I HAD GIVEN THAT GETS THE CURRENT PAGE AND DEFINES THE $offset */

/* THEN THE QUERY THAT LIMITS THE NUMBER OF ROWS TO FETCH */

/* THEN YOU FETCH THE RESULT, AND THEN THE HTML */