0
votes

Ok so i have the following page that will be my shopping cart page, and I am having trouble using sessions to take the values from the product arrays on 1 page and create a functioning cart. I don't clearly understand how to use php session variables in order to maintain persistent information such as a shopping cart. I would like to figure out how create the cart using the $_SESSION['cart'] and how to pass the data into it so that I can display the quantity of each item in the cart and have add/remove functionality.

<!DOCTYPE html>
<html lang="en">

  <?php include 'header.php';

    if(!empty($_SESSION['cart'])){

    }
  ?>


    <title>Football Items</title>
    <b><i><h1>Shopping Cart - Checkout</h1><b></i>
    <!-- Bootstrap -->

   <link rel="stylesheet" href="styles/styles1.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

  </head>

  <!--NavStart-->
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="navbar-collapse-1" aria-expanded="false">
      </button>
     <b><a class="navbar-brand" href="index.php">Home Page</a></b>
    </div>


    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="">About Us <span class="sr-only">(current)</span></a></li>


        <li><a href="productlist.php">Football Jerseys</a></li>
        <li class="dropdown">

      <ul class="nav navbar-nav navbar-right">
        <li><a href="Application.php">Checkout</a></li>
        <li class="dropdown">

          </ul>
        </li>
      </ul>
    </div>
  </div>
</nav>



  <div id="wrapper">
  <div id="wrap">


  <?php
      if (!empty($_GET['act'])) 
      {
        if ($_GET['act']=='add') {
            $numr = $_GET['itemid'];
            $name = $_GET['itemname'];
            $quantity = 1;
            $price = $_GET['itemprice'];
            $total = $quantity * $price;
        }
      }
  ?>



  <body>



  <table border="3" style="height: 100% width:200% cellpadding="3" cellspacing="3">
    <tr>
     <th>Item Number</th></p>
      <th>Item Name</th>
      <th>Price</th>
      <th> Quantity </th>
      <th>Total</th>
    </tr>


      <?php
                echo "<tr>";
                echo "<th>$numr</th>";
                echo "<td>$name</td>";
                echo "<td>$price</td>";
                echo "<td>$quantity</td>";
                echo "<td>$total</td>";
                echo "</tr>";

            ?>




  </table>
  </div>
</div>

  </body>

     <div class="container"> 
     <button type="button" style= color:black;background-color:white;" class="btn btn-primary active">
        <a href="form.php">Checkout now.</a></button>

  </div>
</div>


<?php include 'footer.php';?>  
1

1 Answers

1
votes

On your product page you initialize the session, make sure you have session_start() above your html tag.

Then you need to give the session tags a value, like $_SESSION["cart"] = "";

You also need something to add everything in 1 array so you can check the array later on your shopping cart page.

use array_push for that, create an array when first creating the session. Then use array_push to add items to the cart.

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

Outputs: Red, Green, Blue, Yellow

Use this to add products to the array. On your shopping cart page you just simply count how many items are in the array. Make a loop to get them all out and you're done!