How to do this..

in my cart implementation… am getting all the selected produts total…

Now i wants to display this total and number of items in cart…in each every page user visits until he go to checkout…

my code as follows…

if (count($_SESSION["cart"]) <1) 
    {
       	$msg="Your Cart is empty";
    }

if (isset($_GET["shopping_cart"]) || isset($_POST["shopping_cart"]))
	{

 if (isset($_GET["productID"]) && (int)$_GET["productID"]>0) //add product to cart with productID=$_GET["productID"]
    {
		
		//$is = $is[0];

        //$_SESSION['cart'] contains product IDs
        //$_SESSION['counts'] contains item quantities ($_SESSION['counts'][$i] corresponds to $_SESSION['cart'][$i])
        //$_SESSION['cart'][$i] == 0 means $i-element is 'empty' (does not refer to any product)
        if (!isset($_SESSION["cart"]))
        {
            $_SESSION["cart"] = array();
            $_SESSION["counts"] = array();
        }
        //check for current product in visitor's shopping cart content
        $i=0;
        while ($i<count($_SESSION["cart"]) && $_SESSION["cart"][$i] != $_GET["productID"]){
		
		 $i++;
		 }
        if ($i < count($_SESSION["cart"])) //increase current product's item quantity
        {
           $_SESSION["counts"][$i]++;
		}
        else //no such product in the cart - add it
        {
            $_SESSION["cart"][] = $_GET["productID"];
			$_SESSION["counts"][] = 1;
					
        }
    }

//================================ remove product with productID == $remove =================

    if (isset($_GET["remove"]) && (int)$_GET["remove"] > 0) 
    {
        $i=0;
        while ($i<count($_SESSION["cart"]) && $_SESSION["cart"][$i] != (int)$_GET["remove"])
            $i++;
        if ($i<count($_SESSION["cart"]))
            $_SESSION["cart"][$i] = 0;
    }
	
//===================================================================================
	
//================================ update shopping cart content ============================
	if (isset($_POST["update"])) //
		{
			foreach ($_POST as $key => $val)
				
				if (strstr($key, "count_"))
				{
				 
					if ($val > 0)
					{
						for ($i=0; $i<count($_SESSION["cart"]); $i++)
						{
							if ($_SESSION["cart"][$i] == str_replace("count_","",$key))
							{
								$_SESSION["counts"][$i] = floor($val);
							}
						}
					}
					else //remove
					{
						$i=0;
						while ($_SESSION["cart"][$i] != str_replace("count_","",$key) && $i<count($_SESSION["cart"])) $i++;
						$_SESSION["cart"][$i] = 0;
					}
				  }

					$location="<script>window.location='mycart.php?shopping_cart=yes'</script>";
					echo $location;
					unset($location);
			}
//===================================================================================
	 
//================================ clear whole cart ======================================
	
	if (isset($_GET["clear"]) && $_GET["clear"] =='clear') 
    {
       	unset($_SESSION["cart"]);
    	unset($_SESSION["counts"]);
		$msg="Your Cart is empty";
    }
	
//===================================================================================

//================================ save cart ===========================================
	 if (isset($_GET["save"]) && $_GET["save"] =='save') //save cart
    {
       	if(!isset($_SESSION['email'])){
			
			$location="<script>window.location='login.php?goto=".$_SERVER['PHP_SELF']."'</script>";
			echo $location;
			unset($location);
		}
		else{
			$location="<script>window.location='save.php'</script>";
			echo $location;
			unset($location);
		}
    }
//===================================================================================
	}

and inside the for loop.

..................
..................

$tmp = array(
                    $_SESSION["cart"][$i],
                    $arr_items['txtname'],
                    $_SESSION["counts"][$i],
                    ($_SESSION["counts"][$i]*$pri_vat)." ".$currency_iso_3
                  ); 
		
		 $k += $_SESSION["counts"][$i]*$pri_vat;

.............................
.............................

can anyone help with this issue pls…

[php]echo ‘You have ‘.array_sum($_SESSION[‘counts’]).’ items in ur shopping card’;[/php]
http://php.net/array_sum

of cause u may use an if to make special outputs for 0 and 1 items

thanks for the reply…

any i need to print $k value along with the items in the cart…

that is total amount($k) for the current items…

how can i do that???

Sponsor our Newsletter | Privacy Policy | Terms of Service