luckyirish470 Posted May 18, 2010 Share Posted May 18, 2010 Hello, I'm trying to create a simple database driven shopping cart that merely displays the selected items and adds up the total price. I think the error is in my $_SESSION variable but for the life of me I can't figure out what it is. Right now the script can tell the number of items the user has put in the cart, but the script just displays the last item added to the cart over and over again regardless of whether or not the user selects different items(the number of times the item is displayed is equal to the number of total items the user has placed in the cart) The total price calculation is working, but it is adding the prices for the same item over and over again (once again, this item that is displayed repeatedly is last item the user added to the cart) When I run the "print $_SESSION[items]' at the top of the code, the only thing that is displayed is "Array." Any ideas where I'm going wrong? Thank you very much in advance! Here is the code that gets the information for the actual shopping cart. This script displays the items the users can select to put in their shopping cart... ........................................................................ <?php session_start(); if(!isset($_SESSION['auth']) or $_SESSION['auth'] != "yes") { header("location: loginfinal.php"); header('Content-type: image/jpg'); exit(); } ?> <?php /* Program name: page_links.inc * Description: file builds a list of links from the database */ ini_set("display_errors","on"); error_reporting(E_ALL | E_STRICT); ini_set("include_path","./includes"); ?> <html> <head><title>Customer Products</title></head> <body> <?php include("dbinfo.inc"); $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("couldn't connect to server"); $query = "SELECT * FROM products WHERE category='$_GET[category]'"; $result = mysqli_query($cxn,$query) or die("Couldn't execute query ".mysqli_error($cxn)); while($row = mysqli_fetch_assoc($result)) { extract($row); echo " <ul><li> <img src='images/$thumbnail'/> <br /> <a href='shoppingcart.php?productnumber=$productnumber&thumbnail=$thumbnail&productname=$productname&price=$price'>$productname </a><br /> $description <br/> \$$price </li> </ul>"; } ?> </body></html> ... End first script ... Now here is the actual shopping cart script... <?php session_start(); if(!isset($_SESSION['auth']) or $_SESSION['auth'] != "yes") { header("location: loginfinal.php"); exit(); } ?> <?php print $_SESSION[items]; /* Program name: shoppingcart.inc * Description: file builds a list of links from the database */ ini_set("display_errors","on"); error_reporting(E_ALL | E_STRICT); ini_set("include_path","./includes"); ?> <html> <head><title>Shopping Cart</title></head> <body> <?php $total=0; include("dbinfo.inc"); $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("couldn't connect to server"); if(!isset($_SESSION['items'])) { $_SESSION['items'] = array('productnumber'=>'$productnumber', 'thumbnail'=>'$thumbnail', 'productname'=>'$productname', 'price'=> '$price'); } $_SESSION['items'][] = $_GET['productnumber']; foreach($_SESSION['items'] as $cat_id) { $query = "SELECT * FROM products WHERE productnumber='$_GET[productnumber]'"; $result = mysqli_query($cxn,$query) or die("Couldn't execute query ".mysqli_error($cxn)); while($row = mysqli_fetch_assoc($result)) { extract($row); echo " <ul><li> <img src='images/$_GET[thumbnail]'/> <br /> $_GET[productname] <br/> \$$_GET[price] </li> </ul>"; $total= $total + $price; } } ?> <?php echo" <h3> Total Price </h3> \$$total <br /> <a href='thankyou.php'>Submit Order </a> <a href='products.php?total=$total'>Keep Shopping </a>"; ?> </body> </html> ... Here is a link to the live site....http://notestotheman.com/php/lesson6/finalcountdown/loginfinal.php username=bambi password=1234567 Quote Link to comment https://forums.phpfreaks.com/topic/202122-shopping-cart-problem/ Share on other sites More sharing options...
AE117 Posted May 18, 2010 Share Posted May 18, 2010 foreach($_SESSION['items'] as $cat_id) { $query = "SELECT * FROM products WHERE productnumber='$_GET[productnumber]'"; } maybe... $query = "SELECT * FROM products WHERE productnumber='$cat_id'"; I am having a hard time following the way you write code so I apologize if im wrong I will keep looking at it to see if i come up with anything else Quote Link to comment https://forums.phpfreaks.com/topic/202122-shopping-cart-problem/#findComment-1059884 Share on other sites More sharing options...
luckyirish470 Posted May 18, 2010 Author Share Posted May 18, 2010 Thank you very much for the quick reply! I really appreciate it! Unfortunately I just tried changing the $_GET[productname] to $cat_id and it didn't seem to alter the functionality of the script. Thank you very much anyways though! Adam Quote Link to comment https://forums.phpfreaks.com/topic/202122-shopping-cart-problem/#findComment-1059889 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.