Kevinh Posted June 2, 2015 Share Posted June 2, 2015 Hi I am doing a shopping cart and I would like to stote the product id and product name for teh shopping cart Here is my code so far. The reason for this is that I want to append text in certain instances to the product name Any help in pointing me in the right direction will be appreciated add_to_cart.php view_cart.php Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/ Share on other sites More sharing options...
QuickOldCar Posted June 2, 2015 Share Posted June 2, 2015 Posted code so others do not have to download. add_to_cart.php <?php session_start(); include 'includes/conn_db.php'; /*$jobid_out = $_GET['jobid'];*/ $med_year = date('Y'); $byear = $med_year - 60; $ryear = $med_year + 1; $year = range($byear,$med_year); $yearr = range($ryear,$med_year); $query = "SELECT * from config"; $result = mysql_query($query, $conn); while ($row = mysql_fetch_assoc($result)){ $address1_out = $row["address1"]; $address2_out = $row["address2"]; $suburb_out = $row["suburb"]; $city_out = $row["city"]; $tel_out = $row["tel"]; $fax_out = $row["fax"]; /*$cell_out = $row["cell"];*/ $mail_out = $row["email"]; $code_out = $row["postal_code"]; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="keywords" content="Riccados, restaurant, diner, chicken, fish, sushi, johannesburg, braamfontein, gauteng, south africa, breakfast, sandwiches, omelettes, soups, burgers, steak, grill, lamb, seafood, poultry, salads, pizza, pasta, sushi, desserts, bakery, drinks, cocktails, shooters, draughts, events, birthdays, celebrations, its your vibe" /><meta name="description" content="Riccados, situated in Braamfontein bringing the ambience and vibe for a perfect end to the day, Riccados promises to be a place for all people to feel welcome." /> <meta name="robots" content="index, follow" /> <title>Riccados Restaurant - It's your vibe</title> <link rel="stylesheet" href="css/font1/droidserif.css" /> <link href="css/riccados.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/thickbox.js"></script> <script type="text/javascript"> /*** Simple jQuery Slideshow Script Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc. Please link out to me if you like it ***/ function slideSwitch() { var $active = $('#slideshow IMG.active'); if ( $active.length == 0 ) $active = $('#slideshow IMG:last'); // use this to pull the images in the order they appear in the markup var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first'); // uncomment the 3 lines below to pull the images in random order // var $sibs = $active.siblings(); // var rndNum = Math.floor(Math.random() * $sibs.length ); // var $next = $( $sibs[ rndNum ] ); $active.addClass('last-active'); $next.css({opacity: 0.0}) .addClass('active') .animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active'); }); } $(function() { setInterval( "slideSwitch()", 5000 ); }); </script> </head> <body> <div id="main_cnt"> <table style="width:1000px"><tr><td> <div id="left_cnt"> <div id="slideshow"> <img src="images/contact1.jpg" alt="" class="active" /> <img src="images/contact2.jpg" alt="" /> <img src="images/contact3.jpg" alt="" /> <img src="images/contact4.jpg" alt="" /> </div></div> <table width="1000"><tr><td width="300"> <div id="right_cnt"><img src="images/logo.png" id="logo" alt="Riccados" /><br /> <div id="nav_ctr"> <div id='cssmenu'> <ul> <li class='active '><a href='index.php'><span>HOME</span></a></li> <li><a href=''><span>MENU</span></a> <ul> <li><a href='menu.php?menuid=1'><span>BREAKFASTS</span></a></li> <li><a href='menu.php?menuid=2'><span>OMELETTES</span></a></li> <li><a href='menu.php?menuid=3'><span>SANDWICHES</span></a></li> <li><a href='menu.php?menuid=4'><span>STARTERS</span></a></li> <li><a href='menu.php?menuid=5'><span>SALADS</span></a></li> <li><a href='menu.php?menuid=6'><span>PASTAS</span></a></li> <li><a href='menu.php?menuid=7'><span>BEEF MAINS</span></a></li> <li><a href='menu.php?menuid=8'><span>PREGO'S & BURGERS</span></a></li> <li><a href='menu.php?menuid=9'><span>LAMB & RIB MAINS</span></a></li> <li><a href='menu.php?menuid=10'><span>CHICKEN & SEAFOOD MAINS</span></a></li> <li><a href='menu.php?menuid=11'><span>COMBOS</span></a></li> <li><a href='menu.php?menuid=12'><span>PIZZAS</span></a></li> <li><a href='menu.php?menuid=13'><span>SUSHI</span></a></li> <li><a href='menu.php?menuid=14'><span>DESSERTS</span></a></li> <li><a href='menu.php?menuid=15'><span>KIDDIES</span></a></li> <li><a href='menu.php?menuid=16'><span>DRINKS</span></a></li> </ul> <li><a href="bookings.php"><span>BOOKINGS</span></a></li> <li><a href='events.php'><span>EVENTS</span></a> </li> <li><a href='gallery.php'><span>GALLERY</span></a> </li> <li><a href='about.php'><span>ABOUT RICCADOS</span></a></li> <li><a href='contact.php'><span>CONTACT RICCADOS</span></a></li></ul> </div> </div> </div> <br /><br /> <br /><br /> <div id="left_cnt"> <table width="300"><tr><td valign="top"> <div id="contact_body"> <a href="https://www.facebook.com/pages/Riccados/1418413421809723?ref=bookmarks" target="_blank"><img src="images/facebook.png" border="0" alt="Find us on Facebook" /></a> <a href="https://twitter.com/RicardosRestuar" class="twitter-follow-button" data-show-count="false" target="_blank"><img src="images/twitter.png" border="0" alt="Find us on Twitter" /></a> <br /> <a href="" target="_blank"><img src="images/linkedin.png" border="0" alt="Find us on LinkedIn" /></a> <a href="" target="_blank"><img src="images/instagram.png" border="0" alt="Find us on Instagram" /></a><br /><br /> <?php print $address1_out ?><br /> <?php if (!empty($address2_out)) { print $address2_out;print "<br>"; } ?> <?php print $suburb_out ?><br /> <?php print $city_out ?><br /> <?php print $code_out ?><br /> Tel: <?php print $tel_out ?><br /> <!--- Fax: --> <?php /*print $fax_out */?><br /> Email: <a href="mailto:<?php print $mail_out ?>" style="color:white"><?php print $mail_out ?></a> </div> <?php $curr_date = date('Y-m-d'); $query4 = "SELECT * from riccados_events WHERE event_sort_date >= '$curr_date'"; // display the products in each subcat $result4 = mysql_query($query4, $conn); $num_rows = mysql_num_rows($result4); if ($num_rows > 0) { echo '<div id="events_heading"></div><div id="events_text">'; } $query4 = "SELECT * from riccados_events WHERE event_sort_date >= '$curr_date' ORDER BY event_sort_date ASC LIMIT 8"; // display the products in each subcat $result4 = mysql_query($query4, $conn); while ($row4 = mysql_fetch_assoc($result4)){ $description_out = $row4["event_name"]; $date_out = $row4["event_date"]; $event_desc_out = $row4["event_desc"]; echo '<p></p><strong>' . $description_out . '</strong><br />' . $date_out . '<p></p>' . $event_desc_out . '<br /><a href="events.php" style="float: right;margin-right: 10px; color: white;text-decoration: none;font-style:italic">Continue Reading</a><br />'; } if ($num_rows > 0) { echo '<br /><br />'; } ?> </div> </div> </td> <td width="700" style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px"><br /> <div id="right_cnt"> <div id="contact_heading_main"></div> <div id="contact_us_cnt"><div id="contact_main_cnt_two_text"><p></p> <?php $qty_out = 1; $qty1 = $qty_out; // This page adds products to the shopping cart if (isset($_POST['Bread'])) { $_SESSION['bread'] = $_POST['Bread']; } if (is_numeric ($_SESSION['prodid'])) { // check for a print id $pid = $_SESSION['prodid']; // check to see if the cart already contains one of these products if (isset ($_session['cart'][$pid])) { $qty_out = $_SESSION['cart'][$pid] + $qty1; } else { $qty = $qty1; } // add to the cart session variable $_SESSION['cart'][$pid] = $qty; // Display a message echo '<table summary="" align="center" border="0" bgcolor="white"><tr><td width="370">'; echo '<p><font face="arial" size="2" color="#666666">Your product has been added to your cart<br></font></td></tr><tr><td><form action="view_cart.php" method="post"><input type="submit" name="view_barrel" value="View Cart"></form></td></tr></table>'; } ?></div></div> </td></tr></table> <div id="footer"><div style="margin-left:50px;font-family:Arial, Helvetica, sans-serif;font-size:11px;color:white;margin-top:35px;display:block"><br />Copyright © 2015 Riccados </div><div style="float: right;margin-right:46px;;font-family:Arial, Helvetica, sans-serif;font-size:11px;color:white;display:inline-block;margin-top:-27px">Website designed and developed by <a href="http://www.it-people.co.za" target="_blank"><img src="images/itpeople.png" alt="IT People" style="margin-top:-10px;width:78px;height:35px" /></a></div> </div> </div> </body> </html> view_cart.php <?php session_start(); include 'includes/conn_db.php'; /*$jobid_out = $_GET['jobid'];*/ $med_year = date('Y'); $byear = $med_year - 60; $ryear = $med_year + 1; $year = range($byear,$med_year); $yearr = range($ryear,$med_year); $query = "SELECT * from config"; $result = mysql_query($query, $conn); while ($row = mysql_fetch_assoc($result)){ $address1_out = $row["address1"]; $address2_out = $row["address2"]; $suburb_out = $row["suburb"]; $city_out = $row["city"]; $tel_out = $row["tel"]; $fax_out = $row["fax"]; /*$cell_out = $row["cell"];*/ $mail_out = $row["email"]; $code_out = $row["postal_code"]; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="keywords" content="Riccados, restaurant, diner, chicken, fish, sushi, johannesburg, braamfontein, gauteng, south africa, breakfast, sandwiches, omelettes, soups, burgers, steak, grill, lamb, seafood, poultry, salads, pizza, pasta, sushi, desserts, bakery, drinks, cocktails, shooters, draughts, events, birthdays, celebrations, its your vibe" /><meta name="description" content="Riccados, situated in Braamfontein bringing the ambience and vibe for a perfect end to the day, Riccados promises to be a place for all people to feel welcome." /> <meta name="robots" content="index, follow" /> <title>Riccados Restaurant - It's your vibe</title> <link rel="stylesheet" href="css/font1/droidserif.css" /> <link href="css/riccados.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/thickbox.js"></script> <script type="text/javascript"> /*** Simple jQuery Slideshow Script Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc. Please link out to me if you like it ***/ function slideSwitch() { var $active = $('#slideshow IMG.active'); if ( $active.length == 0 ) $active = $('#slideshow IMG:last'); // use this to pull the images in the order they appear in the markup var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first'); // uncomment the 3 lines below to pull the images in random order // var $sibs = $active.siblings(); // var rndNum = Math.floor(Math.random() * $sibs.length ); // var $next = $( $sibs[ rndNum ] ); $active.addClass('last-active'); $next.css({opacity: 0.0}) .addClass('active') .animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active'); }); } $(function() { setInterval( "slideSwitch()", 5000 ); }); </script> </head> <body> <div id="main_cnt"> <table style="width:1000px"><tr><td> <div id="left_cnt"> <div id="slideshow"> <img src="images/contact1.jpg" alt="" class="active" /> <img src="images/contact2.jpg" alt="" /> <img src="images/contact3.jpg" alt="" /> <img src="images/contact4.jpg" alt="" /> </div></div> <table width="1000"><tr><td width="300"> <div id="right_cnt"><img src="images/logo.png" id="logo" alt="Riccados" /><br /> <div id="nav_ctr"> <div id='cssmenu'> <ul> <li class='active '><a href='index.php'><span>HOME</span></a></li> <li><a href=''><span>MENU</span></a> <ul> <li><a href='menu.php?menuid=1'><span>BREAKFASTS</span></a></li> <li><a href='menu.php?menuid=2'><span>OMELETTES</span></a></li> <li><a href='menu.php?menuid=3'><span>SANDWICHES</span></a></li> <li><a href='menu.php?menuid=4'><span>STARTERS</span></a></li> <li><a href='menu.php?menuid=5'><span>SALADS</span></a></li> <li><a href='menu.php?menuid=6'><span>PASTAS</span></a></li> <li><a href='menu.php?menuid=7'><span>BEEF MAINS</span></a></li> <li><a href='menu.php?menuid=8'><span>PREGO'S & BURGERS</span></a></li> <li><a href='menu.php?menuid=9'><span>LAMB & RIB MAINS</span></a></li> <li><a href='menu.php?menuid=10'><span>CHICKEN & SEAFOOD MAINS</span></a></li> <li><a href='menu.php?menuid=11'><span>COMBOS</span></a></li> <li><a href='menu.php?menuid=12'><span>PIZZAS</span></a></li> <li><a href='menu.php?menuid=13'><span>SUSHI</span></a></li> <li><a href='menu.php?menuid=14'><span>DESSERTS</span></a></li> <li><a href='menu.php?menuid=15'><span>KIDDIES</span></a></li> <li><a href='menu.php?menuid=16'><span>DRINKS</span></a></li> </ul> <li><a href="bookings.php"><span>BOOKINGS</span></a></li> <li><a href='events.php'><span>EVENTS</span></a> </li> <li><a href='gallery.php'><span>GALLERY</span></a> </li> <li><a href='about.php'><span>ABOUT RICCADOS</span></a></li> <li><a href='contact.php'><span>CONTACT RICCADOS</span></a></li></ul> </div> </div> </div> <br /><br /> <br /><br /> <div id="left_cnt"> <table width="300"><tr><td valign="top"> <div id="contact_body"> <a href="https://www.facebook.com/pages/Riccados/1418413421809723?ref=bookmarks" target="_blank"><img src="images/facebook.png" border="0" alt="Find us on Facebook" /></a> <a href="https://twitter.com/RicardosRestuar" class="twitter-follow-button" data-show-count="false" target="_blank"><img src="images/twitter.png" border="0" alt="Find us on Twitter" /></a> <br /> <a href="" target="_blank"><img src="images/linkedin.png" border="0" alt="Find us on LinkedIn" /></a> <a href="" target="_blank"><img src="images/instagram.png" border="0" alt="Find us on Instagram" /></a><br /><br /> <?php print $address1_out ?><br /> <?php if (!empty($address2_out)) { print $address2_out;print "<br>"; } ?> <?php print $suburb_out ?><br /> <?php print $city_out ?><br /> <?php print $code_out ?><br /> Tel: <?php print $tel_out ?><br /> <!--- Fax: --> <?php /*print $fax_out */?><br /> Email: <a href="mailto:<?php print $mail_out ?>" style="color:white"><?php print $mail_out ?></a> </div> <?php $curr_date = date('Y-m-d'); $query4 = "SELECT * from riccados_events WHERE event_sort_date >= '$curr_date'"; // display the products in each subcat $result4 = mysql_query($query4, $conn); $num_rows = mysql_num_rows($result4); if ($num_rows > 0) { echo '<div id="events_heading"></div><div id="events_text">'; } $query4 = "SELECT * from riccados_events WHERE event_sort_date >= '$curr_date' ORDER BY event_sort_date ASC LIMIT 8"; // display the products in each subcat $result4 = mysql_query($query4, $conn); while ($row4 = mysql_fetch_assoc($result4)){ $description_out = $row4["event_name"]; $date_out = $row4["event_date"]; $event_desc_out = $row4["event_desc"]; echo '<p></p><strong>' . $description_out . '</strong><br />' . $date_out . '<p></p>' . $event_desc_out . '<br /><a href="events.php" style="float: right;margin-right: 10px; color: white;text-decoration: none;font-style:italic">Continue Reading</a><br />'; } if ($num_rows > 0) { echo '<br /><br />'; } ?> </div> </div> </td> <td width="700" style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px;;<br /> <div id="right_cnt"> <div id="contact_heading_main"></div> <div id="contact_us_cnt"><div id="contact_main_cnt_two_text"><p></p> <?php if (isset($_POST['submit'])) { foreach ($_POST['qty'] as $key => $value) { if (($value == 0) AND (is_numeric($value))) { unset ($_SESSION['cart'][$key]); } elseif (is_numeric($value) AND ($value > 0)) { $_SESSION['cart'][$key] = $value; } } } // check if the shopping cart is empty $empty = TRUE; if (isset($_SESSION['cart'])) { foreach($_SESSION['cart'] as $key => $value) { if (isset($value)) { $empty = FALSE; } } } // Display the cart if it is not empty if (!$empty) { // Retrieve all of the information for the products in the cart $query = 'SELECT * FROM submenu_name WHERE submenu_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query = substr ($query, 0, -1) . ') ORDER BY submenu_name ASC'; $result = mysql_query($query); $final_item_name = $_SESSION['final_name']; // create a table and a form print <<<TOP <table summary="" width="580" align="center" style="; <tr> <td nowrap align="center">Your Shopping Cart</td></tr></table> <table summary="" align="center" width="580" border="1" style="; <tr> <td> <table border="0" width="580" cellpadding="3"> <tr> <td align="left" width="250"><font face="arial" size="2" color="#666666"><b>Item</b></font></td> <td align="center" width="70"><font face="arial" size="2" color="#666666"><b>Qty</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="#666666"><b>Unit Price</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="#666666"><b>Line Total</b></font></td> </tr> <form action="view_cart.php" method="post"> TOP; // Print each item $total = 0; // total cost of the order while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $list_price_out = $row['item_price']; $list_price_out = number_format($list_price_out, 2, '.', ''); // Calculate the total and subtotals $subtotal = $_SESSION['cart'][$row['submenu_id']] * $list_price_out; $subtotal = number_format($subtotal, 2, '.', ''); $total += $subtotal; $total = number_format($total, 2, '.', ''); // print the row print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="#666666">$final_item_name</font></td> <td align="center"><input type="text" size="3" name="qty[{$row['submenu_id']}]" value="{$_SESSION['cart'][$row['submenu_id']]}"> </td><td align="right"><font face="arial" size="2" color="#666666">R $list_price_out </font></td><td align="right"><font face="arial" size="2" color="#66666">R $subtotal</font></td> </tr> ROW; } // end of the WHILE loop // print the footer and close the table and the form //$finq = $_SESSION['cart'][$row['prod_id']]; print <<<FOOTER <tr> <td></td><td></td> <td align="right" valign="top"><font face="arial" size="2" color="#666666"><b>Sub Total:</b></font></td> <td align="right" valign="top"><font face="arial" size="2" color="#666666"><b> $total</b></font><br></td> </tr> FOOTER; print <<<FOOTER1 </table> <table width="500" align="center" border="1"> <tr><td align="center"><font face="arial" size="2" color="#666666">To change the quantities change the figure in the Qty box, and your Shopping Cart will automatically be updated after clicking on the <strong>Update Cart button</strong>. <br>If you change the quantity to zero, then that item will be deleted from your Shopping Cart.</font></td></tr> </table> <table width="580" align="center"> <tr><br><td width="85"></td><td width="150" valign="top" align="center"><input type="submit" name="submit" value="Update Cart"></form></td><td width="150" align="center"> </td><td width="130" align="center"><form action="checkout.php" method="post"><input type="submit" name="checkout" value="Checkout"></form> </td><td width="85"></td></tr></table> FOOTER1; } else { print <<<EMP <table border="0" width="580" align="center" style="; <tr> <td> <table summary="" width="300" border="1" style="border-color:#960232" bgcolor="#f8f3c2" align="center"> <tr> <td> <table border="0" width="300" cellpadding="3"> <tr><td align="center"><p><font face="arial" size="2" color="#5e5d5d">Your cart is empty!</font></td></tr></table></table> EMP; } ?> </div></div> </tr> </table></table> <div id="footer"><div style="margin-left:50px;font-family:Arial, Helvetica, sans-serif;font-size:11px;color:white;margin-top:35px;display:block"><br />Copyright © 2015 Riccados </div><div style="float: right;margin-right:46px;;font-family:Arial, Helvetica, sans-serif;font-size:11px;color:white;display:inline-block;margin-top:-27px">Website designed and developed by <a href="http://www.it-people.co.za" target="_blank"><img src="images/itpeople.png" alt="IT People" style="margin-top:-10px;width:78px;height:35px" /></a></div> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513038 Share on other sites More sharing options...
QuickOldCar Posted June 2, 2015 Share Posted June 2, 2015 Sessions are arrays, the users session will already have a session id assigned. It's session_id() Anything you add to the session as an associative or named array key is a potential array or a single key value if that's what you desire. I made a little demo can test out and experiment with. <?php //start session session_start(); //unset($_SESSION);//unsetting session for testing //ensure session is set with id if (session_id() != "") { //users session id $session_id = session_id(); //define session array if doesn't exist if (!$_SESSION[$session_id]['cart']) { $_SESSION[$session_id]['cart'] = array(); } //make dynamic values to add into array $product = "bread"; //product name $product_id = 3456; //product id $qty = 2; //quantity to add //add a different item /* $product = "poultry";//product name $product_id = 555;//product id $qty = 1;//quantity to add */ //add additional same items if ($_SESSION[$session_id]['cart'][$product_id]['quantity']) { $qty = $_SESSION[$session_id]['cart'][$product_id]['quantity'] + $qty; } $_SESSION[$session_id]['cart'][$product_id] = array( "product" => $product, "quantity" => $qty ); echo "<pre>"; print_r($_SESSION); echo "</pre>"; } else { echo "No session for cart"; } ?> My results: Array ( [8ba04fc430043d8902928f41b5a83b2e] => Array ( [cart] => Array ( [3456] => Array ( [product] => bread [quantity] => 2 ) [555] => Array ( [product] => poultry [quantity] => 3 ) ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513040 Share on other sites More sharing options...
mac_gyver Posted June 3, 2015 Share Posted June 3, 2015 some recommendations - 1) you should be using css instead of inline styling. this will simplify and clean up all your html makeup and get your page up to 21st century programming standards. if you are trying to develop programming skills, you must start with current coding standards, not something that is out of date. 2) Don't Repeat Yourself (DRY). both of your pages repeat about half of the code, because you are trying to produce a constant page layout (you even have an error between the pages, where you corrected something only on one page.) you can and should actually have just one page, but if you are not up to this point in your programming, you should make the common part of the page layout a php based template that you include on each page that needs it. the main content that's different between the pages would be produced and stored in a php variable, that you just echo at the appropriate place in the 'template' code. 3) your form processing code should come near the top of your file, before any html markup. your code should be laid out in this general order - initialization, post method form processing, get method processing, html page/template. the post method processing code, when it has successfully ran without any errors, should do a header() redirect back to the same url of the page. if you want to display a 'success' message, pass it in a session variable that will be tested and used in the html page/template code. the get method processing code is the php code that gets/produces the data that the html page/template needs to display the dynamic information on the page. 4) your add to cart code contains a typo in - $_session. this should be $_SESSION. your add to cart code should not be testing a variable named - $_POST['Bread'] as that doesn't tell anyone reading the code what the purpose of the variable is. you should have a variable that identifies what the submitted form was for. how about using - $_POST['add_to_cart'] ? all of your add to cart form processing code should be inside the conditional statement that tests if the correct form was submitted. see the following example code for what your add to cart from processing can/should be - // post method form processing code if($_SERVER['REQUEST_METHOD'] == 'POST') { // add to cart if (isset($_POST['add_to_cart'])) { $pid = (int)$_POST['prodid']; if(!isset($_SESSION['cart'][$pid])){ $_SESSION['cart'][$pid] = 0; // create an entry (it will be incremented in the following code) } $_SESSION['cart'][$pid]++; // increment whatever the existing quantity is $_SESSION['message'] = "Your product has been added to your cart"; // uses in the html page/template code } // other form processing code would go here... // after successfully (no errors) processing all post data, do a header() redirect to the exact same url that the form submitted to if(empty($errors)){ $host = $_SERVER['HTTP_HOST']; $uri = $_SERVER['REQUEST_URI']; header("Location: http://$host$uri"); die; } // if there were errors in any of the above form processing code, continue on this page, displaying any errors, redisplay form... } 5) if you are learning php now, you should be using either the mysqli or PDO database library of functions. the mysql functions are obsolete and will be removed from php soon. again, you should start with current coding standards, not something that is out of date. i would also recommend that your config settings be assigned to an array - $config, so that you can distinguish them from other variables. your navigation menus on the page should also be dynamically produced (from configuration settings), rather than hard coded html. Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513104 Share on other sites More sharing options...
Kevinh Posted June 4, 2015 Author Share Posted June 4, 2015 Thank you for your input, I have noted it and will come up to speed asap. I have followed the changes that QuickOldCar gave me, The Add_to_Cart generates the results that I desire, I am just battling a bit with teh view cart Here is the add to cart ession_start(); if (session_id() != "") { // Users session id $session_id = session_id(); } // define session array if it doesn't exist if (!$_SESSION[$session_id]['cart']) { $_SESSION[$session_id]['cart'] = array(); } include 'includes/conn_db.php'; /*$jobid_out = $_GET['jobid'];*/ $med_year = date('Y'); $byear = $med_year - 60; $ryear = $med_year + 1; $year = range($byear,$med_year); $yearr = range($ryear,$med_year); $query = "SELECT * from config"; $result = mysql_query($query, $conn); while ($row = mysql_fetch_assoc($result)){ $address1_out = $row["address1"]; $address2_out = $row["address2"]; $suburb_out = $row["suburb"]; $city_out = $row["city"]; $tel_out = $row["tel"]; $fax_out = $row["fax"]; /*$cell_out = $row["cell"];*/ $mail_out = $row["email"]; $code_out = $row["postal_code"]; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="keywords" content="Riccados, restaurant, diner, chicken, fish, sushi, johannesburg, braamfontein, gauteng, south africa, breakfast, sandwiches, omelettes, soups, burgers, steak, grill, lamb, seafood, poultry, salads, pizza, pasta, sushi, desserts, bakery, drinks, cocktails, shooters, draughts, events, birthdays, celebrations, its your vibe" /><meta name="description" content="Riccados, situated in Braamfontein bringing the ambience and vibe for a perfect end to the day, Riccados promises to be a place for all people to feel welcome." /> <meta name="robots" content="index, follow" /> <title>Riccados Restaurant - It's your vibe</title> <link rel="stylesheet" href="css/font1/droidserif.css" /> <link href="css/riccados.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/thickbox.js"></script> <script type="text/javascript"> /*** Simple jQuery Slideshow Script Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc. Please link out to me if you like it ***/ function slideSwitch() { var $active = $('#slideshow IMG.active'); if ( $active.length == 0 ) $active = $('#slideshow IMG:last'); // use this to pull the images in the order they appear in the markup var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first'); // uncomment the 3 lines below to pull the images in random order // var $sibs = $active.siblings(); // var rndNum = Math.floor(Math.random() * $sibs.length ); // var $next = $( $sibs[ rndNum ] ); $active.addClass('last-active'); $next.css({opacity: 0.0}) .addClass('active') .animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active'); }); } $(function() { setInterval( "slideSwitch()", 5000 ); }); </script> </head> <body> <div id="main_cnt"> <table style="width:1000px"><tr><td> <div id="left_cnt"> <div id="slideshow"> <img src="images/contact1.jpg" alt="" class="active" /> <img src="images/contact2.jpg" alt="" /> <img src="images/contact3.jpg" alt="" /> <img src="images/contact4.jpg" alt="" /> </div></div> <table width="1000"><tr><td width="300"> <div id="right_cnt"><img src="images/logo.png" id="logo" alt="Riccados" /><br /> <div id="nav_ctr"> <div id='cssmenu'> <ul> <li class='active '><a href='index.php'><span>HOME</span></a></li> <li><a href=''><span>MENU</span></a> <ul> <li><a href='menu.php?menuid=1'><span>BREAKFASTS</span></a></li> <li><a href='menu.php?menuid=2'><span>OMELETTES</span></a></li> <li><a href='menu.php?menuid=3'><span>SANDWICHES</span></a></li> <li><a href='menu.php?menuid=4'><span>STARTERS</span></a></li> <li><a href='menu.php?menuid=5'><span>SALADS</span></a></li> <li><a href='menu.php?menuid=6'><span>PASTAS</span></a></li> <li><a href='menu.php?menuid=7'><span>BEEF MAINS</span></a></li> <li><a href='menu.php?menuid=8'><span>PREGO'S & BURGERS</span></a></li> <li><a href='menu.php?menuid=9'><span>LAMB & RIB MAINS</span></a></li> <li><a href='menu.php?menuid=10'><span>CHICKEN & SEAFOOD MAINS</span></a></li> <li><a href='menu.php?menuid=11'><span>COMBOS</span></a></li> <li><a href='menu.php?menuid=12'><span>PIZZAS</span></a></li> <li><a href='menu.php?menuid=13'><span>SUSHI</span></a></li> <li><a href='menu.php?menuid=14'><span>DESSERTS</span></a></li> <li><a href='menu.php?menuid=15'><span>KIDDIES</span></a></li> <li><a href='menu.php?menuid=16'><span>DRINKS</span></a></li> </ul> <li><a href="bookings.php"><span>BOOKINGS</span></a></li> <li><a href='events.php'><span>EVENTS</span></a> </li> <li><a href='gallery.php'><span>GALLERY</span></a> </li> <li><a href='about.php'><span>ABOUT RICCADOS</span></a></li> <li><a href='contact.php'><span>CONTACT RICCADOS</span></a></li></ul> </div> </div> </div> <br /><br /> <br /><br /> <div id="left_cnt"> <table width="300"><tr><td valign="top"> <div id="contact_body"> <a href="https://www.facebook.com/pages/Riccados/1418413421809723?ref=bookmarks" target="_blank"><img src="images/facebook.png" border="0" alt="Find us on Facebook" /></a> <a href="https://twitter.com/RicardosRestuar" class="twitter-follow-button" data-show-count="false" target="_blank"><img src="images/twitter.png" border="0" alt="Find us on Twitter" /></a> <br /> <a href="" target="_blank"><img src="images/linkedin.png" border="0" alt="Find us on LinkedIn" /></a> <a href="" target="_blank"><img src="images/instagram.png" border="0" alt="Find us on Instagram" /></a><br /><br /> <?php print $address1_out ?><br /> <?php if (!empty($address2_out)) { print $address2_out;print "<br>"; } ?> <?php print $suburb_out ?><br /> <?php print $city_out ?><br /> <?php print $code_out ?><br /> Tel: <?php print $tel_out ?><br /> <!--- Fax: --> <?php /*print $fax_out */?><br /> Email: <a href="mailto:<?php print $mail_out ?>" style="color:white"><?php print $mail_out ?></a> </div> <?php $curr_date = date('Y-m-d'); $query4 = "SELECT * from riccados_events WHERE event_sort_date >= '$curr_date'"; // display the products in each subcat $result4 = mysql_query($query4, $conn); $num_rows = mysql_num_rows($result4); if ($num_rows > 0) { echo '<div id="events_heading"></div><div id="events_text">'; } $query4 = "SELECT * from riccados_events WHERE event_sort_date >= '$curr_date' ORDER BY event_sort_date ASC LIMIT 8"; // display the products in each subcat $result4 = mysql_query($query4, $conn); while ($row4 = mysql_fetch_assoc($result4)){ $description_out = $row4["event_name"]; $date_out = $row4["event_date"]; $event_desc_out = $row4["event_desc"]; echo '<p></p><strong>' . $description_out . '</strong><br />' . $date_out . '<p></p>' . $event_desc_out . '<br /><a href="events.php" style="float: right;margin-right: 10px; color: white;text-decoration: none;font-style:italic">Continue Reading</a><br />'; } if ($num_rows > 0) { echo '<br /><br />'; } ?> </div> </div> </td> <td width="700" style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px"><br /> <div id="right_cnt"> <div id="contact_heading_main"></div> <div id="contact_us_cnt"><div id="contact_main_cnt_two_text"><p></p> <?php $qty_out = 1; $qty1 = $qty_out; // This page adds products to the shopping cart if (isset($_POST['Bread'])) { $addtional = $_POST['Bread']; } if (is_numeric ($_SESSION['prodid'])) { // check for a print id $pid = $_SESSION['prodid']; $query4 = "SELECT * from submenu_name WHERE submenu_id = '$pid'"; // display the products in each subcat $result4 = mysql_query($query4, $conn); while ($row4 = mysql_fetch_assoc($result4)){ $submenu_name_out = $row4["submenu_name"]; } // check to see if the cart already contains one of these products /* if ($_SESSION[$session_id]['cart'][$pid]['quantity']) { $qty = $_SESSION[$session_id]['cart'][$pid]['quantity'] + $qty_out; } */ $_SESSION[$session_id]['cart'][$pid] = array ( "product" => $submenu_name_out . $addtional, "quantity" => $qty_out ); echo "<pre>"; print_r($_SESSION); echo "</pre>"; } else { echo "No session for cart"; } /*($_session['cart'][$pid])) { $qty_out = $_SESSION['cart'][$pid] + $qty1; } else { $qty = $qty1; } */ // add to the cart session variable /*$_SESSION['cart'][$pid] = $qty; */ // Display a message echo '<table summary="" align="center" border="0" bgcolor="white"><tr><td width="370">'; echo '<p><font face="arial" size="2" color="#666666">Your product has been added to your cart<br></font></td></tr><tr><td><form action="view_cart.php" method="post"><input type="submit" name="view_barrel" value="View Cart"></form></td></tr></table>'; /*}*/ ?></div></div> </td></tr></table> <div id="footer"><div style="margin-left:50px;font-family:Arial, Helvetica, sans-serif;font-size:11px;color:white;margin-top:35px;display:block"><br />Copyright © 2015 Riccados </div><div style="float: right;margin-right:46px;;font-family:Arial, Helvetica, sans-serif;font-size:11px;color:white;display:inline-block;margin-top:-27px">Website designed and developed by <a href="http://www.it-people.co.za" target="_blank"><img src="images/itpeople.png" alt="IT People" style="margin-top:-10px;width:78px;height:35px" /></a></div> </div> </div> </body> </html> and the view cart <?php session_start(); include 'includes/conn_db.php'; /*$jobid_out = $_GET['jobid'];*/ $med_year = date('Y'); $byear = $med_year - 60; $ryear = $med_year + 1; $year = range($byear,$med_year); $yearr = range($ryear,$med_year); $query = "SELECT * from config"; $result = mysql_query($query, $conn); while ($row = mysql_fetch_assoc($result)){ $address1_out = $row["address1"]; $address2_out = $row["address2"]; $suburb_out = $row["suburb"]; $city_out = $row["city"]; $tel_out = $row["tel"]; $fax_out = $row["fax"]; /*$cell_out = $row["cell"];*/ $mail_out = $row["email"]; $code_out = $row["postal_code"]; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="keywords" content="Riccados, restaurant, diner, chicken, fish, sushi, johannesburg, braamfontein, gauteng, south africa, breakfast, sandwiches, omelettes, soups, burgers, steak, grill, lamb, seafood, poultry, salads, pizza, pasta, sushi, desserts, bakery, drinks, cocktails, shooters, draughts, events, birthdays, celebrations, its your vibe" /><meta name="description" content="Riccados, situated in Braamfontein bringing the ambience and vibe for a perfect end to the day, Riccados promises to be a place for all people to feel welcome." /> <meta name="robots" content="index, follow" /> <title>Riccados Restaurant - It's your vibe</title> <link rel="stylesheet" href="css/font1/droidserif.css" /> <link href="css/riccados.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/thickbox.js"></script> <script type="text/javascript"> /*** Simple jQuery Slideshow Script Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc. Please link out to me if you like it ***/ function slideSwitch() { var $active = $('#slideshow IMG.active'); if ( $active.length == 0 ) $active = $('#slideshow IMG:last'); // use this to pull the images in the order they appear in the markup var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first'); // uncomment the 3 lines below to pull the images in random order // var $sibs = $active.siblings(); // var rndNum = Math.floor(Math.random() * $sibs.length ); // var $next = $( $sibs[ rndNum ] ); $active.addClass('last-active'); $next.css({opacity: 0.0}) .addClass('active') .animate({opacity: 1.0}, 1000, function() { $active.removeClass('active last-active'); }); } $(function() { setInterval( "slideSwitch()", 5000 ); }); </script> </head> <body> <div id="main_cnt"> <table style="width:1000px"><tr><td> <div id="left_cnt"> <div id="slideshow"> <img src="images/contact1.jpg" alt="" class="active" /> <img src="images/contact2.jpg" alt="" /> <img src="images/contact3.jpg" alt="" /> <img src="images/contact4.jpg" alt="" /> </div></div> <table width="1000"><tr><td width="300"> <div id="right_cnt"><img src="images/logo.png" id="logo" alt="Riccados" /><br /> <div id="nav_ctr"> <div id='cssmenu'> <ul> <li class='active '><a href='index.php'><span>HOME</span></a></li> <li><a href=''><span>MENU</span></a> <ul> <li><a href='menu.php?menuid=1'><span>BREAKFASTS</span></a></li> <li><a href='menu.php?menuid=2'><span>OMELETTES</span></a></li> <li><a href='menu.php?menuid=3'><span>SANDWICHES</span></a></li> <li><a href='menu.php?menuid=4'><span>STARTERS</span></a></li> <li><a href='menu.php?menuid=5'><span>SALADS</span></a></li> <li><a href='menu.php?menuid=6'><span>PASTAS</span></a></li> <li><a href='menu.php?menuid=7'><span>BEEF MAINS</span></a></li> <li><a href='menu.php?menuid=8'><span>PREGO'S & BURGERS</span></a></li> <li><a href='menu.php?menuid=9'><span>LAMB & RIB MAINS</span></a></li> <li><a href='menu.php?menuid=10'><span>CHICKEN & SEAFOOD MAINS</span></a></li> <li><a href='menu.php?menuid=11'><span>COMBOS</span></a></li> <li><a href='menu.php?menuid=12'><span>PIZZAS</span></a></li> <li><a href='menu.php?menuid=13'><span>SUSHI</span></a></li> <li><a href='menu.php?menuid=14'><span>DESSERTS</span></a></li> <li><a href='menu.php?menuid=15'><span>KIDDIES</span></a></li> <li><a href='menu.php?menuid=16'><span>DRINKS</span></a></li> </ul> <li><a href="bookings.php"><span>BOOKINGS</span></a></li> <li><a href='events.php'><span>EVENTS</span></a> </li> <li><a href='gallery.php'><span>GALLERY</span></a> </li> <li><a href='about.php'><span>ABOUT RICCADOS</span></a></li> <li><a href='contact.php'><span>CONTACT RICCADOS</span></a></li></ul> </div> </div> </div> <br /><br /> <br /><br /> <div id="left_cnt"> <table width="300"><tr><td valign="top"> <div id="contact_body"> <a href="https://www.facebook.com/pages/Riccados/1418413421809723?ref=bookmarks" target="_blank"><img src="images/facebook.png" border="0" alt="Find us on Facebook" /></a> <a href="https://twitter.com/RicardosRestuar" class="twitter-follow-button" data-show-count="false" target="_blank"><img src="images/twitter.png" border="0" alt="Find us on Twitter" /></a> <br /> <a href="" target="_blank"><img src="images/linkedin.png" border="0" alt="Find us on LinkedIn" /></a> <a href="" target="_blank"><img src="images/instagram.png" border="0" alt="Find us on Instagram" /></a><br /><br /> <?php print $address1_out ?><br /> <?php if (!empty($address2_out)) { print $address2_out;print "<br>"; } ?> <?php print $suburb_out ?><br /> <?php print $city_out ?><br /> <?php print $code_out ?><br /> Tel: <?php print $tel_out ?><br /> <!--- Fax: --> <?php /*print $fax_out */?><br /> Email: <a href="mailto:<?php print $mail_out ?>" style="color:white"><?php print $mail_out ?></a> </div> <?php $curr_date = date('Y-m-d'); $query4 = "SELECT * from riccados_events WHERE event_sort_date >= '$curr_date'"; // display the products in each subcat $result4 = mysql_query($query4, $conn); $num_rows = mysql_num_rows($result4); if ($num_rows > 0) { echo '<div id="events_heading"></div><div id="events_text">'; } $query4 = "SELECT * from riccados_events WHERE event_sort_date >= '$curr_date' ORDER BY event_sort_date ASC LIMIT 8"; // display the products in each subcat $result4 = mysql_query($query4, $conn); while ($row4 = mysql_fetch_assoc($result4)){ $description_out = $row4["event_name"]; $date_out = $row4["event_date"]; $event_desc_out = $row4["event_desc"]; echo '<p></p><strong>' . $description_out . '</strong><br />' . $date_out . '<p></p>' . $event_desc_out . '<br /><a href="events.php" style="float: right;margin-right: 10px; color: white;text-decoration: none;font-style:italic">Continue Reading</a><br />'; } if ($num_rows > 0) { echo '<br /><br />'; } ?> </div> </div> </td> <td width="700" style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px;;<br /> <div id="right_cnt"> <div id="contact_heading_main"></div> <div id="contact_us_cnt"><div id="contact_main_cnt_two_text"><p></p> <?php if (isset($_POST['submit'])) { foreach ($_POST['qty'] as $key => $value) { if (($value == 0) AND (is_numeric($value))) { unset ($_SESSION[$session_id]['cart'][$key]); } elseif (is_numeric($value) AND ($value > 0)) { $_SESSION[$session_id]['cart'][$key] = $value; } } } // check if the shopping cart is empty $empty = TRUE; if (isset($_SESSION[$session_id]['cart'])) { foreach($_SESSION[$session_id]['cart'] as $key => $value) { if (isset($value)) { $empty = FALSE; } } } // Display the cart if it is not empty if (!$empty) { // Retrieve all of the information for the products in the cart $query = 'SELECT * FROM submenu_name WHERE submenu_id IN ('; foreach ($_SESSION[$session_id]['cart'] as $key => $value) { $query .= $key . ','; } $query = substr ($query, 0, -1) . ') ORDER BY submenu_name ASC'; $result = mysql_query($query); $final_item_name = $_SESSION[$session_id]['cart']['product']; // create a table and a form print <<<TOP <table summary="" width="580" align="center" style="; <tr> <td nowrap align="center">Your Shopping Cart</td></tr></table> <table summary="" align="center" width="580" border="1" style="; <tr> <td> <table border="0" width="580" cellpadding="3"> <tr> <td align="left" width="250"><font face="arial" size="2" color="#666666"><b>Item</b></font></td> <td align="center" width="70"><font face="arial" size="2" color="#666666"><b>Qty</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="#666666"><b>Unit Price</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="#666666"><b>Line Total</b></font></td> </tr> <form action="view_cart.php" method="post"> TOP; // Print each item $total = 0; // total cost of the order while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $list_price_out = $row['item_price']; $list_price_out = number_format($list_price_out, 2, '.', ''); // Calculate the total and subtotals $subtotal = $_SESSION[$session_id]['cart'][$row['submenu_id']] * $list_price_out; $subtotal = number_format($subtotal, 2, '.', ''); $total += $subtotal; $total = number_format($total, 2, '.', ''); // print the row print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="#666666">$final_item_name</font></td> <td align="center"><input type="text" size="3" name="qty[{$row['submenu_id']}]" value="{$_SESSION['cart'][$row['submenu_id']]}"> </td><td align="right"><font face="arial" size="2" color="#666666">R $list_price_out </font></td><td align="right"><font face="arial" size="2" color="#66666">R $subtotal</font></td> </tr> ROW; } // end of the WHILE loop // print the footer and close the table and the form //$finq = $_SESSION['cart'][$row['prod_id']]; print <<<FOOTER <tr> <td></td><td></td> <td align="right" valign="top"><font face="arial" size="2" color="#666666"><b>Sub Total:</b></font></td> <td align="right" valign="top"><font face="arial" size="2" color="#666666"><b> $total</b></font><br></td> </tr> FOOTER; print <<<FOOTER1 </table> <table width="500" align="center" border="1"> <tr><td align="center"><font face="arial" size="2" color="#666666">To change the quantities change the figure in the Qty box, and your Shopping Cart will automatically be updated after clicking on the <strong>Update Cart button</strong>. <br>If you change the quantity to zero, then that item will be deleted from your Shopping Cart.</font></td></tr> </table> <table width="580" align="center"> <tr><br><td width="85"></td><td width="150" valign="top" align="center"><input type="submit" name="submit" value="Update Cart"></form></td><td width="150" align="center"> </td><td width="130" align="center"><form action="checkout.php" method="post"><input type="submit" name="checkout" value="Checkout"></form> </td><td width="85"></td></tr></table> FOOTER1; } else { print <<<EMP <table border="0" width="580" align="center" style="; <tr> <td> <table summary="" width="300" border="1" style="border-color:#960232" bgcolor="#f8f3c2" align="center"> <tr> <td> <table border="0" width="300" cellpadding="3"> <tr><td align="center"><p><font face="arial" size="2" color="#5e5d5d">Your cart is empty!</font></td></tr></table></table> EMP; } ?> </div></div> </tr> </table></table> <div id="footer"><div style="margin-left:50px;font-family:Arial, Helvetica, sans-serif;font-size:11px;color:white;margin-top:35px;display:block"><br />Copyright © 2015 Riccados </div><div style="float: right;margin-right:46px;;font-family:Arial, Helvetica, sans-serif;font-size:11px;color:white;display:inline-block;margin-top:-27px">Website designed and developed by <a href="http://www.it-people.co.za" target="_blank"><img src="images/itpeople.png" alt="IT People" style="margin-top:-10px;width:78px;height:35px" /></a></div> </div> </div> </body> </html> I am also getting a Undefined variable: session_id in teh view cart and the first time I run the script in add to cart I really appreaciate all teh assistance Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513144 Share on other sites More sharing options...
mac_gyver Posted June 4, 2015 Share Posted June 4, 2015 the code that QuickOldCar posted, using the session_id as an array key, is overly complex and problematic. a single session_regenerate_id() statement (which you would be using if you are concerned about session security) will loose the contents stored in the cart. there was nothing wrong with the original definition of your cart and in fact it looked like something that would have been suggested here on phpfreaks. part of what i recommend would have separated your main program logic from the unchanging part of the page, so that you can POST JUST THE RELEVANT part of your code that you need help with. no one will try to sift through hundreds of non-relevant lines of code to just locate, then figure out the code you are having a problem with. besides the error about the session_id variable, that you shouldn't have changed your code to use anyway, what sort of problem is your code having that you need to fix? and please, just post the relevant part of your code. Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513145 Share on other sites More sharing options...
Kevinh Posted June 4, 2015 Author Share Posted June 4, 2015 Hi Thanks for your input I am trying to generate a shopping cart where for example if someone orders a breakfast they can choose what breads they would like and I would like to append this to the product name. the view cart is as follows <?php if (isset($_POST['submit'])) { foreach ($_POST['qty'] as $key => $value) { if (($value == 0) AND (is_numeric($value))) { unset ($_SESSION[$session_id]['cart'][$key]); } elseif (is_numeric($value) AND ($value > 0)) { $_SESSION[$session_id]['cart'][$key] = $value; } } } // check if the shopping cart is empty $empty = TRUE; if (isset($_SESSION[$session_id]['cart'])) { foreach($_SESSION[$session_id]['cart'] as $key => $value) { if (isset($value)) { $empty = FALSE; } } } // Display the cart if it is not empty if (!$empty) { // Retrieve all of the information for the products in the cart $query = 'SELECT * FROM submenu_name WHERE submenu_id IN ('; foreach ($_SESSION[$session_id]['cart'] as $key => $value) { $query .= $key . ','; } $query = substr ($query, 0, -1) . ') ORDER BY submenu_name ASC'; $result = mysql_query($query); echo '<tr><td align="left"><font face="arial" size="2" color="#666666">' . $value["product"] . '</font></td><td align="center"><input type="text" size="3" name="' . $value{$row["submenu_id"]} . '" value="' . $value["quantity"] . '"></td><td align="right"><font face="arial" size="2" color="#666666">R ' . $list_price_out . ' </font></td><td align="right"><font face="arial" size="2" color="#66666">R ' . $subtotal . '</font></td> </tr>'; /* $final_item_name = $value['product'];*/ // create a table and a form print <<<TOP <table summary="" width="580" align="center" style="; <tr> <td nowrap align="center">Your Shopping Cart</td></tr></table> <table summary="" align="center" width="580" border="1" style="; <tr> <td> <table border="0" width="580" cellpadding="3"> <tr> <td align="left" width="250"><font face="arial" size="2" color="#666666"><b>Item</b></font></td> <td align="center" width="70"><font face="arial" size="2" color="#666666"><b>Qty</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="#666666"><b>Unit Price</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="#666666"><b>Line Total</b></font></td> </tr> <form action="view_cart.php" method="post"> TOP; // Print each item $total = 0; // total cost of the order while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $list_price_out = $row['item_price']; $list_price_out = number_format($list_price_out, 2, '.', ''); // Calculate the total and subtotals $subtotal =$key[$row['submenu_id']] * $list_price_out; $subtotal = number_format($subtotal, 2, '.', ''); $total += $subtotal; $total = number_format($total, 2, '.', ''); // print the row print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="#666666">{$value['product']}</font></td> <td align="center"><input type="text" size="3" name="$value{$row['submenu_id']}" value="{$value['quantity']}"> </td><td align="right"><font face="arial" size="2" color="#666666">R $list_price_out </font></td><td align="right"><font face="arial" size="2" color="#66666">R $subtotal</font></td> </tr> ROW; } // end of the WHILE loop // print the footer and close the table and the form and add to cart <?php $qty_out = 1; $qty1 = $qty_out; // This page adds products to the shopping cart if (isset($_POST['Bread'])) { $addtional = $_POST['Bread']; } if (is_numeric ($_SESSION['prodid'])) { // check for a print id $pid = $_SESSION['prodid']; $query4 = "SELECT * from submenu_name WHERE submenu_id = '$pid'"; // display the products in each subcat $result4 = mysql_query($query4, $conn); while ($row4 = mysql_fetch_assoc($result4)){ $submenu_name_out = $row4["submenu_name"]; } // check to see if the cart already contains one of these products /* if ($_SESSION[$session_id]['cart'][$pid]['quantity']) { $qty = $_SESSION[$session_id]['cart'][$pid]['quantity'] + $qty_out; } */ $_SESSION[$session_id]['cart'][$pid] = array ( "product" => $submenu_name_out . $addtional, "quantity" => $qty_out ); echo "<pre>"; print_r($_SESSION); echo "</pre>"; } else { echo "No session for cart"; } /*($_session['cart'][$pid])) { $qty_out = $_SESSION['cart'][$pid] + $qty1; } else { $qty = $qty1; } */ // add to the cart session variable /*$_SESSION['cart'][$pid] = $qty; */ // Display a message echo '<table summary="" align="center" border="0" bgcolor="white"><tr><td width="370">'; echo '<p><font face="arial" size="2" color="#666666">Your product has been added to your cart<br></font></td></tr><tr><td><form action="view_cart.php" method="post"><input type="submit" name="view_barrel" value="View Cart"></form></td></tr></table>'; /*}*/ ?> I have just included the PHP part that deals with the shopping cart Thanks for your time and patience Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513147 Share on other sites More sharing options...
mac_gyver Posted June 4, 2015 Share Posted June 4, 2015 (edited) i am trying to generate a shopping cart where for example if someone orders a breakfast they can choose what breads they would like and I would like to append this to the product name. when you display the cart, you would also retrieve the category information for the items in the cart and pre-process the retrieved data to get a list of the bread(s) in the order (you may in fact want to retrieve and save the results from the query by category so that similar items are listed together when displayed.) then display the bread information the way you want when you display the contents of the cart. in order you specifically help you, we would need an example of your database table data showing how you know if an item is from the 'bread' category? at the point of displaying the cart, you cannot use any $_POST data, because there will only be $_POST data after you submit a form. you should also post an example of how you want the output to appear when there is a bread item(s) in the cart. short-answer - at the point of displaying the cart, you have as input data - 1) a page request to display the cart 2) the contents of the cart. the product id and the quantity are all you need to store in the cart. this results in the simplest and cleanest general purpose code. 3) the data about the items that is stored your database table(s). your task is to produce the result you want using the available input data. Edited June 4, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513182 Share on other sites More sharing options...
Kevinh Posted June 5, 2015 Author Share Posted June 5, 2015 I have the breakfasts listed where the client selects their breakfast. Then on the following page there are rour radio buttins that allows the client to select their bread, then from this page I go to the add to cart page where I would like to display the breakfast alnong with the type of bread that the client wants with their breakfast At the moment the breads are hard coded in the choices_bread page. The product ID is stored in a session in the bread_choices page Then I go to the add to cart page where I send the selection of bread as well as retrieving the product id information I stored in the session The menu page $query4 = "SELECT * from submenu_name WHERE menu_id = '$menuid_out' AND active = '1'"; // display the products in each subcat $result4 = mysql_query($query4, $conn); while ($row4 = mysql_fetch_assoc($result4)){ $submenu_id_out = $row4["submenu_id"]; $submenu_name_out = $row4["submenu_name"]; $item_price_out = $row4["item_price"]; $menu_desc_out = $row4["menu_desc"]; $image_out = $row4["image"]; /*$description_out = $row4["description"]; */ $num = mysql_num_rows($result4); if ($num > 0) { print <<<thumb <td width="230" height="180" align="left" valign="top" style="border:0px solid #d2aa64"> <img src="images/menu/$image_out" alt="$submenu_name_out" border="0" style="width: 220px"><a href="menu_redirect.php?prodid=$submenu_id_out&menuid=$menuid_out"><img src="images/add_to_cart.png" alt="add to cart" style="border: 0px; width: 71px; height:15px;float: right;magrin-bottom:0px" /></a><br /><div style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px;padding-left:5px">$submenu_name_out</b><br />Price R $item_price_out<br /><br />$menu_desc_out<br /> </div></div> </td> thumb; } Choices of bread <?php $prodid_out = $_GET['prodid']; if (!preg_match("/^[0-9]{1,3}$/", $prodid_out)) die("Bad category, please re-enter."); $_SESSION['prodid'] = $prodid_out; ?> </td><td width="700" valign="top"> <div id="right_cnt"> <div style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px;margin-left:150px">Please select the type of bread with your meal!</div><br /> <form method="post" action="add_to_cart.php" style="margin-left:150px"> <input type="radio" name="Bread" value="White" checked="checked" /><div style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px;margin-left:10px;display:inline-block">White</div> <input type="radio" name="Bread" value="Brown" /><div style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px;margin-left:10px;display:inline-block">Brown</div> <input type="radio" name="Bread" value="Rye" /><div style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px;margin-left:10px;display:inline-block">Rye</div> <input type="radio" name="Bread" value="Seeded" /><div style="color: white;font-family:Arial, Helvetica, sans-serif;font-size:14px;margin-left:10px;display:inline-block">Seeded</div><br /><br /> <input type="submit" value="Select Bread" style="margin-left:100px" /> </form> Add to cart back to original that I created <?php $qty_out = 1; $qty1 = $qty_out; // This page adds products to the shopping cart if (isset($_POST['Bread'])) { $_SESSION['bread'] = $_POST['Bread']; } if (is_numeric ($_SESSION['prodid'])) { // check for a print id $pid = $_SESSION['prodid']; // check to see if the cart already contains one of these products if (isset ($_session['cart'][$pid])) { $qty_out = $_SESSION['cart'][$pid] + $qty1; } else { $qty = $qty1; } // add to the cart session variable $_SESSION['cart'][$pid] = $qty; // Display a message echo '<table summary="" align="center" border="0" bgcolor="white"><tr><td width="370">'; echo '<p><font face="arial" size="2" color="#666666">Your product has been added to your cart<br></font></td></tr><tr><td><form action="view_cart.php" method="post"><input type="submit" name="view_barrel" value="View Cart"></form></td></tr></table>'; } And my view cart <?php if (isset($_POST['submit'])) { foreach ($_POST['qty'] as $key => $value) { if (($value == 0) AND (is_numeric($value))) { unset ($_SESSION['cart'][$key]); } elseif (is_numeric($value) AND ($value > 0)) { $_SESSION['cart'][$key] = $value; } } } // check if the shopping cart is empty $empty = TRUE; if (isset($_SESSION['cart'])) { foreach($_SESSION['cart'] as $key => $value) { if (isset($value)) { $empty = FALSE; } } } // Display the cart if it is not empty if (!$empty) { // Retrieve all of the information for the products in the cart $query = 'SELECT * FROM submenu_name WHERE submenu_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query = substr ($query, 0, -1) . ') ORDER BY submenu_name ASC'; $result = mysql_query($query); $final_item_name = $_SESSION['final_name']; // create a table and a form print <<<TOP <table summary="" width="580" align="center" style="; <tr> <td nowrap align="center">Your Shopping Cart</td></tr></table> <table summary="" align="center" width="580" border="1" style="; <tr> <td> <table border="0" width="580" cellpadding="3"> <tr> <td align="left" width="250"><font face="arial" size="2" color="#666666"><b>Item</b></font></td> <td align="center" width="70"><font face="arial" size="2" color="#666666"><b>Qty</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="#666666"><b>Unit Price</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="#666666"><b>Line Total</b></font></td> </tr> <form action="view_cart.php" method="post"> TOP; // Print each item $total = 0; // total cost of the order while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $list_price_out = $row['item_price']; $list_price_out = $row['item_price']; $list_price_out = number_format($list_price_out, 2, '.', ''); // Calculate the total and subtotals $subtotal = $_SESSION['cart'][$row['submenu_id']] * $list_price_out; $subtotal = number_format($subtotal, 2, '.', ''); $total += $subtotal; $total = number_format($total, 2, '.', ''); // print the row print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="#666666">$final_item_name</font></td> <td align="center"><input type="text" size="3" name="qty[{$row['submenu_id']}]" value="{$_SESSION['cart'][$row['submenu_id']]}"> </td><td align="right"><font face="arial" size="2" color="#666666">R $list_price_out </font></td><td align="right"><font face="arial" size="2" color="#66666">R $subtotal</font></td> </tr> ROW; } // end of the WHILE loop // print the footer and close the table and the form //$finq = $_SESSION['cart'][$row['prod_id']]; print <<<FOOTER <tr> <td></td><td></td> <td align="right" valign="top"><font face="arial" size="2" color="#666666"><b>Sub Total:</b></font></td> <td align="right" valign="top"><font face="arial" size="2" color="#666666"><b> $total</b></font><br></td> </tr> FOOTER; print <<<FOOTER1 </table> <table width="500" align="center" border="1"> <tr><td align="center"><font face="arial" size="2" color="#666666">To change the quantities change the figure in the Qty box, and your Shopping Cart will automatically be updated after clicking on the <strong>Update Cart button</strong>. <br>If you change the quantity to zero, then that item will be deleted from your Shopping Cart.</font></td></tr> </table> <table width="580" align="center"> <tr><br><td width="85"></td><td width="150" valign="top" align="center"><input type="submit" name="submit" value="Update Cart"></form></td><td width="150" align="center"> </td><td width="130" align="center"><form action="checkout.php" method="post"><input type="submit" name="checkout" value="Checkout"></form> </td><td width="85"></td></tr></table> FOOTER1; } else { print <<<EMP <table border="0" width="580" align="center" style="; <tr> <td> <table summary="" width="300" border="1" style="border-color:#960232" bgcolor="#f8f3c2" align="center"> <tr> <td> <table border="0" width="300" cellpadding="3"> <tr><td align="center"><p><font face="arial" size="2" color="#5e5d5d">Your cart is empty!</font></td></tr></table></table> EMP; } ?> Menu database table submenu_id menu_id submenu_name menu_desc image item_price active 1 1 Riccado's Breakfast 2 Eggs, Bacon, oven roasted Cherry Tomatoes, Itali riccados_breakfast.png 49 1 2 1 Simply Breakfast Bacon, Oven Roasted Cherry Tomatoes & 2 Eggs serve simply_breakfast.png 30 1 3 1 Chef's Breakfast 2 Eggs, Bacon, Oven Roasted Cherry Tomatoes, White chefs_breakfast.png 65 1 4 1 Egg Benedict 2 Poached eggs, asparaguise, baby spinach with ham egg_benedict.png 65 1 5 1 Salmon Benedict 2 poached eggs, asparaguise, baby spinach, salmon salmon_benedict.png 85 1 6 1 French Toast Eggs, cinnamon, brioche bread served with honey cr french_toast.png 55 1 The breakfasts are stored as menu item 1, I do not have a bread category as such Thanks for the help, if I need to restructure it and recode then that is ok as I am sure this might not be the best way of doing it Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513219 Share on other sites More sharing options...
Kevinh Posted June 5, 2015 Author Share Posted June 5, 2015 Sorry database did not come corerctly here it is /*SQLyog Community v8.71MySQL - 5.5.24-log**********************************************************************//*!40101 SET NAMES utf8 */;create table `submenu_name` ( `submenu_id` int (9), `menu_id` int (9), `submenu_name` varchar (750), `menu_desc` varchar (2250), `image` varchar (450), `item_price` int (7), `active` int (1));insert into `submenu_name` (`submenu_id`, `menu_id`, `submenu_name`, `menu_desc`, `image`, `item_price`, `active`) values('1','1','Riccado\'s Breakfast','2 Eggs, Bacon, oven roasted Cherry Tomatoes, Italian sausages, white mushrooms served with two slices of toast','riccados_breakfast.png','49','1');insert into `submenu_name` (`submenu_id`, `menu_id`, `submenu_name`, `menu_desc`, `image`, `item_price`, `active`) values('2','1','Simply Breakfast','Bacon, Oven Roasted Cherry Tomatoes & 2 Eggs served with two slices of toast','simply_breakfast.png','30','1');insert into `submenu_name` (`submenu_id`, `menu_id`, `submenu_name`, `menu_desc`, `image`, `item_price`, `active`) values('3','1','Chef\'s Breakfast','2 Eggs, Bacon, Oven Roasted Cherry Tomatoes, White Mushrooms, Beans, hash browns served with two slices of toast','chefs_breakfast.png','65','1');insert into `submenu_name` (`submenu_id`, `menu_id`, `submenu_name`, `menu_desc`, `image`, `item_price`, `active`) values('4','1','Egg Benedict','2 Poached eggs, asparaguise, baby spinach with ham','egg_benedict.png','65','1');insert into `submenu_name` (`submenu_id`, `menu_id`, `submenu_name`, `menu_desc`, `image`, `item_price`, `active`) values('5','1','Salmon Benedict','2 poached eggs, asparaguise, baby spinach, salmon with two slices of toast','salmon_benedict.png','85','1'); Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513220 Share on other sites More sharing options...
mac_gyver Posted June 5, 2015 Share Posted June 5, 2015 my recommendation is to store the bread choices in your database table, along with all the other items that can be added to the cart. to organize the type of items in the database table, you would have a category table that defines the category_id and the category name, then in the table holding data about all the items, you would have a category_id column. the bread choices would get whatever category_id is assigned to the bread category name. once you do this, your hard-coded bread menu can be dynamically built, the bread choice can be added to the cart just like any of the other items, and displaying the contents of the cart will just involve getting the item id's from the cart, retrieving the information about the items from the database table, and displaying the information as you loop over the rows that the database query matched. Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513221 Share on other sites More sharing options...
Kevinh Posted June 5, 2015 Author Share Posted June 5, 2015 Ok but then will we be able to link a specific bread type with a specific breakfast if a client orders two different breakfasts with different breads we need to let the kitchen which goes with which Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513239 Share on other sites More sharing options...
mac_gyver Posted June 5, 2015 Share Posted June 5, 2015 if multiple 'orders' can be added to a cart, you would create a sub-array in the cart for each order - $_SESSION['cart'][order_number][pid] = quantity // an example, assuming white bread ends up with a pid of 12 and brown bread has a pid of 13 - $_SESSION['cart'][1][1] = 1; // order #1, menu item pid 1, qty 1 $_SESSION['cart'][1][12] = 2; // order #1, white bread, qty 2 $_SESSION['cart'][2][1] = 1; // order #2, menu item pid 1, qty 1 $_SESSION['cart'][2][13] = 2; // order #2, brown bread, qty 2 Quote Link to comment https://forums.phpfreaks.com/topic/296602-storing-multipe-session-variables-for-a-cart/#findComment-1513254 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.