Intergrate pagination in table PHP/MySQL

时间:2017-08-04 12:00:31

标签: php mysql pagination

I wrote this code:

<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<table class="table mx-auto">
    <thead>
    <tr>
        <th>Name</th>
        <th>Surname</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Address</th>
        <th>Zipcode</th>
        <th>City</th>
        <th>Company</th>


    </tr>
    </thead>
    <tbody>
    <ul class="pagination">
        <?php
        $page_max = 10;
        $entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
        $numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
        if(isset($_GET['page']) && $_GET['page']){

            $previous = $_GET['page'] - 1;
            if($previous = -1 ){
                $previous = 0;
            }
            if($previous = 0){
                echo '<li class="page-item"><a class="page-link" href="#">Previous</a></li>';
            }
            else{
                echo '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>';
            }
        }


        for($i = 0; $i < $numberOfPages; $i++){

            echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
        }
        $numberOfRecords = 10;
        if(isset($_GET['page'])) {
            $page = $_GET['page'];
            $start = $page * 10;
        }
        else{
            $page = 0;
            $start = $page * 10;

        }

        $customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");

        ?>
        <li class="page-item"><a class="page-link" href="#">Next</a></li>
    </ul>
    <?php

    foreach($customers as $customer){
        $name = $customer ['name'];
        $surname = $customer['surname'];
        $email = $customer['email'];
        $phone = $customer['phone'];
        $address = $customer['address'];
        $zipcode = $customer['zipcode'];
        $city = $customer['city'];
        $company = $customer['company'];
        $id = $customer['id'];
        echo "<tr>
                <td>$name</td>
                <td>$surname</td>
                <td>$email</td>
                <td>$phone</td>
                <td>$address</td>
                <td>$zipcode</td>
                <td>$city</td>
                <td>$company</td>



              </tr>";
    }
    ?>
</tbody>
</table>
</div>

It's working except for the next button but I can fix that my own. I would like to know how I can use the pagination below the table. Now it's above the table, copy & pasting the code below the table doesn't work because the table with the customers is build before the variable customers is defined.

Could anyone help me achieve this?

4 个答案:

答案 0 :(得分:1)

You can simply put neccessary code for pagination at bottom

<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<table class="table mx-auto">
    <thead>
    <tr>
        <th>Name</th>
        <th>Surname</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Address</th>
        <th>Zipcode</th>
        <th>City</th>
        <th>Company</th>


    </tr>
    </thead>
    <tbody>

        <?php
        $page_max = 10;
        $entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
        $numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);

        $numberOfRecords = 10;
        if(isset($_GET['page'])) {
            $page = $_GET['page'];
            $start = $page * 10;
        }
        else{
            $page = 0;
            $start = $page * 10;

        }

        $customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");

        ?>
        <li class="page-item"><a class="page-link" href="#">Next</a></li>
    </ul>
    <?php

    foreach($customers as $customer){
        $name = $customer ['name'];
        $surname = $customer['surname'];
        $email = $customer['email'];
        $phone = $customer['phone'];
        $address = $customer['address'];
        $zipcode = $customer['zipcode'];
        $city = $customer['city'];
        $company = $customer['company'];
        $id = $customer['id'];
        echo "<tr>
                <td>$name</td>
                <td>$surname</td>
                <td>$email</td>
                <td>$phone</td>
                <td>$address</td>
                <td>$zipcode</td>
                <td>$city</td>
                <td>$company</td>



              </tr>";
    }
    ?>
</tbody>
</table>
<ul class="pagination">
<?php
if(isset($_GET['page']) && $_GET['page']){

  $previous = $_GET['page'] - 1;
  if($previous = -1 ){
      $previous = 0;
  }
  if($previous = 0){
      echo '<li class="page-item"><a class="page-link" href="#">Previous</a></li>';
  }
  else{
      echo '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>';
  }
}


for($i = 0; $i < $numberOfPages; $i++){

  echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
?>
</ul>
</div>

答案 1 :(得分:0)

Maybe it's time to tryframeworks? Save time and effort

答案 2 :(得分:0)

You can define all your variables on top of your HTML file:

<?php

$database = ...;
$page_max = 10;
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);

?>

<DOCTYPE html>
...

答案 3 :(得分:0)

just store your pagination html in variable and echo at top and bottom when u need like below.

<?php
    $paginationhtml = "";
    $paginationhtml .= '<ul class="pagination">';
    $page_max = 10;
    $entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
    $numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
    if(isset($_GET['page']) && $_GET['page']){
        $previous = $_GET['page'] - 1;
        if($previous = -1 ){
            $previous = 0;
        }
        if($previous = 0){
            $paginationhtml .= '<li class="page-item"><a class="page-link" href="#">Previous</a></li>';
        }
        else{
            $paginationhtml .= '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>';
        }
    }
    for($i = 0; $i < $numberOfPages; $i++){
        $paginationhtml .= '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
    }
    $numberOfRecords = 10;
    if(isset($_GET['page'])) {
        $page = $_GET['page'];
        $start = $page * 10;
    }else{
        $page = 0;
        $start = $page * 10;
    }
    $customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");
    $paginationhtml .= '<li class="page-item"><a class="page-link" href="#">Next</a></li>';
    $paginationhtml .= '</ul>';
?>
// just echo pagination where u need //
<?php echo $paginationhtml; ?>