php - How to update the data in an array with a specific array key? -


i creating cart using session , 2d array problem want change content of 1 of array in 2d array , have order id make sure changes correct array, here's code please me;

this code inserting new item:

if(isset($_post['addtocart'])){     $count= count($_session['cart']);     $newproduct= array(         'id' =>  $count,         'code' => $_session['code'],         'color' => $_post['color'],         'type' => $_post['type'],         'size' =>$_post['size'],         'quantity' => 1     );     $_session['cart'][]= $newproduct; } 

$_session['cart'] name of 2d array. here's sample list:

$_session['cart']=> array([0] =>(array order=> 0,   code=> 123,   color=> blue,   type=> us,   size=> m.   quantity => 1 ); ([1] =>(array order=> 1,   code=> 125,   color=> red,   type=> uk,   size=> s.   quantity => 1 );  

now want update quantity of array id of 125, supposed don't know id. in cart list have selectbox automatically submits when option selected. here's code;

    <select name="quantity1" id="quantity" onchange="javascript: submit()" > <option value="<?php echo $item['quantity']; ?>"><?php echo $item['quantity']; ?></option> <?php     $sql2 = "select * color_variation       color='$color'      , size='$size'      , size_type='$type'";     $q2= $conn->query($sql2);     $q2->setfetchmode (pdo::fetch_assoc);     while($r2 = $q2->fetch())     {         $x= $r2['quantity'];     }     for($y=1; $y<=$x; $y++){         if($y== $r1['quantity']){          } else{ ?> <option value="<?php echo $y; ?>"><?php echo $y; ?></option> <?php         }     } ?> </select>   

when submits heres code updating array specific array key:

if(isset($_post['quantity1'])){     $quantity = $_post['quantity1'];       $cart= $_session['cart'];     foreach($cart  &$value)     {         $value['quantity']= $quantity;     } } 


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -