Jump to content

shopping cart


westminster86

Recommended Posts

Back again...

 

Im having a little trouble with my shopping cart script ???. When adding an item to the cart, it retrieves and displays the size submitted from the form. However, the size for previous items added to the cart change to the size of the most recent item added to the cart. So items in the cart have the same sizes. Where am i going wrong :-[

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

can we see some code. It does help

 

<?php

  // The shopping cart needs sessions, so start one

  session_start();

 

  @ $new = $_GET['new'];

 

  if($new)

  {

    //new item selected

    if(!isset($_SESSION['cart']))

    {

      $_SESSION['cart'] = array();

      $_SESSION['items'] = 0;

      $_SESSION['total_price'] ='0.00';

    }

 

    if(isset($_SESSION['cart'][$new]))

      $_SESSION['cart'][$new]++;

    else

      $_SESSION['cart'][$new] = 1;

 

    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);

    $_SESSION['items'] = calculate_items($_SESSION['cart']);

  }

 

  if(isset($_POST['save']))

  {

    foreach ($_SESSION['cart'] as $productid => $qty)

    {

      if($_POST[$productid]=='0')

        unset($_SESSION['cart'][$productid]);

      else

        $_SESSION['cart'][$productid] = $_POST[$productid];

    }

    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);

    $_SESSION['items'] = calculate_items($_SESSION['cart']);

  }

 

  do_html_header('Your shopping cart');

 

  if($_SESSION['cart']&&array_count_values($_SESSION['cart']))

    display_cart($_SESSION['cart']);

  else

  {

    echo '<p>There are no items in your cart</p>';

    echo '<hr />';

  }

  $target = 'index.php';

 

  // if we have just added an item to the cart

  // continue shopping in that category

  if($new)

  {

    $details = get_product_details($new);

    if($details['catid'])

      $target = 'showCat.php?catid='.$details['catid'];

  }

 

  display_button($target, 'continue-shopping', 'Continue Shopping');

  display_button('checkout.php', 'go-to-checkout', 'Go To Checkout');

 

  do_html_footer();

 

function display_cart($cart, $change = true, $images = 1)

{

  // display items in shopping cart

  // optionally allow changes (true or false)

  // optionally include images (1 - yes, 0 - no)

 

  echo '<table border = "0" cellspacing = "0" width = "100%">

      <form action = "showCart.php" method = "post">

      <tr><th colspan = '. (1+$images) .' bgcolor="#cccccc">Item</th><th bgcolor="#cccccc">Size</th>

      <th bgcolor="#cccccc">Price</th><th bgcolor="#cccccc">Quantity</th>

      <th bgcolor="#cccccc">Total</th></tr>';

 

  // display each item as a table row

  foreach ($cart as $productid => $qty)

  {

    $product = get_product_details($productid);

    echo '<tr>';

    if($images ==true)

    {

      echo '<td align = left>';

      if (file_exists("$productid.jpg"))

      {

        $size = GetImageSize($productid.'.jpg');

        if($size[0]>0 && $size[1]>0)

        {

          echo '<img src="'.$productid.'.jpg" border="0" ';

          echo 'width = '. $size[0]/9 .' height = '. $size[1]/9 .' />';

        }

    }

    else

      echo ' ';

    echo '</td>';

  }

  echo '<td align = "left">';

  echo '<a href = "showProduct.php?productid='.$productid.'">'

      .$product['name'].'</a>';

  echo '</td><td align = "center">'.$_POST['size'];

  echo '</td><td align = "center">£'.number_format($product['price'], 2);

  echo '</td><td align = "center">';

  // if we allow changes, quantities are in text boxes

  if ($change == true)

    echo "<input type = 'text' name = \"$productid\" value = \"$qty\" size = \"3\" />";

  else

    echo $qty;

  echo '</td><td align = "center">£'.number_format($product['price']*$qty,2)

      ."</td></tr>\n";

  }

  // display total row

  echo "<tr>

          <th colspan = ". (3+$images) ." bgcolor=\"#cccccc\"> </td>

          <th align = \"center\" bgcolor=\"#cccccc\">

              ".$_SESSION['items']."

          </th>

          <th align = \"center\" bgcolor=\"#cccccc\">

              £".number_format($_SESSION['total_price'], 2).

          '</th>

        </tr>';

  // display save change button

  if($change == true)

  {

    echo '<tr>

            <td colspan = '. (3+$images) .'> </td>

            <td align = "center">

              <input type = "hidden" name = "save" value = "true" />

              <input type = "image" src = "save-changes.gif"

                    border = "0" alt = "Save Changes" />

            </td>

            <td> </td>

          </tr>';

  }

  echo '</form></table>';

 

}

 

Link to comment
https://forums.phpfreaks.com/topic/84460-shopping-cart/#findComment-430290
Share on other sites

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.