Guest User

Untitled

a guest
May 19th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>P2 PHP PRODUCTS</title>
  6. </head>
  7. <body>
  8. <form method="post">
  9.     <input type="submit" name="direction" value="Previous"/>
  10.     <input type="submit" name="direction" value="Next"/>
  11.     <br>
  12.     <label>
  13.         How many products should be on a page:
  14.         <select name="productsCount">
  15.             <?php
  16.             $user = 'root';
  17.             $pass = '';
  18.             $dbName = 'pw';
  19.             $host = '127.0.0.1';
  20.             $connection = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
  21.             $query = 'SELECT * FROM laptops';
  22.             $result = $connection->query($query);
  23.             for ($i = 1; $i <= mysqli_num_rows($result); $i++) {
  24.                 if (isset($_POST['productsCount'])) {
  25.                     if ($i == (int)$_POST['productsCount']) {
  26.                         echo '<option selected value="' . $i . '">' . $i . '</option>';
  27.                     } else {
  28.                         echo '<option value="' . $i . '">' . $i . '</option>';
  29.                     }
  30.                 } else {
  31.                     echo '<option value="' . $i . '">' . $i . '</option>';
  32.                 }
  33.             }
  34.             ?>
  35.         </select>
  36.     </label>
  37.     <input type="hidden" name="page"
  38.         <?php
  39.         // If there is no page we put a default "0" page
  40.         if (!isset($_POST['page'])) echo "value=\"0\"";
  41.         else {
  42.             // We check the direction
  43.             if (isset($_POST['direction'])) {
  44.                 // If we go back we decrement the page number
  45.                 if ($_POST['direction'] == 'Previous') {
  46.                     $pageNumber = (int)$_POST['page'];
  47.                     if ($pageNumber > 0) {
  48.                         $pageNumber -= 1;
  49.                     }
  50.                     echo 'value="' . $pageNumber . '"';
  51.                 }
  52.                 // If we go foreword we increment the page number
  53.                 if ($_POST['direction'] == 'Next') {
  54.                     $user = 'root';
  55.                     $pass = '';
  56.                     $dbName = 'pw';
  57.                     $host = '127.0.0.1';
  58.                     $connection = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
  59.                     $query = 'SELECT * FROM laptops';
  60.                     $result = $connection->query($query);
  61.                     $numberOfElements = mysqli_num_rows($result);
  62.                     // We calculate the number of element to be sure we don't go past the maximum number of pages
  63.  
  64.                     $pageNumber = (int)$_POST['page'];
  65.                     if ($pageNumber < ceil($numberOfElements / (int)$_POST['productsCount'] - 1)) {
  66.                         $pageNumber += 1;
  67.                     }
  68.                     echo 'value="' . $pageNumber . '"';
  69.                 }
  70.             }
  71.         }
  72.         ?>/>
  73. </form>
  74. <?php
  75. // Check if the a button was pressed
  76. if (isset($_POST['direction'])) {
  77.     $page = (int)$_POST['page'];
  78.     $numberOfElements = (int)$_POST['productsCount'];
  79.     $offset = $page * $numberOfElements;
  80.  
  81.     $user = 'root';
  82.     $pass = '';
  83.     $dbName = 'pw';
  84.     $host = '127.0.0.1';
  85.     $connection = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
  86.     $query = "SELECT * FROM laptops LIMIT ?, ?";
  87.     $stmt = $connection->prepare($query);
  88.     if ($stmt === false) {
  89.         trigger_error('Wrong SQL!', E_USER_ERROR);
  90.     }
  91.  
  92.     $stmt->bind_param('ii', $offset, $numberOfElements);
  93.     $stmt->execute();
  94.     $result = $stmt->get_result();
  95.     echo '<br><div>';
  96.     if (mysqli_num_rows($result) > 0) {
  97.         while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  98.             echo $row['Producator'] . ' ' . $row['Procesor'] . ' ' . $row['Memorie'] . ' ' . $row['HDD'] . ' ' . $row['PlacaVideo'] . '<br/>';
  99.         }
  100.         echo '</div>';
  101.     }
  102. }
  103. ?>
  104. </body>
  105. </html>
Add Comment
Please, Sign In to add comment