Advertisement
crDahaka

Untitled

Nov 17th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.66 KB | None | 0 0
  1. <?php
  2. //
  3. //$db_host = "localhost";
  4. //$db_name = "dbpaging";
  5. //$db_user = "root";
  6. //$db_pass = "";
  7. //
  8. //$db = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
  9. //$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. //
  11. //
  12. //
  13. //$users = $db->query("SELECT * FROM tbl_tutorials LIMIT 0, 10");
  14. //
  15. //
  16. //
  17. //?>
  18. <!--<!DOCTYPE html>-->
  19. <!--<html xmlns="http://www.w3.org/1999/html">-->
  20. <!---->
  21. <!--<head>-->
  22. <!---->
  23. <!--</head>-->
  24. <!---->
  25. <!--    <body>-->
  26. <!---->
  27. <!--        Username: <input type="text" value=""> </br>-->
  28. <!--        Email: <input type="text">-->
  29. <!---->
  30. <!--        <input type="submit" value="Search">-->
  31. <!---->
  32. <!---->
  33. <!--        <table border="1" align="center">-->
  34. <!--            <tr>-->
  35. <!--                <th>ID</th>-->
  36. <!--                <th>Title</th>-->
  37. <!--                <th>Link</th>-->
  38. <!--                <th>Actions</th>-->
  39. <!--            </tr>-->
  40. <!--            --><?php //foreach ($users as $user) : ?>
  41. <!--                <tr>-->
  42. <!--                    <td>--><?//= $user['tuts_id'] ?><!--</td>-->
  43. <!--                    <td>--><?//= htmlspecialchars($user['tuts_title'])?><!--</td>-->
  44. <!--                    <td>--><?//= htmlspecialchars($user['tuts_link'])?><!--</td>-->
  45. <!---->
  46. <!--                    <td>-->
  47. <!--                        <a href="#" >[Edit]</a>-->
  48. <!--                        <a href="#">[Delete]</a>-->
  49. <!--                    </td>-->
  50. <!--                </tr>-->
  51. <!--            --><?php //endforeach ?>
  52. <!--        </table>-->
  53. <!--    </body>-->
  54. <!--</html>-->
  55.  
  56.  
  57. <?php
  58.  
  59. function paginate($item_per_page, $current_page, $total_records, $total_pages, $page_url)
  60. {
  61.     $pagination = '';
  62.     if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number
  63.         $pagination .= '<ul class="pagination">';
  64.  
  65.         $right_links    = $current_page + 3;
  66.         $previous       = $current_page - 3; //previous link
  67.         $next           = $current_page + 1; //next link
  68.         $first_link     = true; //boolean var to decide our first link
  69.  
  70.         if($current_page > 1){
  71.             $previous_link = ($previous==0)?1:$previous;
  72.             $pagination .= '<li class="first"><a href="'.$page_url.'?page=1" title="First">&laquo;</a></li>'; //first link
  73.             $pagination .= '<li><a href="'.$page_url.'?page='.$previous_link.'" title="Previous">&lt;</a></li>'; //previous link
  74.             for($i = ($current_page-2); $i < $current_page; $i++){ //Create left-hand side links
  75.                 if($i > 0){
  76.                     $pagination .= '<li><a href="'.$page_url.'?page='.$i.'">'.$i.'</a></li>';
  77.                 }
  78.             }
  79.             $first_link = false; //set first link to false
  80.         }
  81.  
  82.         if($first_link){ //if current active page is first link
  83.             $pagination .= '<li class="first active">'.$current_page.'</li>';
  84.         }elseif($current_page == $total_pages){ //if it's the last active link
  85.             $pagination .= '<li class="last active">'.$current_page.'</li>';
  86.         }else{ //regular current link
  87.             $pagination .= '<li class="active">'.$current_page.'</li>';
  88.         }
  89.  
  90.         for($i = $current_page+1; $i < $right_links ; $i++){ //create right-hand side links
  91.             if($i<=$total_pages){
  92.                 $pagination .= '<li><a href="'.$page_url.'?page='.$i.'">'.$i.'</a></li>';
  93.             }
  94.         }
  95.         if($current_page < $total_pages){
  96.             $next_link = ($i > $total_pages)? $total_pages : $i;
  97.             $pagination .= '<li><a href="'.$page_url.'?page='.$next_link.'" >&gt;</a></li>'; //next link
  98.             $pagination .= '<li class="last"><a href="'.$page_url.'?page='.$total_pages.'" title="Last">&raquo;</a></li>'; //last link
  99.         }
  100.  
  101.         $pagination .= '</ul>';
  102.     }
  103.     return $pagination; //return pagination links
  104. }
  105.  
  106.  
  107.  
  108.  
  109. $db_username        = 'root'; //database username
  110. $db_password        = ''; //dataabse password
  111. $db_name            = 'dbpaging'; //database name
  112. $db_host            = 'localhost'; //hostname or IP
  113. $item_per_page      = 5; //item to display per page
  114. $page_url           = "http://localhost/grid/";
  115.  
  116. $mysqli_conn = new mysqli($db_host, $db_username, $db_password,$db_name); //connect to MySql
  117. if ($mysqli_conn->connect_error) { //Output any connection error
  118.     die('Error : ('. $mysqli_conn->connect_errno .') '. $mysqli_conn->connect_error);
  119. }
  120.  
  121. if(isset($_GET["page"])){ //Get page number from $_GET["page"]
  122.     $page_number = filter_var($_GET["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
  123.     if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
  124. }else{
  125.     $page_number = 1; //if there's no page number, set it to 1
  126. }
  127.  
  128.  
  129. $results = $mysqli_conn->query("SELECT COUNT(*) FROM tbl_tutorials"); //get total number of records from database
  130. $get_total_rows = $results->fetch_row(); //hold total records in variable
  131.  
  132. $total_pages = ceil($get_total_rows[0]/$item_per_page); //break records into pages
  133.  
  134. ################# Display Records per page ############################
  135. $page_position = (($page_number-1) * $item_per_page); //get starting position to fetch the records
  136. //Fetch a group of records using SQL LIMIT clause
  137. $results = $mysqli_conn->query("SELECT tuts_id, tuts_title, tuts_link FROM tbl_tutorials LIMIT $page_position, $item_per_page");
  138.  
  139. //Display records fetched from database.
  140.  
  141. echo '<ul class="contents">';
  142. while($row = $results->fetch_assoc()) {
  143.     echo '<li>';
  144.     echo  $row["tuts_id"]. '. <strong>' .$row["tuts_title"].'</strong> &mdash; '.$row["tuts_link"];
  145.     echo '</li>';
  146. }
  147. echo '</ul>';
  148. ################### End displaying Records #####################
  149.  
  150. //create pagination
  151. echo '<div align="center">';
  152. // We call the pagination function here.
  153. echo paginate($item_per_page, $page_number, $get_total_rows[0], $total_pages, $page_url);
  154. echo '</div>';
  155.  
  156. ?>
  157.  
  158. <style>
  159.     .pagination{
  160.         margin:0;
  161.         padding:0;
  162.     }
  163.     .pagination li{
  164.         display: inline;
  165.         padding: 6px 10px 6px 10px;
  166.         border: 1px solid #ddd;
  167.         margin-right: -1px;
  168.         font: 13px/20px Arial, Helvetica, sans-serif;
  169.         background: #FFFFFF;
  170.     }
  171.     .pagination li a{
  172.         text-decoration:none;
  173.         color: rgb(89, 141, 235);
  174.     }
  175.     .pagination li.first {
  176.         border-radius: 5px 0px 0px 5px;
  177.     }
  178.     .pagination li.last {
  179.         border-radius: 0px 5px 5px 0px;
  180.     }
  181.     .pagination li:hover{
  182.         background: #EEE;
  183.     }
  184.  
  185.     .pagination li.current {
  186.         background: #89B3CC;
  187.         border: 1px solid #89B3CC;
  188.         color: #FFFFFF;
  189.     }
  190. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement