Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- $product_ids = array();
- session_destroy();
- // Check if the Add to Card button has been submited
- if ($_POST['add_to_cart']) {
- echo"prvi if";
- if (isset($_SESSION['shopping_cart'])) {
- echo "nekaj je bilo dodano v košarico";
- //keep track of how many products are in the cart
- $count = count($_SESSION['shopping_cart']);
- $product_ids = array_column($_SESSION['shopping cart'], 'id');
- pre_r($product_ids);
- } else { // if the shopping cart doesnt exist, create first product with array key 0
- //create array using submitted form data, start from key 0 and fill it with values
- echo"else";
- $_SESSION['shopping_cart'][0] = array(
- "id" => $_GET['id'],
- 'name' => $_POST['name'],
- 'price' => $_POST['price'],
- 'quantity' => $_POST['quantity'],
- );
- }
- }
- pre_r($_SESSION);
- print_r($_SESSION);
- function pre_r($array)
- {
- echo '<pre>';
- print_r($array);
- echo '</pre>';
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Online Shopping</title>
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
- crossorigin="anonymous">
- <link rel="stylesheet" href="cart.css">
- </head>
- <body>
- <div class="container">
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "cart";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "SELECT * FROM products ORDER by id ASC";
- $result = $conn->query($sql);
- echo "<div class='row'>";
- if ($result->num_rows > 0) {
- // output data of each row
- while ($product = $result->fetch_assoc()) {
- ?>
- <div class="col-sm-4 col-md-3">
- <form action="cart.php?action=add&id=<?php echo $product[ 'id' ]; ?>"
- method="post">
- <div class="products">
- <img class="img-responsive" src="<?php echo $product['image']; ?>"
- style="width:100%" />
- <h4 class="text-info">
- <?php echo $product['name']; ?>
- </h4>
- <h4> €
- <?php echo $product['price']; ?>
- </h4>
- <input type="text" name="quantity" class="form-control" value="1" style="width:25%" />
- <input type="hidden" name="name" value="<?php echo $product['name']; ?>" />
- <input type="hidden" name="price" value="<?php echo $product['price']; ?>" />
- <input type="submit" name="add_to_cart" class="btn btn-info" value="Add to Cart" />
- </div>
- </form>
- </div>
- <?php
- // tole je var_dumb (pokaže kaj je napisano v mySql) -> print_r($product);
- }
- } else {
- echo "0 results";
- }
- echo "</div>";
- $conn->close();
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement