简体   繁体   中英

Pagination issues in php

When I click on class of year a student graduated as seen below enter image description here

it takes user to the students.php with the list of students graduated that year.

在此处输入图像描述

Then I use this piece of code to check and get the value from the members.php url string. enter image description here

In the students.php page, I have a pagination implemented which is also redirecting to the same stud enter code here ents.php page

The problem is that when I click on the pagination numbers displayed on the students.php page, no data is returned in the same students.php page.在此处输入图像描述

just wondering if the issue could be as result of this piece of code: enter image description here as it goes to check for the classof again in the url which no longer exist

My code on students.php is below:

$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'); 
?>

In Sql you need to use LIMIT and OFFSET for pagination.

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

I have been able to solve it by changing the below part of the script. All other part remains same.

Before: enter image description here

After change enter image description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM