0
Eai rapazeada!
 
I’m making a prototype order system.
 
My idea: I have a list of products and when user clicks on more or less of the item, I call a javascript function to change the quantity of the product and it calls, via ajax, a php routine that adjusts the amount of requested items in a session.
My problem is that I am unable to change the value of the second position of the two-dimensional array. I’ve asked apache to show me what the position value is and it shows, but if I need to add one or subtract one but it doesn’t change the original value.
 Follows code:
error_reporting(E_ALL);
session_start();
//unset($_SESSION['ItensPedido']);
//$codProd = $_GET['cod'];
//$conta = $_GET['conta'];
//$existe = 0;
$codProd = 20;
$conta = 1;
$existe = 0;
if(!isset($_SESSION['ItensPedido'])){
    $_SESSION['ItensPedido'] = array();
    array_push($_SESSION['ItensPedido'], array($codProd,1));
}else{
    foreach($_SESSION['ItensPedido'] as $item){
        if($item[0] == $codProd){
            if($conta == 1){
                $item[1] = $item[1] + 1;
                echo "<pre>", print_r($item, true),"</pre>";
            }else{
                if($item[1] != 0){
                    $item[1]--;
                }else{
                    unset($item);
                }
            }
        $existe++;
        }
    }
    if($existe == 0){
        array_push($_SESSION['ItensPedido'], array($codProd,1));
    }
}
echo "<pre>", print_r($_SESSION['ItensPedido'], true),"</pre>";
,, thanks! had not understood yet why of this in the foreach. It worked perfectly with the adjustments that told me to do.
– Matheus Delatorrre