Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>P2 PHP PRODUCTS</title>
- </head>
- <body>
- <form method="post">
- <input type="submit" name="direction" value="Previous"/>
- <input type="submit" name="direction" value="Next"/>
- <br>
- <label>
- How many products should be on a page:
- <select name="productsCount">
- <?php
- $user = 'root';
- $pass = '';
- $dbName = 'pw';
- $host = '127.0.0.1';
- $connection = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
- $query = 'SELECT * FROM laptops';
- $result = $connection->query($query);
- for ($i = 1; $i <= mysqli_num_rows($result); $i++) {
- if (isset($_POST['productsCount'])) {
- if ($i == (int)$_POST['productsCount']) {
- echo '<option selected value="' . $i . '">' . $i . '</option>';
- } else {
- echo '<option value="' . $i . '">' . $i . '</option>';
- }
- } else {
- echo '<option value="' . $i . '">' . $i . '</option>';
- }
- }
- ?>
- </select>
- </label>
- <input type="hidden" name="page"
- <?php
- // If there is no page we put a default "0" page
- if (!isset($_POST['page'])) echo "value=\"0\"";
- else {
- // We check the direction
- if (isset($_POST['direction'])) {
- // If we go back we decrement the page number
- if ($_POST['direction'] == 'Previous') {
- $pageNumber = (int)$_POST['page'];
- if ($pageNumber > 0) {
- $pageNumber -= 1;
- }
- echo 'value="' . $pageNumber . '"';
- }
- // If we go foreword we increment the page number
- if ($_POST['direction'] == 'Next') {
- $user = 'root';
- $pass = '';
- $dbName = 'pw';
- $host = '127.0.0.1';
- $connection = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
- $query = 'SELECT * FROM laptops';
- $result = $connection->query($query);
- $numberOfElements = mysqli_num_rows($result);
- // We calculate the number of element to be sure we don't go past the maximum number of pages
- $pageNumber = (int)$_POST['page'];
- if ($pageNumber < ceil($numberOfElements / (int)$_POST['productsCount'] - 1)) {
- $pageNumber += 1;
- }
- echo 'value="' . $pageNumber . '"';
- }
- }
- }
- ?>/>
- </form>
- <?php
- // Check if the a button was pressed
- if (isset($_POST['direction'])) {
- $page = (int)$_POST['page'];
- $numberOfElements = (int)$_POST['productsCount'];
- $offset = $page * $numberOfElements;
- $user = 'root';
- $pass = '';
- $dbName = 'pw';
- $host = '127.0.0.1';
- $connection = new mysqli($host, $user, $pass, $dbName) or die("Can't connect");
- $query = "SELECT * FROM laptops LIMIT ?, ?";
- $stmt = $connection->prepare($query);
- if ($stmt === false) {
- trigger_error('Wrong SQL!', E_USER_ERROR);
- }
- $stmt->bind_param('ii', $offset, $numberOfElements);
- $stmt->execute();
- $result = $stmt->get_result();
- echo '<br><div>';
- if (mysqli_num_rows($result) > 0) {
- while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
- echo $row['Producator'] . ' ' . $row['Procesor'] . ' ' . $row['Memorie'] . ' ' . $row['HDD'] . ' ' . $row['PlacaVideo'] . '<br/>';
- }
- echo '</div>';
- }
- }
- ?>
- </body>
- </html>
Add Comment
Please, Sign In to add comment