Becca Posted July 4, 2008 Share Posted July 4, 2008 I'm trying to do a simple shopping cart for a Basic PHP class. It consists of three main pages - the login page, the product page, and the shopping cart. The items can be passed through the URL to the shopping cart, or the information is to be passed through $_SESSION. Either way, the user needs to be able to add more items to the shopping cart (we don't have to worry about ability to delete them). I'm having no issues with the login page, that's working fine. The problem that I'm having is moving items from the product page to the shopping cart. I think the issue I'm having is how to display the $_GET array. I'm currently trying to pass the information in the URL, but if someone could walk me through passing it in the $_Session (especially if that's easier for adding multiple items) it would be greatly appreciated. Here's the information for the Product page: <?php session_start(); /* Program name: furniture.php * Description: Displays selected furniture category */ ini_set("display_errors","on"); error_reporting(E_ALL | E_STRICT); ini_set("include_path","./supportfiles"); /*Test if user logged in, if not, return to loginpage*/ if(!isset($_SESSION['auth']) or $_SESSION['auth'] != "yes") { header("location: login.php"); exit(); } else { /*Test to see whether category button was clicked*/ if(isset($_POST['cat_select']) and $_POST['cat_select'] == "yes") { include("cat_select.inc"); /*Check the Database for all items within that category*/ $sql = "SELECT * FROM $tablename WHERE Category='$_POST[Category]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query."); /*Run a while loop that displays each item found by the inquiry*/ echo "<table border='1' cellpadding='5'><tr>"; while($row = mysqli_fetch_assoc($result)) { extract($row); echo "<td><img src='$ImgURL'><br />"; echo "$ItemName <br />"; echo "$ItemDesc <br />"; echo "$ $Price <br /><a href='cart.php?ItemID=$ItemID'>Buy Me!</a></td>"; } echo "</tr></table>"; exit(); } /*If nothing else, include Category selection*/ else { include("cat_select.inc"); } } ?> And so far for the cart: <?php session_start(); /* Program name: cart.php * Description: Displays shopping cart */ ini_set("display_errors","on"); error_reporting(E_ALL | E_STRICT); ini_set("include_path","./supportfiles"); include("dbinfo_test.inc"); $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("couldn't connect to server"); $tablename = "furniturestore"; $items = array($_GET['ItemID']); /*Test if user logged in, if not, return to loginpage*/ if(!isset($_SESSION['auth']) or $_SESSION['auth'] != "yes") { header("location: login.php"); exit(); } else { /*If there are no items in the cart, provide a link to go back to the catalog*/ if(@sizeof($items = 0)) { echo "There are no items currently in your cart<br /><a href='furniture.php'>Click here to continue shopping</a>"; } else { /*If there are items already in the cart, display them here*/ echo "<table>"; foreach($items as $_GET['ItemID']) { $sql = "SELECT * FROM $tablename WHERE ItemID='$_SESSION[itemID]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query."); echo "<tr><td>$items</td></tr>"; } echo "</table>"; } /*Function to sum up all items in cart*/ } ?> Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/ Share on other sites More sharing options...
br0ken Posted July 4, 2008 Share Posted July 4, 2008 To pass information through sessions you first have to pass the information in the URL, extract it from $_GET or $_POST and then add it manually to $_SESSION. Are you saying you don't know how to assign information to a session variable? Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-581907 Share on other sites More sharing options...
Becca Posted July 4, 2008 Author Share Posted July 4, 2008 I'm having problems figuring out how to extract it. I'm able to pass the variable via the URL, but I'm having difficulty then extracting that data, or, alternately saving the data directly to the Session variable. My class didn't talk about it very in depth, and what I have been able to find online hasn't helped me too much :-\ Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-581916 Share on other sites More sharing options...
br0ken Posted July 4, 2008 Share Posted July 4, 2008 Does this help? <?php session_start(); // This must be the first thing the page $_SESSION['var1'] = $_GET['var1']; ?> Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-581969 Share on other sites More sharing options...
Becca Posted July 4, 2008 Author Share Posted July 4, 2008 I revamped the codes slightly to try to just pass it as a straight $_SESSION versus $_GET via the URL, rather than reassigning the variable. Here's what I've got: Problem is that it's telling me Notice: Undefined index: $ItemID in C:\xampp\htdocs\dynamicphp\cart.php on line 14 This refers to this line in cart.php $items = array($_SESSION['$ItemID']); So, it seems the script is running, but that somewhere, the information isn't passing? Thanks for taking a look at this, it's greatly appreciated! For the Catalog: <?php session_start(); /* Program name: furniture.php * Description: Displays selected furniture category */ ini_set("display_errors","on"); error_reporting(E_ALL | E_STRICT); ini_set("include_path","./supportfiles"); /*Test if user logged in, if not, return to loginpage*/ if(!isset($_SESSION['auth']) or $_SESSION['auth'] != "yes") { header("location: login.php"); exit(); } elseif (isset($_POST['tocart']) and $_POST['tocart'] == "yes") { if (isset($_POST['buyid']) and $_POST['buyid'] == "selected") { $_SESSION['$ItemID'] = "$ItemID"; } header("location: cart.php"); exit(); } else { /*Test to see whether category button was clicked*/ if(isset($_POST['cat_select']) and $_POST['cat_select'] == "yes") { include("cat_select.inc"); /*Check the Database for all items within that category*/ $sql = "SELECT * FROM $tablename WHERE Category='$_POST[Category]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query."); /*FORM TO PLACE ORDER*/ echo "<form action='$_SERVER[php_SELF]' method='POST'>"; echo "<table border='1' cellpadding='5'><tr>"; /*Run a while loop that displays each item found by the inquiry*/ while($row = mysqli_fetch_assoc($result)) { extract($row); echo "<td><img src='$ImgURL'><br />"; echo "$ItemName <br />"; echo "$ItemDesc <br />"; echo "$ $Price <br /><input type='radio' name='buyid' value='$ItemID'"; /*If the item is selected, indicate this when information is sent*/ if ($ItemID == @$_POST['buyid']) { echo "selected='selected'>\n"; } else { echo ">\n"; } echo "Buy Me!</td>"; } echo "</tr></table>"; echo "<div id='submit'><input type='hidden' name='tocart' value='yes'><input type='submit' value='Add to Cart'></div></form>"; exit(); } /*If nothing else, include Category selection*/ else { include("cat_select.inc"); } } ?> For the Cart: <?php session_start(); /* Program name: cart.php * Description: Displays shopping cart */ ini_set("display_errors","on"); error_reporting(E_ALL | E_STRICT); ini_set("include_path","./supportfiles"); include("dbinfo_test.inc"); $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("couldn't connect to server"); $tablename = "furniturestore"; $items = array($_SESSION['$ItemID']); /*Test if user logged in, if not, return to loginpage*/ if(!isset($_SESSION['auth']) or $_SESSION['auth'] != "yes") { header("location: login.php"); exit(); } else { /*If there are no items in the cart, provide a link to go back to the catalog*/ if(@sizeof($items = 0)) { echo "There are no items currently in your cart<br /><a href='furniture.php'>Click here to continue shopping</a>"; } else { /*If there are items already in the cart, display them here*/ echo "<table>"; foreach($items as $_SESSION['$ItemID']) { $sql = "SELECT * FROM $tablename WHERE ItemID='$_SESSION[itemID]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query."); echo "<tr><td>$items</td></tr>"; } echo "</table>"; } /*Function to sum up all items in cart*/ } ?> Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-581992 Share on other sites More sharing options...
darkfreaks Posted July 4, 2008 Share Posted July 4, 2008 $items = array($_SESSION['$ItemID']); Should be: <?php $items = array($_SESSION[$ItemID]);?> Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582001 Share on other sites More sharing options...
darkfreaks Posted July 5, 2008 Share Posted July 5, 2008 you do not need array at all. remove it. Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582005 Share on other sites More sharing options...
Becca Posted July 5, 2008 Author Share Posted July 5, 2008 Changing it to that code generates two messages: Notice: Undefined variable: ItemID in C:\xampp\htdocs\dynamicphp\cart.php on line 14 Notice: Undefined index: in C:\xampp\htdocs\dynamicphp\cart.php on line 14 Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582006 Share on other sites More sharing options...
Becca Posted July 5, 2008 Author Share Posted July 5, 2008 you do not need array at all. remove it. If I don't need the array, how would I walk it through the foreach loop? (sorry I'm REALLY new to PHP). Outside of that, even removing that, the error will just move from the line where I'm assigning the array as $items, to where I'm running through the foreach loop, where I'm trying to use $_SESSION['var']. ??? Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582009 Share on other sites More sharing options...
darkfreaks Posted July 5, 2008 Share Posted July 5, 2008 Example: <?php // begin the session session_start(); // loop through the session array with foreach foreach($_SESSION['animals'] as $key=>$value) { // and print out the values echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582034 Share on other sites More sharing options...
Becca Posted July 5, 2008 Author Share Posted July 5, 2008 Thanks, that got rid of the error message for the unidentified variable. So with eliminating the array, I now have this code: foreach($_SESSION['Cartitems'] as $key => $value) { $sql = "SELECT * FROM $tablename WHERE ItemID='$_SESSION[itemID]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query."); echo "<tr><td>$ItemID</td></tr>"; } But the variable doesn't seem to be carrying over - instead, it's outputting this command: if(@sizeof($_SESSION['Cartitems'] = 0)) { echo "There are no items currently in your cart<br /><a href='furniture.php'>Click here to continue shopping</a>"; } Rather than going on to try to echo the items. Is the code below not doing what I'm expecting it to do - which is saving $ItemID in the $_SESSION if the radio item is selected on post? elseif (isset($_POST['tocart']) and $_POST['tocart'] == "yes") { if (isset($_POST['buyid']) and $_POST['buyid'] == "selected") { $_SESSION['Cartitems'] = "$ItemID"; } header("location: cart.php"); exit(); } Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582049 Share on other sites More sharing options...
darkfreaks Posted July 5, 2008 Share Posted July 5, 2008 Syntax is wrong <?php elseif (isset($_POST['tocart']) && $_POST['tocart'] == "yes") { if (isset($_POST['buyid']) && $_POST['buyid'] == "selected") { $_SESSION['Cartitems'] = "$ItemID"; } header("location: cart.php"); exit(); } ?> Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582052 Share on other sites More sharing options...
Becca Posted July 5, 2008 Author Share Posted July 5, 2008 Hmmm...the other PHP pages I've written using and vs && have worked. But, tried the syntax that you provided, and it still seems to be doing the same thing (running as though there are no variables saved to the session that is). ??? This is probably the most challenging stuff I've learned in web coding so far (and, also the most interesting - probably because it's challenging ) - I just hope to get the hang of things soon enough! I appreciate all the help! Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582111 Share on other sites More sharing options...
br0ken Posted July 5, 2008 Share Posted July 5, 2008 I would try writing an echo statement inside the if so that when the $ItemID should be added to the session, a message will be printed informing you this has worked. This way if you see the message then you know it's definately been added and you have a problem with your sessions. <?php elseif (isset($_POST['tocart']) && $_POST['tocart'] == "yes") { if (isset($_POST['buyid']) && $_POST['buyid'] == "selected") { $_SESSION['Cartitems'] = "$ItemID"; echo "Adding $itemID to $_SESSION<br />"; } header("location: cart.php"); exit(); } ?> This will code will cause an error because the header() function cannot be called after you've sent output to the browser but still, just use this for debugging, tell us what happens and then remove. Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582175 Share on other sites More sharing options...
darkfreaks Posted July 5, 2008 Share Posted July 5, 2008 actually you can get around that by using output buffering just put ob_start() at the top of the code Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582239 Share on other sites More sharing options...
compguru910 Posted July 5, 2008 Share Posted July 5, 2008 In order to use sessions "buffered" (which means where they are not 'SUPPOSED' to be used is ob_start (which you put before the <html> tag on every page, and ob_end_flush at the end of your page, after the </html> tag. Sessions are supposed to be used before anything is written to the page, such as any html tags, thats why buffers are useful. So, before the html tag, put ob_start() and after the closing html tag put ob_end_flush(). This will aleviate alot of the problems. I dont know why you are using an array either. How I add items to my cart was. 1st, you create a link on your item that goes to blahblah.php?add_item=1 2nd, you need to create the function that adds the item, and most importantly, takes you off of that page, and gets rid of the add_item completely, or it will add 2 everytime you do it. Took me forever to figure that one out. and that should be it. What are you trying to use the sessions for. Are you using a table to track all of items in their shopping cart, or an array? Mine works off of a table inside of a database that stored information like the id of that entry into the table, the item id number, and the basket id (which is pretty much the id of the shopper) then I just had the cookie/session store the basket id on the shoppers computer so that whenever you needed to view the cart, it just looked up their basket id in the table, and that was all of their items. If you need help, [email protected] Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582250 Share on other sites More sharing options...
Becca Posted July 5, 2008 Author Share Posted July 5, 2008 I'm using an array because that's how my teacher advised me that that would be how best to pass them via $_SESSION, because the lesson is about learning to pass things through $_SESSION or as a cookie (and I understood the cookies section fairly well, so thought it best to tackle and learn what I understood the least to learn it). This is the same reason that I'm not using a database (I had initially planned to do a database for the orders, using an order id to track what the person placed in the cart (and to track the session), but for what this assignment is supposed to be, that's not necessary. I was initially trying to pass things via the URL, but couldn't figure out how to grab them from the URL and then pass them into the page on cart.php. I'm using an array to track the items in their shopping cart and displaying them in a table on cart.php. I tried using ob_start and ob_end_flush at the beginning and end of the script, but this produces the same issue, leading me to believe that I'm somehow testing for the wrong thing... In changing my elseif to this in furniture.php: elseif (isset($_POST['tocart']) and $_POST['tocart'] == "yes") { if (isset($_POST['buyid']) and $_POST['buyid'] == "selected") { $_SESSION['Cartitems'] = array($ItemID); } header("location: cart.php"); exit(); } This should be passing things to the $_SESSION['Cartitems'] array, and it finally seems to be doing so (verified by adding var_dump($_SESSION); to the top of my cart.php script). I then assigned the array to a variable $items and ran a check to see if $items == 0 (that there are no items in the shopping cart) $items = array($_SESSION['Cartitems']); /*If there are no items in the cart, provide a link to go back to the catalog*/ if(@sizeof($items == 0)) { echo "There are no items currently in your cart<br /><a href='furniture.php'>Click here to continue shopping</a>"; } My script seems to be halting here, and running this portion of the script, even when var_dump is telling me that the 'Cartitems' array has an item in it. I figure that this means that the variable being tested in the size of line is NOT the 'Cartitems' array, but I can't seem to figure out how I might word it to test for the appropriate thing, if this is incorrect... ??? Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582360 Share on other sites More sharing options...
Becca Posted July 5, 2008 Author Share Posted July 5, 2008 Hmmm...scratch that, the changes no longer seem to be passing items to the array - array = NULL... ??? Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582370 Share on other sites More sharing options...
darkfreaks Posted July 5, 2008 Share Posted July 5, 2008 also remove the @ it only suppresses errors Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582383 Share on other sites More sharing options...
Becca Posted July 5, 2008 Author Share Posted July 5, 2008 Oh! So that's what @ does (where's the lightbulb over the head emoticon when you need one?) I've once again gotten the selection to pass through the array again (although I'm not sure HOW, since all I did was clean up my furniture.php file by moving the form portion that displays the products and allows you to select them to an include file...). Now my issue is testing the sizeof $items, which is still remaining at 0. I removed the @, but am not receiving any errors, so it would still appear to be the case that the array is not what's being tested by the if(sizeof($items = 0) Hmmm.... You guys are awesome, and thanks for the clarifications you've all given so far! Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582391 Share on other sites More sharing options...
darkfreaks Posted July 5, 2008 Share Posted July 5, 2008 also since sizeof is an alias of count you could do <?php if($items=count(null)){ }?> Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582396 Share on other sites More sharing options...
Becca Posted July 5, 2008 Author Share Posted July 5, 2008 Once again, my code broke in furniture.php and is not passing the array. ??? As soon as I get that fixed, I'll definitely try using count vs. sizeof and see whether that makes any difference... Not sure why, because I stopped editing it when it started working again. I've posted the portion of the code below that I suspect must contain my error... <?php /*If user has logged in, test to see whether cart has been submitted. If so, save items to $_SESSION array and load cart.php */ elseif (isset($_POST['tocart']) and $_POST['tocart'] == "yes") { if (isset($_POST['buyid']) and $_POST['buyid'] == "selected") { $_SESSION['Cartitems'] = array($_POST['buyid']); } header("location: cart.php"); exit(); } /*If user has logged in and has not yet submitted the page, load this*/ else { /*Test to see whether category dropdown was selected*/ if(isset($_POST['cat_select']) and $_POST['cat_select'] == "yes") { include("cat_select.inc"); /*Check the Database for all items within that category*/ $sql = "SELECT * FROM $tablename WHERE Category='$_POST[Category]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query."); /*FORM TO PLACE ORDER*/ include("order_form.inc"); exit(); } And for reference, order_form.inc, which I simply extracted from furniture.php to help clean up the code in hopes of determining the error there: <html> <head> </head> <body> <?php echo "<form action='$_SERVER[php_SELF]' method='POST'>"; echo "<table border='1' cellpadding='5'><tr>"; /*Run a while loop that displays each item found by the inquiry*/ while($row = mysqli_fetch_assoc($result)) { extract($row); echo "<td><img src='$ImgURL'><br />"; echo "$ItemName <br />"; echo "$ItemDesc <br />"; echo "$ $Price <br /><input type='radio' name='buyid' value='$ItemID'"; /*If the item is selected, indicate this when information is sent*/ if ($ItemID == @$_POST['buyid']) { echo "selected='selected'>\n"; } else { echo ">\n"; } echo "Buy Me!</td>"; } echo "</tr></table>"; echo "<div id='submit'><input type='hidden' name='tocart' value='yes'><input type='submit' value='Add to Cart'></div></form>"; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582475 Share on other sites More sharing options...
darkfreaks Posted July 5, 2008 Share Posted July 5, 2008 remove the @ and for future practices use && instead of and. it is the same thing. <?php /*If user has logged in, test to see whether cart has been submitted. If so, save items to $_SESSION array and load cart.php */ elseif (isset($_POST['tocart'])&& $_POST['tocart'] == "yes") { if (isset($_POST['buyid']) || $_POST['buyid'] == "selected"||!empty($_POST['buyid'])) { $_SESSION['Cartitems'] = array($_POST['buyid']); } header("location: cart.php"); exit(); } /*If user has logged in and has not yet submitted the page, load this*/ else { /*Test to see whether category dropdown was selected*/ if(isset($_POST['cat_select']) && $_POST['cat_select'] == "yes") { include("cat_select.inc"); /*Check the Database for all items within that category*/ $sql = "SELECT * FROM $tablename WHERE Category='$_POST[Category]'"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query."); /*FORM TO PLACE ORDER*/ include("order_form.inc"); exit(); } ?> Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582476 Share on other sites More sharing options...
Becca Posted July 5, 2008 Author Share Posted July 5, 2008 Removing the @ from the line if ($ItemID == @$_POST['buyid']) generates this error: Undefined index: buyid in C:\xampp\htdocs\dynamicphp\supportfiles\order_form.inc on line 22 From my class, it was my understanding that you use the @ in the $_POST['var'] Variable in the initial code to suppress the error because you will always get an error the first time the code runs, since the variable doesn't exist until you post - is that incorrect? Can you clarify what this bit of code states? if (isset($_POST['buyid']) || $_POST['buyid'] == "selected"||!empty($_POST['buyid'])) Particularly confused by the || marks and what the !empty($_POST['buyid']) Signify And, out of curiosity, is there a reason that && is preferred to and, since they both do the same thing? My class never discussed &&. Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582480 Share on other sites More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 && is slightly higher priority I think....same thing. Just makes for nicer code usually since everyone understands it and looks for that rather than "and". Link to comment https://forums.phpfreaks.com/topic/113250-solved-basic-shopping-cart-trying-to-pass-information-via-_get-or-_session/#findComment-582481 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.