“下一步”按钮不会刷新页面,显示接下来的 25 个结果


"Next" button doesn't refresh page with next 25 results

我有下面的代码,并试图从我的 sql 表中获取接下来的 25 个结果以显示在页面上。 但是,每当我单击下一步按钮时,都不会显示任何信息。 我有我的偏移量 = ($page - 1) * $items_per_page......我正在努力弄清楚这一点,因为与我编写的其他代码相比,它似乎很简单,但事实证明对我来说非常难以捉摸......任何协助将不胜感激。 我的主要问题是下一个链接没有提供接下来的 25 个结果,我无法确定为什么以及如何更正。

                            echo "<h3 style='text-align:center;'>Welcome to the Exchange Portal,&nbsp;" . $row['name'] . "!&nbsp;</h3>";

                            $items_per_page = 25;

                            $sql_count = "SELECT pin, title, title2, email, phone FROM crown_acura";
                            $result_cnt = mysqli_query($conn, $sql_count);
                            if(false === $result_cnt) {
                                throw new Exception('Query failed with: ' . mysqli_error());
                                } else {
                                   $row_count = mysqli_num_rows($result_cnt);
                                   // free the result set as you don't need it anymore
                                   //mysqli_free_result($result_cnt);
                                }
                                echo $row_count;
                                echo "&nbsp;";
                                if (!isset($_GET['Page'])) {
                                    $Page = 1;
                                } else {
                                    $Page = $_GET['Page'];
                                }
                                echo $page;
                                echo "&nbsp;";
                                $page_count = 0;
                                if (0 === $row_count) {  
                                    // maybe show some error since there is nothing in your table
                                } else {
                                  // determine page_count
                                   $page_count = (int)ceil($row_count / $items_per_page);
                                   // double check that request page is in range
                                   if($page > $page_count) {
                                        // error to user, maybe set page to 1
                                        $page = 1;
                                   }
                                }
                                echo "&nbsp;";
                                echo $page_count;
                                echo "&nbsp;";
                                echo $items_per_page;
                                $offset = ($page-1)*$items_per_page;

                                //echo $paging_info;
                                //echo "&nbsp;";
                                echo "<br />";
                            //Query for displaying results
                            $list_sql = "SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page";
                            $result_query = $conn->query($list_sql);
                                //Table for displaying query results
                                echo "<table class='verify'>";
                                echo "<tr >";
                                echo "<td><h3>Name</h3></td><td>&nbsp;</td><td><h3>E-mail</h3></td><td><h3>Phone</h3></td>";
                                echo "</tr>";
                                for($i = 1; $i<= $page_count; $i++) {
                                    if ($result_query->num_rows > 0) {
                                        // output data of each row
                                        while($row3 = mysqli_fetch_array($result_query)) {
                                            echo "<tr>";
                                            echo "<td class='dltd2 dlcl'>" . $row3["title"] . "</td><td>" . $row3["title2"] . "</td><td><a href='mailto:" . $row3['email'] . "'>" . $row3["email"] . "</a>&nbsp;</td><td>" . $row3["phone"] . "&nbsp;</td>";
                                            echo "</tr>";   
                                        }
                                    } else {
                                        echo "0 results";   
                                    }
                                }
                                echo "<tr></tr>";

                                $next_page = $page + 1;
                                $last_page = $page - 1;
                                if($paging_info['curr_page'] <= 1) {
                                    echo "<tr>";
                                    echo "<td></td><td colspan='2'><a class='loadlink' href='" . $_PHP_SELF . "'>Next 25</a></td><td></td>";
                                    echo "</tr>";
                                } elseif ($paging_info['curr_page'] < $page_count) {
                                        echo "<tr>";
                                        echo "<td></td><td><a href='" . $_PHP_SELF . "?page=" . $last_page . "'>Prev 25</a></td><td><a href='" . $_PHP_SELF . "?page=" . $next_page . "'>Next 25</a></td><td></td>";
                                        echo "</tr>";
                                        } elseif ($paging_info['curr_page'] === $page_count) {
                                            echo "<tr>";
                                            echo "<td></td><td colspan='2'><a href='" . $_PHP_SELF . "?page=" . $last_page . "'>Prev 25</a></td><td></td>";
                                            echo "</tr>";
                                            }
                                echo "</table>";
                    }
                }
            }

您是否尝试运行渲染的 SQL。

输出到浏览器:

"SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page" 

试试这个...并更改不同值(2,3,...,等)的$page

<?php
$items_per_page = 25;
$sql_count = "SELECT pin, title, title2, email, phone FROM crown_acura";
$result_cnt = mysqli_query($conn, $sql_count);
if (false === $result_cnt) {
    throw new Exception('Query failed with: ' . mysqli_error());
} else {
    $row_count = mysqli_num_rows($result_cnt);
    // free the result set as you don't need it anymore
    //mysqli_free_result($result_cnt);
}
echo $row_count;
echo "&nbsp;";
if (!isset($_GET['Page'])) {
    $Page = 1;
} else {
    $Page = $_GET['Page'];
}
echo $page;
echo "&nbsp;";
$page_count = 0;
if (0 === $row_count) {
    // maybe show some error since there is nothing in your table
} else {
    // determine page_count
    $page_count = (int)ceil($row_count / $items_per_page);
    // double check that request page is in range
    if ($page > $page_count) {
        // error to user, maybe set page to 1
        $page = 1;
    }
}
echo "&nbsp;";
echo $page_count;
echo "&nbsp;";
echo $items_per_page;
$offset = ($page - 1) * $items_per_page;

//echo $paging_info;
//echo "&nbsp;";
echo "<br />";
//Query for displaying results
$list_sql = "SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page";
$result_query = $conn->query($list_sql);

echo ("RESULTS: ".$result_query->num_rows());

?>