Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //
- //$db_host = "localhost";
- //$db_name = "dbpaging";
- //$db_user = "root";
- //$db_pass = "";
- //
- //$db = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
- //$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- //
- //
- //
- //$users = $db->query("SELECT * FROM tbl_tutorials LIMIT 0, 10");
- //
- //
- //
- //?>
- <!--<!DOCTYPE html>-->
- <!--<html xmlns="http://www.w3.org/1999/html">-->
- <!---->
- <!--<head>-->
- <!---->
- <!--</head>-->
- <!---->
- <!-- <body>-->
- <!---->
- <!-- Username: <input type="text" value=""> </br>-->
- <!-- Email: <input type="text">-->
- <!---->
- <!-- <input type="submit" value="Search">-->
- <!---->
- <!---->
- <!-- <table border="1" align="center">-->
- <!-- <tr>-->
- <!-- <th>ID</th>-->
- <!-- <th>Title</th>-->
- <!-- <th>Link</th>-->
- <!-- <th>Actions</th>-->
- <!-- </tr>-->
- <!-- --><?php //foreach ($users as $user) : ?>
- <!-- <tr>-->
- <!-- <td>--><?//= $user['tuts_id'] ?><!--</td>-->
- <!-- <td>--><?//= htmlspecialchars($user['tuts_title'])?><!--</td>-->
- <!-- <td>--><?//= htmlspecialchars($user['tuts_link'])?><!--</td>-->
- <!---->
- <!-- <td>-->
- <!-- <a href="#" >[Edit]</a>-->
- <!-- <a href="#">[Delete]</a>-->
- <!-- </td>-->
- <!-- </tr>-->
- <!-- --><?php //endforeach ?>
- <!-- </table>-->
- <!-- </body>-->
- <!--</html>-->
- <?php
- function paginate($item_per_page, $current_page, $total_records, $total_pages, $page_url)
- {
- $pagination = '';
- if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number
- $pagination .= '<ul class="pagination">';
- $right_links = $current_page + 3;
- $previous = $current_page - 3; //previous link
- $next = $current_page + 1; //next link
- $first_link = true; //boolean var to decide our first link
- if($current_page > 1){
- $previous_link = ($previous==0)?1:$previous;
- $pagination .= '<li class="first"><a href="'.$page_url.'?page=1" title="First">«</a></li>'; //first link
- $pagination .= '<li><a href="'.$page_url.'?page='.$previous_link.'" title="Previous"><</a></li>'; //previous link
- for($i = ($current_page-2); $i < $current_page; $i++){ //Create left-hand side links
- if($i > 0){
- $pagination .= '<li><a href="'.$page_url.'?page='.$i.'">'.$i.'</a></li>';
- }
- }
- $first_link = false; //set first link to false
- }
- if($first_link){ //if current active page is first link
- $pagination .= '<li class="first active">'.$current_page.'</li>';
- }elseif($current_page == $total_pages){ //if it's the last active link
- $pagination .= '<li class="last active">'.$current_page.'</li>';
- }else{ //regular current link
- $pagination .= '<li class="active">'.$current_page.'</li>';
- }
- for($i = $current_page+1; $i < $right_links ; $i++){ //create right-hand side links
- if($i<=$total_pages){
- $pagination .= '<li><a href="'.$page_url.'?page='.$i.'">'.$i.'</a></li>';
- }
- }
- if($current_page < $total_pages){
- $next_link = ($i > $total_pages)? $total_pages : $i;
- $pagination .= '<li><a href="'.$page_url.'?page='.$next_link.'" >></a></li>'; //next link
- $pagination .= '<li class="last"><a href="'.$page_url.'?page='.$total_pages.'" title="Last">»</a></li>'; //last link
- }
- $pagination .= '</ul>';
- }
- return $pagination; //return pagination links
- }
- $db_username = 'root'; //database username
- $db_password = ''; //dataabse password
- $db_name = 'dbpaging'; //database name
- $db_host = 'localhost'; //hostname or IP
- $item_per_page = 5; //item to display per page
- $page_url = "http://localhost/grid/";
- $mysqli_conn = new mysqli($db_host, $db_username, $db_password,$db_name); //connect to MySql
- if ($mysqli_conn->connect_error) { //Output any connection error
- die('Error : ('. $mysqli_conn->connect_errno .') '. $mysqli_conn->connect_error);
- }
- if(isset($_GET["page"])){ //Get page number from $_GET["page"]
- $page_number = filter_var($_GET["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
- if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
- }else{
- $page_number = 1; //if there's no page number, set it to 1
- }
- $results = $mysqli_conn->query("SELECT COUNT(*) FROM tbl_tutorials"); //get total number of records from database
- $get_total_rows = $results->fetch_row(); //hold total records in variable
- $total_pages = ceil($get_total_rows[0]/$item_per_page); //break records into pages
- ################# Display Records per page ############################
- $page_position = (($page_number-1) * $item_per_page); //get starting position to fetch the records
- //Fetch a group of records using SQL LIMIT clause
- $results = $mysqli_conn->query("SELECT tuts_id, tuts_title, tuts_link FROM tbl_tutorials LIMIT $page_position, $item_per_page");
- //Display records fetched from database.
- echo '<ul class="contents">';
- while($row = $results->fetch_assoc()) {
- echo '<li>';
- echo $row["tuts_id"]. '. <strong>' .$row["tuts_title"].'</strong> — '.$row["tuts_link"];
- echo '</li>';
- }
- echo '</ul>';
- ################### End displaying Records #####################
- //create pagination
- echo '<div align="center">';
- // We call the pagination function here.
- echo paginate($item_per_page, $page_number, $get_total_rows[0], $total_pages, $page_url);
- echo '</div>';
- ?>
- <style>
- .pagination{
- margin:0;
- padding:0;
- }
- .pagination li{
- display: inline;
- padding: 6px 10px 6px 10px;
- border: 1px solid #ddd;
- margin-right: -1px;
- font: 13px/20px Arial, Helvetica, sans-serif;
- background: #FFFFFF;
- }
- .pagination li a{
- text-decoration:none;
- color: rgb(89, 141, 235);
- }
- .pagination li.first {
- border-radius: 5px 0px 0px 5px;
- }
- .pagination li.last {
- border-radius: 0px 5px 5px 0px;
- }
- .pagination li:hover{
- background: #EEE;
- }
- .pagination li.current {
- background: #89B3CC;
- border: 1px solid #89B3CC;
- color: #FFFFFF;
- }
- </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement