Jump to content

Help with Shopping Cart


Mr.Noob

Recommended Posts

Hi there,

 

I am having trouble getting my shopping cart on my website to function properly. This is the first time I've ever used PHP but I've gotten the hang of allowing users to register, login and logout.

 

However, I am trying to make my shopping cart storing information using session arrays. The problems I am having are that when I add an item to my shopping cart, it adds 0 to it first and if I click it again, then it adds 1 to it and then it is ok. Another problem I am having is removing items from my cart. Can someone please help me understand what I can do to fix these two problems?

 

My code for my cart.php is below:

 

              <?php
              echo "<h2>Welcome to your shopping cart " . $_SESSION['user']."</h2>";
              echo "<br>You have the following items in your shopping cart:";

              if(isset($_POST['submit']))
              {

              $itemname = $_POST['h1'];

              //$_SESSION['itemname'][$itemname];
              unset($_SESSION['itemqty'][$itemname]);
              unset($_SESSION['itemprice'][$itemname]);
              unset($_SESSION['itemname'][$itemname]);
              }

              echo "<br/><br/>";
              echo "<table border='1'>";
              echo "<tr><th>Name</th><th>Quantity</th><th>Price</th><th>Total Price</th>
              <th>Remove</th></tr>";
              foreach($_SESSION['itemname'] as $key=>$value)
              {

              echo '<tr><td>'.$_SESSION['itemname'][$key].'</td>
              <td><input type="text" name="t1" value='.$_SESSION['itemqty'][$key].'></td>
              <td>$'.$_SESSION['itemprice'][$key].'</td>
              <td>$'.($_SESSION['itemqty'][$_SESSION['item']] * $_SESSION['itemprice'] 
               [$_SESSION['item']]).'</td>
              <td><form id="f1" method="post" name="f1">
              <input type="submit" name="submit" value ="delete">
              <input type="hidden" name="h1" value='.$key.'></td></tr>';
             
              }
              echo "</table>";

              ?>
              
              <br/><a href="checkoutval.php">Checkout</a>

 

And here is code from one of my product pages, adding it to my cart:

 

<?php

session_start();


if (isset($_POST['submit']))
{

  if($_SESSION['item']==$_POST['h1'])
{

  $_SESSION['qty'] = $_SESSION['qty'] + 1;
  
}
else
{
  $_SESSION['item'] = $_POST['h1'];
  $_SESSION['price']= $_POST['h2'];
}

$_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item'];
$_SESSION['itemqty'][$_SESSION['item']] = $_SESSION['qty'];
$_SESSION['itemprice'][$_SESSION['item']] = $_SESSION['price'];

}


?>

                       Add to cart button:

                       <form action="nokia-n97.php" method="post">
                       <input type="hidden" name="h1" value="Nokia N97" />
                       <input type="hidden" name="h2" value="979"/>
                       <input type="submit" name="submit" value="Add to Cart"/></form> 

 

Also since this is my first time using PHP can you please try to explain things clearly to me? Haha I just need to make sure I know what I have to fix up.

Link to comment
https://forums.phpfreaks.com/topic/177263-help-with-shopping-cart/
Share on other sites

Hi,

 

Not 100% sure but it may be to do with the;


  if($_SESSION['item']==$_POST['h1'])
{

  $_SESSION['qty'] = $_SESSION['qty'] + 1;
  
}

 

try changing it to

<?php

session_start();


if (isset($_POST['submit'])) {
    
    if($_SESSION['item']==$_POST['h1']) {
        
        if ($_SESSION['qty'] > 0) {
            
            $_SESSION['qty']++;
        }
        else {
            $_SESSION['qty'] = 1;
        }
    }
    else {
        $_SESSION['item'] = $_POST['h1'];
        $_SESSION['price']= $_POST['h2'];
    }
    
    $_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item'];
    $_SESSION['itemqty'][$_SESSION['item']] = $_SESSION['qty'];
    $_SESSION['itemprice'][$_SESSION['item']] = $_SESSION['price'];

}


?>

 

 

All i have done is make sure it is greater than zero before adding to it if not then make it one.

 

see how it goes

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.