Skip to content
Advertisement

Updating value in array updates all values with the last inserted value PHP

I have two arrays, one with quantities called aantal and one with products called producten.

Producten:

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => Tegel zwart
            [2] => Zwarte tegel 2x2m
            [3] => 47,5
            [4] => 25
        )

    [1] => Array
        (
            [0] => 4
            [1] => Tegel lichtgrijs
            [2] => Lichtgrijze tegel 2x2m
            [3] => 40,5
            [4] => 25
        )

    [2] => Array
        (
            [0] => 2
            [1] => Tegel zwart
            [2] => Zwarte tegel 2x2m
            [3] => 47,5
            [4] => 25
        )

    [3] => Array
        (
            [0] => 4
            [1] => Tegel lichtgrijs
            [2] => Lichtgrijze tegel 2x2m
            [3] => 40,5
            [4] => 25
        )

)

Aantal:

Array
(
    [0] => 20
    [1] => 20
    [2] => 27
    [3] => 25
)

I want to update each quantity according to the $_GET values from the url with the following code:

$sum = 0;
foreach($_SESSION['producten'] as $key => $product){
    //$_SESSION['producten'][$key][4] = '';
    $number = str_replace(',', '.', $product[3]);
    $sum+= $number;
    if(!empty($_GET['aantal'])){
        foreach($_GET['aantal'] as $keyaantal => $aantal){
            $_SESSION['producten'][$key][4] = $_GET['aantal'][$keyaantal];
        }
    }else{
        $_SESSION['producten'][$key][4] = '1';
    }
}

This is the form with the html:

<form action="cart.php">
<?php foreach ( $_SESSION['producten'] as $row){

    if(!empty($_GET['aantal'])){
        $aantalwaarde = $row[4];
    }else{
        $aantalwaarde = 1;
    }

    // if($row != ){

    // }
    ?>


        <tr>
            <td><?php echo $row[0] // ID; ?></td>
            <td><?php echo $row[1] // Product_naam; ?></td>
            <td><?php echo $row[2] // Beschrijving; ?></td> 
            <td><input name="aantal[]" type="number" value="<?PHP echo $aantalwaarde; ?>"></td> 
            <td>€<?php echo $row[3] // Prijs; ?></td>   

            <?php $total = $total + intval($row[3]); ?>
        </tr>

        <?php } ?>

        <tr>
            <td></td>
            <td></td>
            <td></td> 
            <td></td>   
        </tr>
        <tr>
            <td><input type="submit" value="Updaten"></td>
            <td></td>
            <td>Totaalprijs</td>
            <td></td> 
            <td>€<?php echo $sum // Prijs; ?></td>   

        </tr>

</form>

Why if I click the update button, it updates the last inserted values for all products? For example if I set the last quantity input at 25 and the one before it at 10, all products have a quantity for 25.

How can I fix that?

Advertisement

Answer

Your inner foreach loop is the problem. You’re going through all the quantities instead of picking the related one. So when the loop ends you’re always left with the last one being added to your session.

I can’t test right now but I think you should change

foreach($_GET['aantal'] as $keyaantal => $aantal){ 
   $_SESSION['producten'][$key][4] = $_GET['aantal'][$keyaantal]; 
} 

to

$_SESSION['producten'][$key][4] = $_GET['aantal'][$key]; 

That way, you select the matching key for both arrays.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement