nstruth2

Can't Figure Out PHP & CSS

Jul 20th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 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.                         <?php for ( $x=1; $x <= $pages; $x++ ): ?>
  50.                         <ul class="pagination">
  51.                             <li>
  52.                                 <a href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage; ?>">
  53.                                     <?php if ($page == $x) {
  54.                                         echo "<a style='color:blue;'>" . $x . "</a>"; } else {
  55.                                     echo $x;
  56.                                 }
  57.                                 ?>
  58.                                 </a>
  59.                             </li>
  60.                         </ul>
  61.                         <?php endfor; ?>
  62.                     </div>
  63.                 </div>
  64.             </div>
  65.         </div><!--end main container-->
  66.     </body>
  67. </html>
  68.    
Advertisement
Add Comment
Please, Sign In to add comment