tauchai83 Posted January 11, 2007 Share Posted January 11, 2007 I have been integrate the shopping cart written by someone with following code.function showCart() { $link = mysql_connect('localhost', 'root', ''); if (!$link) { die('Not connected : ' . mysql_error());}$db_selected = mysql_select_db('tcmdb', $link);if (!$db_selected) { die ('Can\'t use the Database: ' . mysql_error());} $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { //loop over items array and each loop, the value of the current element is assigned to item (use when unknown n. of elements. items is array name and item is array element. item is select by user. $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { //key and value $sql = 'SELECT * FROM Item WHERE Item_id = '.$id; $result = mysql_query($sql); $row = mysql_fetch_array($result, MYSQL_ASSOC); extract($row); //Import variables into the current symbol table from an array $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$Item_name.'</td>'; $output[] = '<td>RM'.$Unit_price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="6" maxlength="3" /></td>'; $output[] = '<td>RM'.($Unit_price * $qty).'</td>'; $total += $Unit_price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>RM'.$total.'</strong></p>'; $output[] = '<p align="center"><button type="submit">Update cart</button></p>'; $output[] = '[color=red]<input name="total" type="hidden" value="$total"></[/color]form><p><p><br>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output);} echo "<form></form>";how i can use this variable information to pass to next page to check out? i use input type hidden but does not work...is it input type hidden could no longer used to pass the info to next page in a FUNCTION? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted January 11, 2007 Share Posted January 11, 2007 it will be passed - just use $_POST['total'] in the script that processes the form.Your problem here is that you are trying to place variables in a single quoted string! change that line to:$output[] = '<input name="total" type="hidden" value="' . $total . '"></form><p><p> Quote Link to comment Share on other sites More sharing options...
tauchai83 Posted January 11, 2007 Author Share Posted January 11, 2007 thanks...it pass...hehe...thxs Quote Link to comment Share on other sites More sharing options...
tauchai83 Posted January 11, 2007 Author Share Posted January 11, 2007 [color=blue]function showCart() { $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { //key and value $sql = 'SELECT * FROM Item WHERE Item_id = '.$id; $result = mysql_query($sql); $row = mysql_fetch_array($result, MYSQL_ASSOC); extract($row); //Import variables into the current symbol table from an array $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove[/url]</td>'; $output[] = '<td>'.$Item_name.'</td>'; $output[] = '<td>RM'.$Unit_price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="6" maxlength="3" /></td>'; $output[] = '<td>RM'.($Unit_price * $qty).'</td>'; $total += $Unit_price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>RM'.$total.'</strong></p>'; $output[] = '<p align="center"><button type="submit">Update cart</button></p>'; $output[] = '<input name="total" type="hidden" value="$total"></form><p><p>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output);} echo "<form></form>";[/color]the $total pass well to another pages. How to pass the content of the cart to next page? the item selected by users....Information that I nit to pass:$total -done$qty of EACH item$price of EACH itemany1 can help? thanks! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted January 11, 2007 Share Posted January 11, 2007 if you have placed the cart in to a session then it is automatically passed. If the cart is an object then just put that into a session - objects are automagically serialized/unserialized when stored in sessions. Quote Link to comment Share on other sites More sharing options...
tauchai83 Posted January 13, 2007 Author Share Posted January 13, 2007 it is an object...it put item selected vy user in shopping cart in session before insert into DB...this "virtual" cart is used so tat no nit query DB many times.I got 1 problem to pass the value of the following:Each quantity of each item.Each price of each item.is it have to use foreach?? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.