Click here to Skip to main content
15,900,553 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey so what I want to do is to update my price depending on the quantity that the user puts in. and its doing that right now... except that it will update all the prices?
I don't know how to explain this without pictures but its kinda like this
object    price    quantity
dress----$100------1
pants----$20-------1
shoes----$50-------1
-----------------------
TOTAL    $170


but if I want to ,for example, have 2 pants it does this
object    price    quantity
dress----$100------1
pants----$20-------2
shoes----$50-------1
-----------------------
TOTAL    $340 


and I obviously don't want that
How can I fix this so that it will only calculate the field the user decides to change?
<?php
$broodjes = $_GET['broodjes_ID'];

if (isset($_SESSION['basket'])){
    if( in_array( $broodjes ,$_SESSION['basket']) )
    {

    }else{
        $_SESSION['basket'][] = $broodjes;

    }
}else{
    $_SESSION['basket'][]= $broodjes;

}

$sum = 0;
$sumtotal = 0;

         foreach($_SESSION['basket'] as $key => $value){

             //echo "Key = $key; value = $value; <br>";
             $sql = "SELECT broodjes_ID, broodnaam, prijs, voorraad FROM broodjes WHERE broodjes_ID=$value";
             $stmt = $conn->prepare($sql);
             $stmt->execute();
             $result = $stmt->get_result();

             while($row = mysqli_fetch_assoc($result)){


                echo '<div class="cart-items">';
                echo '<div class="cart-row">';
                    echo '<div class="cart-item cart-column">';
                        echo $row['broodnaam'];
                    echo '</div>';
                    echo '<div class="cart-item cart-column">';
                        echo '€ ' . $row['prijs'];
                    echo '</div>';
                    //quantity
                    echo '<div class="cart-item cart-column">';
                      echo '<form method="POST" action"">';
                         echo '<div class="col-xs-4">';
                         echo '<input type="number" name="quantity" id="quantity" class="form-control input-sm" placeholder="1" min="1">';
                         echo '</div>';
                      echo '</form>';
                     echo '</div>';
                     if (!empty($_POST["quantity"])){
                        if(isset($_POST["quantity"]))
                        {
                            echo '<div class="cart-item cart-column">';
                            $test = $_POST['quantity'];
                            $rowtotaal = $row['prijs'] * $test;
                            $sumtotal += $rowtotaal;
                            echo $rowtotaal;
                            echo '</div>';
                        }else{
                            //er was ene goede rede waarom deze hier was maar ben het even vergeten
                            echo '<div class="cart-item cart-column">';
                            $test = 1;
                            $rowtotaal = $row['prijs'] * $test;
                            $sumtotal += $rowtotaal;
                            echo $rowtotaal;
                            echo '</div>';
                        }
                    }else{
                        echo '<div class="cart-item cart-column">';
                        $test = 1;
                        $rowtotaal = $row['prijs'] * $test;
                        $sumtotal += $rowtotaal;
                        echo $rowtotaal;
                        echo '</div>';
                    }
                echo '</div>';
                echo '</div>';
                //$sum = $row['prijs'] *
                //$sum += $row['prijs'];
                 if(isset($_POST['quantity'][0])){
                     $quantity = $_POST['quantity'];
                     $sum += $row['prijs'] * $quantity;
                 }else{
                     $sum += $row['prijs'];
                }


             }


         }
         ?> <br />

        <div class="cart-total">
            class="cart-total-title">Total
             € <?php   echo $sumtotal;?>
        </div>
        <br/>


        <button type="button" class="btn btn-danger"><a href="emptyarray.php"> Bestellen</a></button>


 <br>


What I have tried:

I tried to use the $_POST['quantity'][0] thing to get it to work but that doesn't help. then I tried to explain myself to google but idk how so I just decides to ask here.
Posted
Updated 18-Jan-22 23:09pm

1 solution

Each <form> is posting a single quantity. But there is nothing in that form to indicate which row it belongs to, so you end up applying the new quantity to every row.

You need to add an <input type="hidden"> to the form with the value of a field which identifies the row from your cart. For example:
PHP
echo '<form method="POST" action"">';
    echo '<input type="hidden" name="broodnaam" value="' . $row['broodnaam'] . '">';
    echo '<div class="col-xs-4">';
        echo '<input type="number" name="quantity" id="quantity" class="form-control input-sm" placeholder="1" min="1" value="1">';
    echo '</div>';
echo '</form>';

You then need to test that value to see if the new quantity applies to the current row.
PHP
$quantity = 1;
if (isset($_POST['quantity']) && !empty($_POST['quantity'])){
    if (isset($_POST['broodnaam']) && !empty($_POST['broodnaam'])){
        if ($_POST['broodnaam'] == $row['broodnaam']){
            $quantity = $_POST['quantity'];
        }
    }
}

echo '<div class="cart-item cart-column">';
$rowtotaal = $row['prijs'] * $quantity;
$sumtotal += $rowtotaal;
echo $rowtotaal;
echo '</div>';

NB: You don't appear to be storing the quantity anywhere on the cart. If the user updates the quantity of "pants" to 2, and then updates the quantity of "shoes" to 2, the quantity of "pants" will be reset to 1.
 
Share this answer
 
Comments
Rebecca2002 19-Jan-22 5:31am    
you said "NB: You don't appear to be storing the quantity anywhere on the cart. If the user updates the quantity of "pants" to 2, and then updates the quantity of "shoes" to 2, the quantity of "pants" will be reset to 1." but am I not storing that in $_POST['quantity']?
Richard Deeming 19-Jan-22 5:34am    
The $_POST collection only exists for the duration of one request. When you submit a different form, or follow a link to make a GET request to another page, the previous $_POST variables will no longer exist.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900