tayhaithian Posted January 20, 2009 Share Posted January 20, 2009 haizz previous problem solve and now the other things .... problem may found after several try of retrieve data and actually the session cart array doesnt increment ++ to the other row how can i make it ??? $_SESSION['cart']['food_id']++; <<=== this doesnt sounds correct > <' <?php session_start(); ?> <html> <body> <?php include('conn.php'); if(isset($_POST['Submitted'])) { $food_id = (int) $_GET['food_id']; $price = $_POST['price']; $quant = $_POST['quant']; if((isset($_SESSION['cart']))&&$_SESSION['cart']['food_id']==$food_id && $_SESSION['cart']['price']==$price) { $myarray=array('food_id'=> $food_id,'quantity' => $quant, 'price' => $price); $_SESSION['cart']=$myarray; var_dump($_SESSION['cart']); echo '<p>Another copy of the print has added </p>'; }else{ if(isset($_SESSION['cart'])) { $_SESSION['cart']['food_id']++; require_once('conn.php'); $myarray=array('food_id'=> $food_id,'quantity' => $quant, 'price' => $price); $_SESSION['cart']=$myarray; echo '<p>The print has been added to your shopping cart.</p>'; //print_r($_SESSION['cart']['food_id']); //print_r($_SESSION['cart']['price']); foreach($_SESSION['cart']['food_id'] as $price) { var_dump($_SESSION['cart']); //echo $_SESSION['cart']['price']; echo "blabla<br />"; } }else{ require_once('conn.php'); $myarray=array('food_id'=> $food_id,'quantity' => $quant, 'price' => $price); $_SESSION['cart']=$myarray; echo '<p>The print has been added to your shopping cart.</p>'; //print_r($_SESSION['cart']['food_id']); //print_r($_SESSION['cart']['price']); //var_dump($_SESSION['cart']); foreach($_SESSION['cart']['food_id'] as $key => $price) { var_dump($_SESSION['cart']); echo "<br />"; } } } mysql_close($conn); } include('conn.php'); $query= "select * from foods"; $result= mysql_query($query); while( $row=mysql_fetch_array($result)) { echo '<form method="POST" action="test.php?food_id=',$row["food_id"],'">'; echo '<table border="0" cellspacing="5">'; echo '<tr><td>',$row["name"],'</td></tr>', '<tr><td rowspan="2"><img src="',$row["image"],'" width="200px" height="200px" /></td>', '<td>',$row["description"],'</td></tr>', '<tr><td><input type="radio" name="price" value="'; echo $row["price_reg"]; echo '" />','RM',$row["price_reg"],' ', '<input type="radio" name="price" value="'; echo $row["price_large"]; echo '" />','RM',$row["price_large"],'<br />', 'Quantity:','<input type="text" name="quant" size="1"/>','</td>'; echo '<td><input type="submit" name="ADD" value="ADD" /></td></tr>'; echo '<input type="hidden" name="Submitted" value="TRUE" />'; echo '<br />',$row["food_id"]; echo '</table>'; echo '</form>'; } ?> <a href="destroy.php">destroy</a> <br /> <a href="viewcart.php">view cart</a> </body> </html> Link to comment https://forums.phpfreaks.com/topic/141601-session-cart-problem-array-increment-failed/ Share on other sites More sharing options...
tayhaithian Posted January 20, 2009 Author Share Posted January 20, 2009 after some googling about session array~ i have changed the test session array output code and notice that the session array doesnt increment but straight away overwrite . what happen ? $_SESSION['cart']++; <<=== this sounds wasnt right because the 'cart' doesnt contain anything foreach($_SESSION['cart'] as $key => $value) { echo $key; echo $value; } Full code : <?php session_start(); ?> <html> <body> <?php include('conn.php'); if(isset($_POST['Submitted'])) { $food_id = (int) $_GET['food_id']; $price = $_POST['price']; $quant = $_POST['quant']; $cart = 0; if((isset($_SESSION['cart']))&&$_SESSION['cart']['food_id']==$food_id && $_SESSION['cart']['price']==$price) { $myarray=array('food_id'=> $food_id,'quantity' => $quant, 'price' => $price); $_SESSION['cart']=$myarray; var_dump($_SESSION['cart']); echo '<p>Another copy of the print has added </p>'; }else{ if(isset($_SESSION['cart'])) { $_SESSION['cart']++; require_once('conn.php'); $myarray=array('food_id'=> $food_id,'quantity' => $quant, 'price' => $price); $_SESSION['cart']=$myarray; echo '<p>The print has been added to your shopping cart.</p>'; //print_r($_SESSION['cart']['food_id']); //print_r($_SESSION['cart']['price']); //print_r($_Session); foreach($_SESSION['cart'] as $key => $value) { echo $_SESSION['cart']; echo $key; echo $value; echo "blabla<br />"; } }else{ require_once('conn.php'); $myarray=array('food_id'=> $food_id,'quantity' => $quant, 'price' => $price); $_SESSION['cart']=$myarray; echo '<p>The print has been added to your shopping cart.</p>'; //print_r($_SESSION['cart']['food_id']); //print_r($_SESSION['cart']['price']); //var_dump($_SESSION['cart']); foreach($_SESSION['cart'] as $key => $value) { echo $key; echo $value; echo "blabla<br />"; } } } mysql_close($conn); } include('conn.php'); $query= "select * from foods"; $result= mysql_query($query); while( $row=mysql_fetch_array($result)) { echo '<form method="POST" action="test.php?food_id=',$row["food_id"],'">'; echo '<table border="0" cellspacing="5">'; echo '<tr><td>',$row["name"],'</td></tr>', '<tr><td rowspan="2"><img src="',$row["image"],'" width="200px" height="200px" /></td>', '<td>',$row["description"],'</td></tr>', '<tr><td><input type="radio" name="price" value="'; echo $row["price_reg"]; echo '" />','RM',$row["price_reg"],' ', '<input type="radio" name="price" value="'; echo $row["price_large"]; echo '" />','RM',$row["price_large"],'<br />', 'Quantity:','<input type="text" name="quant" size="1"/>','</td>'; echo '<td><input type="submit" name="ADD" value="ADD" /></td></tr>'; echo '<input type="hidden" name="Submitted" value="TRUE" />'; echo '<br />',$row["food_id"]; echo '</table>'; echo '</form>'; } ?> <a href="destroy.php">destroy</a> <br /> <a href="viewcart.php">view cart</a> </body> </html> Link to comment https://forums.phpfreaks.com/topic/141601-session-cart-problem-array-increment-failed/#findComment-741213 Share on other sites More sharing options...
tayhaithian Posted January 20, 2009 Author Share Posted January 20, 2009 any idea ? Link to comment https://forums.phpfreaks.com/topic/141601-session-cart-problem-array-increment-failed/#findComment-741612 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.