php - how to make display cart shows different size yet maintaining other size already added -
the coding i'm working on right correct: add column when choose different size , submit form replace previous output new one.
it suppose this
cart ___________________________________________ item/size |qty | amount(price) | orange polo size: xs | 1 | 43.00 | orange polo size: l | 1 | 43.00 | ____________________________________________
but instead becomes
cart ___________________________________________ item/size |qty | amount(price) | orange polo size: l | 2 | 86.00 | orange polo size: l | 2 | 86.00 | ____________________________________________
how make add new size maintaining previous size that's added?
<?php session_start(); $products = $_post["item_name"]."size:".$_post["size"]; $key= $_post["size"]; $amounts = $_post["amount"]; if ( !isset($_session["total"]) ) { $_session["total"] = 0; ($i=0; $i< count($products); $i++) { $_session["qty"][$i] = 0; $_session["amounts"][$i] = 0; } } //--------------------------- //add if ( isset($_post["submit1"]) ) { $i = $_post["submit1"]; $qty = $_session["qty"][$i] + 1; $_session["amounts"][$i] = $amounts * $qty; $_session["cart"][$key] = $i; $_session["qty"][$i] = $qty; $_session["products"] = $products; } //view cart <br/><br/> <h2>cart</h2> <table> <tr> <th width="300px">product</th> <th width="10px"> </th> <th>qty</th> <th width="10px"> </th> <th>amount</th><br> <th width="10px"> </th> <th>action</th> </tr> <?php $total = 0; foreach ( $_session["cart"] $i) { ?> <tr> <td><?php echo ($_session["products"]);?></td> <td width="10px"> </td> <td><?php echo( $_session["qty"][$i] ); ?></td> <td width="100px"> </td> <td><?php echo( $_session["amounts"][$i] ); ?></td> <td width="10px"> </td> <td><a href="?delete=<?php echo($i); ?>">delete cart</a></td> </tr> <?php $total = $total + $_session["amounts"][$i]; } $_session["total"] = $total; ?> <tr> <td colspan="7">total : <?php echo($total); ?></td> <tr> <td colspan="5"></td> </tr> <tr> <td colspan="5"><a href="?reset=true">reset cart</a></td> </tr> </tr> </table>
the form :
<form action="viewcart.php" method="post"> <div style="background:#eeeeee; padding:10px; border-style:double; font-size:24px;"> size <input type="hidden" value="size"/> <select name="size" style="width:97%"> <option value="xs">xs</option> <option value="s">s</option> <option value="m">m</option> <option value="l">l</option> <option value="xl">xl</option> </select> </div> <!-- item name --> <input type="hidden" name="item_name" value="polo orange"/> <!-- item price --> <input type="hidden" name="amount" value="43"/> <button class="button button5" type="submit" name="submit1" border="0" style="margin-top:20px;">add chart</button>
you forgot give button
value inside form
. in php-script take value
, use index ($i = $_post["submit1"];
). crash because overwrite erverything.
try add value this
<button class="button button5" type="submit" name="submit1" border="0" style="margin-top:20px;" value="<?php echo count($_session["qty"]) ?>">add chart</button>
or generate new index inside php-script this
//--------------------------- //add if ( isset($_post["submit1"]) ) { $i = count($_session["qty"]); $qty = $_session["qty"][$i] + 1; $_session["amounts"][$i] = $amounts * $qty; $_session["cart"][$key] = $i; $_session["qty"][$i] = $qty; $_session["products"] = $products; }
Comments
Post a Comment