Coder Social home page Coder Social logo

php-mysql-pagination's Introduction

php-mysql-pagination

pagination using php and mysql

  • create project folder in your localhost
  • download and copy index.php file into your folder
  • download pagination_db and import to your database
  • just run it's

*please makesure your db username and password~

<?php
// connection
$conn = new mysqli('localhost', 'root', '', 'pagination_db');
// calculation preparation for pagination
$data_per_page    = 2; // the amount of data you want to display per page
$amount_of_data   = ($conn->query("SELECT * FROM items"))->num_rows; // calculate the amount of data
$number_of_page   = ceil($amount_of_data / $data_per_page); // ceil : rounding up value
$active_page      = ( isset($_GET['page']) ) ? $_GET['page'] : 1; // if else
$initial_data     = ($data_per_page * $active_page) - $data_per_page;
// the beginning of the pagination
// go to the previous link
if ( $active_page > 1 ) {
    echo '<a href="?page='.( $active_page - 1 ).'">&lt;</a> '; 
}
// pagination
for ($i=1; $i <= $number_of_page; $i++) { 
    // validation for active page
    if ( $i == $active_page ) {
        $link = '<a href="?page='.$i.'" style="color:red">'.$i.'</a> ';
    }
    else {
        $link =  '<a href="?page='.$i.'">'.$i.'</a> ';
    }
    echo $link;
}
// go to the next link
if ( $active_page <> $number_of_page ) {
    echo '<a href="?page='.( $active_page + 1 ).'">&gt;</a> '; 
}
// end of pagination
// result data using LIMIT
$result = $conn->query("SELECT * FROM items LIMIT $initial_data, $data_per_page");
echo '
<table>
    <th>Id</th>
    <th>Item</th>
    <th>Price</th>
';
while( $x = $result->fetch_object() ) {
    echo '
    <tr>
        <td>'.$x->id.'</td>
        <td>'.$x->item.'</td>
        <td>'.$x->price.'</td>
    </tr>
    ';
}
echo '</table>';
?>

php-mysql-pagination's People

Contributors

rendy-ananda avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.