【问题标题】:Pagination issues in phpphp中的分页问题
【发布时间】:2019-10-19 08:20:22
【问题描述】:

当我点击年级时,一个学生毕业了,如下所示 enter image description here

它把用户带到students.php,里面有当年毕业的学生名单。

然后我使用这段代码检查并从 members.php url 字符串中获取值。 enter image description here

在students.php页面中,我实现了一个分页,它也重定向到同一个studenter code hereents.php页面

问题是当我点击students.php页面上显示的分页数时,在同一个students.php页面中没有返回任何数据。

只是想知道问题是否可能是这段代码的结果: enter image description here 因为它会在不再存在的 url 中再次检查 classof

我在students.php上的代码如下:

$results_per_page = 2;

if (isset($_GET['classof']))
{
        // find out the number of results\records stored in the database

        $classof =  mysqli_real_escape_string($conn, $_GET['classof']);


        $query = "SELECT *  from register where classof=? order by firstname asc";
        $statement = $conn->prepare($query);
        $statement->bind_param('s', $classof);
        $statement->execute();
        $result = $statement->get_result();
        $number_of_results = $result->num_rows;
        $statement->close();

        // determine the number of total pages available
        $number_of_pages = ceil($number_of_results/$results_per_page);

         // determine which page number visitor is currently on
        if(!isset($_GET['page']))
        {
            $page=1;
        }
        else
        {
            $page = $_GET['page'];
        }



    //determine the sql limit starting number for the results on the displaying page
    $this_page_first_result = ($page-1)*$results_per_page;

if ($number_of_results > 0 )
    {
        //retrieve selected results from database and display them on page.
        $sql = "SELECT *  from register where classof=? order by firstname asc LIMIT " . $this_page_first_result . ',' . $results_per_page;

        $stmt = $conn->prepare($sql);
        $stmt->bind_param('s', $classof);
        $stmt->execute();
        $result = $stmt->get_result();
        $usercount = $result->num_rows;
        //$statement->close();


          echo "
          <center>
        <div class='container'>
        <div class='row'>
        <div class='col-md-6 offset-md-3 form-div'>
        <h3 class='text-success'> Class of $classof </h3>
        <table class='table table-striped table-borderless table-hover'>
            <thead class='thead-dark'>
                <tr>
                    <th >Firstname</th>
                    <th>Lastname</th>
                    <th>Country of Residence</th>
                    <th>Contact</th>
                </tr>
            </thead>";




        while ($row = mysqli_fetch_array($result))
        {

         $userid = $row["id"];
         $firstname = $row["firstname"];
         $lastname = $row["lastname"];
         $country = $row["country"];

         echo "
            <tr>
                <td>$firstname</td>
                <td>$lastname</td>
                <td>$country</td>
                <td><a href='contact_student.php?userid=$userid'>Contact</a></td>

            </tr>";



        }
    }



        echo "</table>";

         // Display the link to the pages
        for ($page=1;$page<=$number_of_pages;$page++)
        {
            //echo "
            //<tr>
            //";
            echo '<a href="students.php?page=' . $page . '">' . $page . '</a>';
            //echo "</tr>";
        }

        echo "
        </div>
        </div>
        </div>
        </center>";


}       



include ('includes/footer.php'); 
?>

【问题讨论】:

  • 请用ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);发布错误
  • 我已添加,但没有错误。只是当我单击分页编号时,没有显示结果集

标签: php mysql


【解决方案1】:

在 Sql 中你需要使用 LIMIT 和 OFFSET 进行分页。

SELECT
  *
FROM
  my_table
LIMIT {PER_PAGE} OFFSET {LAST_ELEMENT_NUMBER_IN_PREV_PAGE};

【讨论】:

  • 我的 sql 查询具有已在变量中定义的 LIMIT 和 OFFSET。 $sql = "SELECT * from register where classof=? order by firstname asc LIMIT " . $this_page_first_result 。 ','。 $results_per_page;
  • 我看到您使用的是短限制语法。尝试使用LIMIT %n OFFSET %n,而不是LIMIT %n, %n
【解决方案2】:

我已经能够通过更改脚本的以下部分来解决它。所有其他部分保持不变。

之前: enter image description here

更改后 enter image description here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    相关资源
    最近更新 更多