Advertisement
nstruth2

Attempted Fix

Jul 20th, 2023
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.05 KB | None | 0 0
  1. <?php
  2.  
  3. include 'config.php';
  4.  
  5. // user input
  6. $page    = isset( $_GET['page'] ) ? (int) $_GET['page'] : 1;
  7. $perPage = isset( $_GET['per-page'] ) && $_GET['per-page'] <= 50 ? (int) $_GET['per-page'] : 5;
  8.  
  9. // positioning
  10. $start = ( $page > 1 ) ? ( $page * $perPage ) - $perPage : 0;
  11.  
  12. // query
  13. $articles = $conn->prepare( "SELECT SQL_CALC_FOUND_ROWS * FROM articles LIMIT {$start}, {$perPage}" );
  14. $articles->execute();
  15. $articles = $articles->fetchAll( PDO::FETCH_ASSOC );
  16.  
  17. // pages
  18. $total = $conn->query( "SELECT FOUND_ROWS() as total" )->fetch()['total'];
  19. $pages = ceil( $total / $perPage );
  20.  
  21. ?>
  22.  
  23. <!DOCTYPE html>
  24. <html>
  25.     <head>
  26.         <meta charset="utf-8">
  27.         <!-- Latest compiled and minified CSS -->
  28.         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  29.         <!-- Latest compiled and minified JavaScript -->
  30.         <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  31.         <!--custom css-->
  32.         <link rel="stylesheet" type="text/css" href="style.css">
  33.         <title>php pagination</title>
  34.     </head>
  35.     <body>
  36.         <div class="container">
  37.             <div class="col-md-12">
  38.                 <?php foreach ( $articles as $article ): ?>
  39.                 <div class="article">
  40.                     <p class="lead">
  41.                         <?php echo $article['id']; ?>: <?php echo $article['title']; ?>
  42.                     </p>
  43.                 </div>
  44.                 <?php endforeach ?>
  45.             </div>
  46.             <div class="col-md-12">
  47.                 <div class="well well-sm">
  48.                     <div class="paginate">
  49. <ul class="pagination">
  50. <?php for ( $x=1; $x <= $pages; $x++ ): ?>
  51.     <?php $currentStyle = ($x === $page) ? 'class="page-selected"' : ''; ?>
  52.     <li>
  53.         <a <?= $currentStyle ?> href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage; ?>"><?= $x ?></a>
  54.     </li>
  55. <?php endfor; ?>
  56. </ul>
  57.                     </div>
  58.                     </div>
  59.                 </div>
  60.             </div>
  61.         </div><!--end main container-->
  62.     </body>
  63. </html>
  64.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement