Please help how to add multiple items to cart

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
prasitc2005
Forum Commoner
Posts: 42
Joined: Thu Jul 13, 2006 7:14 am

Please help how to add multiple items to cart

Post by prasitc2005 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all Gurus

I need you to help me adjusting scripts for my bespoke website. I want to be able to add multiple items to a cart so client can shop and pick and just click on button

At a moment I can only pick one and add one to the cart.

Here's my code for showcart.php

Code: Select all

<?
session_start();
require_once("include/function.php");
require_once("include/connect_db.php");
if (empty($cart)) $cart =NULL;
if (empty($index)) $index=NULL;
if (empty($new)) $new=NULL;
if($new)
{
	if(!session_is_registered("cart"))
	{
		$cart = array();
		session_register("cart");
		$items=0;
		$total_price = "0.00";
		session_register("items");
		session_register("total_price");
	}
	if($cart[$new])
		$cart[$new]++;
	else
		$cart[$new] = 1;
	$total_price = calculate_price($cart);
	$items = calculate_items($cart);
}
else
{
if($save)
{
	foreach($cart as $code=>$qty)
	{
		if($$code=="0")
			unset($cart[$code]);
		else
			$cart[$code] = $$code;
	}
	$total_price = calculate_price($cart);
	$items = calculate_items($cart);
}
}

require_once("headshopcart.php");
	if($cart&&array_count_values($cart))
			display_cart($cart,true);
		else
	{
	echo "<center>The cart is empty!</center><br>";
	}
?>

Here's my code for funtion.php;

Code: Select all

function calculate_price($cart)
{
  $price = 0.0;
  if(is_array($cart))
  {
    $conn = connect_db("yourserver");
    foreach($cart as $code => $qty)
    {  
      $query = "select price from product_line where code='$code'";
      $result = mysql_query($query);
      if ($result)
      {
        $item_price = @mysql_result($result, 0, "price");
        $price +=$item_price*$qty;
      }
    }
  }
  return $price;
}

//=================================================================================

/*???????????????????????????*/
function calculate_items($cart)
{
  // ??????????????????????
  $items = 0;
  if(is_array($cart))
  {
    foreach($cart as $code => $qty)
    {  
      $items += $qty;
    }
  }
  return $items;
}

//=================================================================================

//display selected items
function display_cart($cart,$change)
{
  global $items;
  global $total_price;
  echo "<h1><div align=center>Shopping Cart</div></h1>";
  echo "<table  border=1 cellpadding=0 cellspacing=0 width = 50% bordercolor=#eeeeee align=center>
            <form action = ../show_cart.php method = post>
            <tr bordercolor=#CCCCFF bgcolor=#eeeeee>
              <th width=22><div align=center><strong>Name</strong></div></th>
              <th width=51><div align=center><strong>Size</strong></div></th>
              <th width=51><div align=center><strong>Colour</strong></div></th>
              <th width=126><div align=center>Price</div></th>
              <th width=83><div align=center><strong>Quantity</strong></div></th>
              <th width=89><div align=center><strong>Total Price</strong></a></div></th>
            </tr>
             ";
		  foreach ($cart as $code => $qty)
  {
    $product = get_product_details($code);
        echo "<tr>";
    echo "<td>";
    echo "<a href = \"../price.php?code=".$code."\"><font face=\"MS Sans Serif\" size=2>".$product["name"]."</font></a></td>" ;
	echo "<td><font face=\"MS Sans serif\" size=2>".$product["size"]."</font></td>";
    echo "<td><font face=\"MS Sans serif\" size=2>".$product["colour"]."</font></td>";
    echo "<td align = center><font face=\"MS Sans Serif\" size=2>".number_format($product["price"], 2) ."</font></td>";
    echo "<td align = center>";
    if ($change == true)
      echo "<input type = text name = \"$code\" value = $qty size = 2>";
    else
      echo "<font face=\"MS Sans serif\" size=2>".$qty."</font>";
    echo "</td><td align = right><font face=\"MS Sans Serif\" size=2>" .number_format($product["price"]*$qty,2). " &pound;</font></td></tr>\n";
  }
  // ????????????????
  echo "<tr bgcolor=#FFAAAA>
          <th align = left colspan=4>Grand Total</th>
          <th align = center>$items</th>
          <th align = right>".number_format($total_price, 2)." &pound;</th></tr>";
  if($change == true)
  {
    echo "<tr>
            <td colspan =5>&nbsp;</td>
            <td align = center>
              <input type = hidden name = new value = 0 >  
              <input type = hidden name = save value = true>  
              <input type = submit value = \"Update Items\">
            </td>
            <td>&nbsp;</td>
        </tr>";
  }
  echo "</form></table>";
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply