malatamil 9 Junior Poster

i have a add to cart website, in that am getting some problem. if i choose available pack size is 100 Grm,quantity-1 and i add to cart this product. again if i choose different product name,pack size is 200 Grm,quantity-2 and add to cart its changed to 200 Grm for first product also.that pack size is not fixed for every product while am selecting. someone help me

order.php

<script language="javascript" type="text/javascript">
    function getXMLHTTP() { //fuction to return the xml http object
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }

    function getBrand_amount(Brand_amountId) {      
        var brandid = document.getElementById('brandid').value;
        var strURL="availablepack.php?Brand_amountId="+Brand_amountId+"&id="+brandid;
        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('available').innerHTML=req.responseText;                        
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }       
    }
    </script>



 <?php
    $current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

if(isset($_SESSION["product"]))
{
    $total = 0;
    $total1 =0;
    echo '<ol>';
    foreach ($_SESSION["product"] as $cart_itm)
    {
        echo '<li class="cart-itm">';
        echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["name"].'&return_url='.$current_url.'">&times;</a></span>';
        echo '<h3>'.$cart_itm["name"].'</h3>';
        echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        echo '<div class="p-qty">Available In : '.$cart_itm["packsize"].'</div>';
        echo '<div class="p-price">MRP :'.$cart_itm["price"].'</div>';
        echo '<div class="p-price">Our Price :'.$cart_itm["our_price"].'</div>';
        echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);

        $subtotal1 = ($cart_itm["our_price"]*$cart_itm["qty"]);
        $total1 = ($total1 + $subtotal1);
        $num = $cart_itm["name"];
        //echo 'NUM : '.$cart_itm["num"];
    }
    echo '</ol>';
    echo '<span class="check-out-txt"><strong>Total : '.$total1.'</strong> <a href="view_cart.php">ORDER !</a></span>';
    echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url='.$current_url.'">Empty Cart</a></span>';
}else{
    echo 'Your Cart is empty';
}
?>





    <h3 class="m_3"><?php echo $singlename; ?></h3>

                         <div class="btn_form">
                            <form method="post" action="cart_update.php">
                            <table border="0" >
                            <input type="hidden" name="brand_name" id="brand_name">
                            <tr><td>
                            <label for="size">Available in : </label>
                            <select name="size" id="size" onChange="getBrand_amount(this.value)" >
                            <option value="0" selected="">...SELECT...</option>
                            <?php include("config.php");
                    $query = "SELECT `brand_amount_id`, `available_pack`, `mrp_amount`, `our_price`, `product_id` FROM `brand_amount`  WHERE product_id='$singleid'";
                    $result = mysql_query($query);


        while($rows = mysql_fetch_array($result))
        {
        $selected = "";
        $brand_amount_id = $rows['brand_amount_id'];
        $categoryName = $rows['available_pack'];
        if($catid == $brand_amount_id)
        {
        $selected = "selected";
        }

                            ?>

                            <option value="<?php echo $rows['available_pack']; ?>" <?php echo $selected; ?>><?php echo $rows['available_pack']; ?></option>
                            <?php } ?>

                            </select>
                            <input type="hidden" name="brandid" id="brandid" value="<?php echo $singleid; ?> "/>
                            </td></tr>
                            <tr><td>
                            <label for="quantity">Quantity : </label>
                            <input type="text" id="quantity" name="quantity" value="">
                            </td></tr>
                            <?php //} ?>
                            </table>
                        <div id="available">     

                         </div>
                         <button value="Add" name="addcart" title="" class="add_to_cart">ADD</button>
                         <input type="hidden" name="pack" value="<?php echo $categoryName; ?>" />
                         <input type="hidden" name="product_name" value="<?php echo $singlename; ?>" />
                         <input type="hidden" name="brand_id" value="<?php echo $brandid; ?>" />
                         <input type="hidden" name="type" value="add" />
                         <input type="hidden" name="return_url" value="<?php echo $current_url; ?>" />
                            </form>
                            </div>

available_pack.php

<?php $Brand_amountId = $_GET['Brand_amountId'];
$id = intval($_GET['id']);
include("config.php");
$sql5 ="SELECT `brand_amount_id`, `available_pack`, `mrp_amount`, `our_price`, `product_id` FROM `brand_amount` WHERE `available_pack`='$Brand_amountId' AND product_id='$id'";
$result = mysql_query($sql5);

?>

<?php while($row5 = mysql_fetch_array($result)) { ?>

<p class="m_5">Rs. <?php echo $row5['our_price']; ?> <span class="reducedfrom"> Rs. <?php echo $row5['mrp_amount']; ?></span></p>

<?php } ?>

cart_update.php

<?php
session_start();
include_once("config1.php");

//empty cart by distroying current session
if(isset($_GET["emptycart"]) && $_GET["emptycart"]==1)
{
    $return_url = base64_decode($_GET["return_url"]); //return url
    session_destroy();
    header('Location:'.$return_url);
}

//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
    $product_name   = filter_var($_POST["product_name"], FILTER_SANITIZE_STRING); //product code
    $brand_id       = filter_var($_POST["brand_id"], FILTER_SANITIZE_STRING);
    $available_pack = filter_var($_POST["size"], FILTER_SANITIZE_STRING);
    $product_qty    = filter_var($_POST["quantity"], FILTER_SANITIZE_NUMBER_INT); //product code
    $return_url     = base64_decode($_POST["return_url"]); //return url

    //limit quantity for single product
    if($product_qty > 10){
        die('<div align="center">This demo does not allowed more than 10 quantity!<br /><a href="http://sanwebe.com/assets/paypal-shopping-cart-integration/">Back To Products</a>.</div>');
    }

    //MySqli query - get details of item from db using product code
    $results = $mysqli->query("SELECT p.`product_id`, p.`brand_name`, p.`brand_path`, p.`sub2_category_id`, p.`sub_category_id`, p.`main_category_id`, p.`date`, ba.`brand_amount_id`, ba.`available_pack`, ba.`mrp_amount`, ba.`our_price`, ba.`product_id` FROM `product` p JOIN `brand_amount` ba ON ba.product_id=p.product_id WHERE ba.`brand_amount_id`=$brand_id LIMIT 5");
    $obj = $results->fetch_object();
    //$num = $results->num_rows();

    if ($results) { //we have the product info 

        /*if($result = $mysqli->query("SELECT p.`product_id`, p.`brand_name`, p.`brand_path`, p.`sub2_category_id`, p.`sub_category_id`, p.`main_category_id`, p.`date`, ba.`brand_amount_id`, ba.`available_pack`, ba.`mrp_amount`, ba.`our_price`, ba.`product_id` FROM `product` p JOIN `brand_amount` ba ON ba.product_id=p.product_id WHERE p.brand_name='$product_name'"))
        {
        $row_cnt = $mysqli->num_rows($result);
        }*/
        //prepare array for the session variable
        $new_product = array(array('name'=>$obj->brand_name, 'qty'=>$product_qty, 'packsize'=>$available_pack, 'price'=>$obj->mrp_amount, 'our_price'=>$obj->our_price));

        if(isset($_SESSION["product"])) //if we have the session
        {
            $found = false; //set found item to false

            foreach ($_SESSION["product"] as $cart_itm) //loop through session array
            {
                if($cart_itm["name"] == $product_name){ //the item exist in array

                    $product[] = array('name'=>$cart_itm["name"], 'qty'=>$product_qty, 'packsize'=>$available_pack, 'price'=>$cart_itm["price"], 'our_price'=>$cart_itm['our_price']);
                    $found = true;
                }else{
                    //item doesn't exist in the list, just retrive old info and prepare array for session var
                    $product[] = array('name'=>$cart_itm["name"], 'qty'=>$cart_itm["qty"], 'packsize'=>$available_pack, 'price'=>$cart_itm["price"], 'our_price'=>$cart_itm["our_price"]);
                }
            }

            if($found == false) //we didn't find item in array
            {
                //add new user item in array
                $_SESSION["product"] = array_merge($product, $new_product);
            }else{
                //found user item in array list, and increased the quantity
                $_SESSION["product"] = $product;
            }

        }else{
            //create a new session var if does not exist
            $_SESSION["product"] = $new_product;
        }

    }

    //redirect back to original page
    header('Location:'.$return_url);
}

//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["product"]))
{
    $product_name   = $_GET["removep"]; //get the product code to remove
    $return_url     = base64_decode($_GET["return_url"]); //get return url


    foreach ($_SESSION["product"] as $cart_itm) //loop through session array var
    {
        if($cart_itm["code"]!=$product_name){ //item does,t exist in the list
            $product[] = array('name'=>$cart_itm["name"], 'qty'=>$cart_itm["qty"], 'packsize'=>$available_pack, 'price'=>$cart_itm["price"], 'our_price'=>$cart_itm["our_price"]);
        }

        //create a new product list for cart
        $_SESSION["product"] = $product;
    }

    //redirect back to original page
    header('Location:'.$return_url);
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.