tayhaithian Posted January 20, 2009 Share Posted January 20, 2009 i need some help on 'adding cart'... it seems to be have some problem with session that it wont add new value if the SESSION exist and it just acumulate +1 rather than array ++; . Thx for advance help. <html> <body> <?php include('conn.php'); if(isset($_POST['Submitted'])) { session_start(); $food_id = (int) $_GET['food_id']; $price = $_POST['price']; $quant = $_POST['quant']; if(isset($_SESSION['cart'][$food_id])) { $_SESSION['cart'][$food_id]['quantity']++; print_r($_SESSION['cart'][$food_id]); echo '<p>Another copy of the print has added </p>'; }else{ require_once('conn.php'); $_SESSION['cart'][$food_id]= array('quantity' => $quant, 'price' => $price); echo '<p>The print has been added to your shopping cart.</p>'; print_r($_SESSION['cart'][$food_id]); } 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>'; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/141544-php-mysql/ Share on other sites More sharing options...
Philip Posted January 20, 2009 Share Posted January 20, 2009 Are you setting these ($_SESSION['cart'][$food_id]['quantity']) to 0 when initializing them? Link to comment https://forums.phpfreaks.com/topic/141544-php-mysql/#findComment-740895 Share on other sites More sharing options...
tayhaithian Posted January 20, 2009 Author Share Posted January 20, 2009 if(isset($_SESSION['cart'][$food_id])) <<==== if 'exist' then $_SESSION['cart'][$food_id]['quantity']++; <<=== increment array it wasn't initializing to '0' but change the quantity number . Link to comment https://forums.phpfreaks.com/topic/141544-php-mysql/#findComment-740901 Share on other sites More sharing options...
Philip Posted January 20, 2009 Share Posted January 20, 2009 Can you echo out $_SESSION['cart'][$food_id]['quantity'] for me? I'm thinking you're trying to increment nothing. $_SESSION['cart'][$food_id] might exist, but whats the value for ['quantity'] Link to comment https://forums.phpfreaks.com/topic/141544-php-mysql/#findComment-740902 Share on other sites More sharing options...
tayhaithian Posted January 20, 2009 Author Share Posted January 20, 2009 hold on i think i get where the wrong adi , but dunno how to exchange the quantity instead on just adding another new line of array++ . any idea to modify the quantity instead of array++ ? $_SESSION['cart'][$food_id] Array ( [quantity] => 47 [price] => 19.88 ) if $_SESSION['cart'][$food_id]['quantity'] it just apear quantity number 47 Link to comment https://forums.phpfreaks.com/topic/141544-php-mysql/#findComment-740907 Share on other sites More sharing options...
sasa Posted January 20, 2009 Share Posted January 20, 2009 you can't srart session after any output Link to comment https://forums.phpfreaks.com/topic/141544-php-mysql/#findComment-740921 Share on other sites More sharing options...
tayhaithian Posted January 20, 2009 Author Share Posted January 20, 2009 you can't srart session after any output sorry can i know which line u are mentioning ? Link to comment https://forums.phpfreaks.com/topic/141544-php-mysql/#findComment-740934 Share on other sites More sharing options...
Philip Posted January 20, 2009 Share Posted January 20, 2009 <html> <body> <?php include('conn.php'); if(isset($_POST['Submitted'])) { session_start(); change to <?php session_start(); ?> <html> <body> <?php include('conn.php'); if(isset($_POST['Submitted'])) { I dunno how I missed that Link to comment https://forums.phpfreaks.com/topic/141544-php-mysql/#findComment-740936 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.