“下一步”按钮不会刷新下一个25个结果的页面

时间:2015-01-03 14:07:51

标签: php sql

我有下面的代码,我试图从我的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>";
                    }
                }
            }

2 个答案:

答案 0 :(得分:1)

您是否尝试过运行渲染的SQL?

输出到浏览器:

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

答案 1 :(得分:-1)

试试这个......并为不同的值(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());

&GT;