Jump to content

Need help - getting the used price


divinedesigns1

Recommended Posts

hey so i been working on this website and as always have to make changes to it, so now im stuck on trying to get the used price of an items if the person picked to buy the used product. but i keep on ending up with the original price all the time

 

can anyone give me some tips or hint on going about doing this?

if(isset($_POST['promit'])){
        include "php/sql_cn2.php";
        $pid = $_POST['pid'];
        $sql = mysqli_query($con, "SELECT usedprice, price FROM thoseproduct WHERE id='$pid'") or die('Error: ' . mysqli_error($con));
        if($sql){
            while($row = mysqli_fetch_assoc($sql)){
                $uprice = $row['usedPrice'];
                $price = $row['price'];
            }
        }
        $wasFound = false;
        $i = 0;
        // if the cart session is empty or not set
        if(!isset($_SESSION['cart_array']) || count($_SESSION['cart_array']) < 1){
            // if the cart is empty
            $_SESSION['cart_array'] = array(0 => array("item_id" => $pid, "quantity" => 1, "price" => $price, "uprice" => $uprice));
            header("location: confirm.php");
        }else{
            // run cart if have atleast one item
            foreach($_SESSION['cart_array'] as $each_item){
                $i++;
                while(list($key, $value) = each($each_item)){
                    if($key == "item_id" && $value == $pid){
                        // that item is in the cart already adject quantity
                        array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item["quantity"] + 1, "price" => $price, "uprice" => $uprice)));
                        $wasFound = true;
                        header("location: confirm.php");
                    }
                }
            }
            // if no item is in the cart
            if($wasFound == false){
                array_push($_SESSION['cart_array'], array("item_id" => $pid, "quantity" => 1));
                header("location: confirm.php");
            }
        }
    }
Edited by DDs1
Link to comment
Share on other sites

  • 2 weeks later...

We can't answer questions like that without seeing your code. You're 1100 posts in, you should know that by now.

sorry about that, at the time i didnt had the code with me so i just posted the question instead, the code is there now,

Link to comment
Share on other sites

No code= no answer, but I would guess that the PHP has an error. :facepalm:

nope there's no error in the code, it works perfectly find, im just trying to add the used price to the code, which is an additional edit to the original code

Link to comment
Share on other sites

nope there's no error in the code, it works perfectly find, im just trying to add the used price to the code, which is an additional edit to the original code

If the code does not perform as it is expected there is an error in the code. Not all errors produce error messages. Only syntax errors produce messages. Logic errors simply produce unexpected results. The latter is much more difficult to trace and fix.

 

I don't see anywhere in your code where you check whether a new or used item is selected. You're just passing both prices into the session variable.

Link to comment
Share on other sites

i see that you have 4 elements in your cart as folow :

$_SESSION['cart_array'] = array(0 => array("item_id" => $pid, "quantity" => 1, "price" => $price, "uprice" => $uprice));

but later on the code you are only pushing 2 items in the array :

array_push($_SESSION['cart_array'], array("item_id" => $pid, "quantity" => 1));

hope that was it , Good luck

Edited by john_jack
Link to comment
Share on other sites

If the code does not perform as it is expected there is an error in the code. Not all errors produce error messages. Only syntax errors produce messages. Logic errors simply produce unexpected results. The latter is much more difficult to trace and fix.

 

I don't see anywhere in your code where you check whether a new or used item is selected. You're just passing both prices into the session variable.

thanks

 

i see that you have 4 elements in your cart as folow :

$_SESSION['cart_array'] = array(0 => array("item_id" => $pid, "quantity" => 1, "price" => $price, "uprice" => $uprice));

but later on the code you are only pushing 2 items in the array :

array_push($_SESSION['cart_array'], array("item_id" => $pid, "quantity" => 1));

hope that was it , Good luck

alrite ill try that and get back at you, i appreciate the help on this one

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...