Ruth Posted March 14, 2008 Share Posted March 14, 2008 I'm attempting a simple shopping cart to try and learn sessions, so far it's been horrible. I have several links that will display a selection of books. If the user clicks on one book it will show the description of that book. They then can click add to chart at this point I want to store the book information aka order into a session, but have been unsuccessful. I find these simple examples of code online that do not help. Can anyone help me figure this out? Here is what is in my session page session_start (); $_SESSION ['cart'] = array(); $_SESSION ['image'] = $image; $_SESSION ['bktitle'] = $bktitle; $_SESSION ['price'] = $price; $_SESSION ['qty'] = $qty; $_SESSION ['totalqty'] = $totalqty; $_SESSION ['totalprice'] = $totalprice; $_SESSION ['total'] = $total; show cart page session_start (); if (isset ($_SESSION['cart'])) { $_SESSION['cart'] ++; } else { $_SESSION['cart'] = 1; } $isbn = $_GET['isbn']; if (!$_GET['cart']) { if (!$isbn) { echo 'You have no books in your cart. Please add a book.'; } } require_once ('dbconnection.php'); if ($isbn or $_GET['cart']) { $query = "SELECT image_path, title, book_price from books where isbn = '$isbn'"; $result = mysqli_query ($db, $query) or die (mysqli_error ($db)); while ($row = mysqli_fetch_array ($result)) { $image = $row ['image_path']; $bktitle = $row ['title']; $price = $row ['book_price']; $count = count ($row['title']); } $qty = 1; $totalqty = $qty + $_POST ['qty']; $totalprice = $price * $qty; $total = $totalprice * $count ; require_once ('session.php'); echo<<<stop <form method = "post" action = "show_cart.php"> <table> <tr bgcolor = "red"> <th colspan = "2" align = "center"><font color = "white">Item</font></th> <th><font color = "white">Price</font></th> <th><font color = "white">Qty</font></th> <th><font color = "white">Total</font></th> </tr> <tr> <tfoot bgcolor = "red"> <td></td> <td></td> <td></td> <td><font color = "white">$totalqty </font></td> <td><font color = "white">$total</font></td> </tfoot> </tr> <tr> <td><img src = "$image"></td> <td valign = "top">$bktitle</td> <td valign = "top">$price</td> <td valign = "top"><input type = "text" value = "$qty" name = "qty" size ="2"></td> <td valign = "top">$totalprice</td> </tr> </table> </form> stop; } Quote Link to comment Share on other sites More sharing options...
p2grace Posted March 14, 2008 Share Posted March 14, 2008 Are the sessions not working at all? If they're not trying something simple first, just create a variable, save it in a session and see if you can echo it. Once you get one to work the rest should be easy. Quote Link to comment Share on other sites More sharing options...
Ruth Posted March 14, 2008 Author Share Posted March 14, 2008 I really don't know if I'm doing this right. I turned on error_reporting(E_ALL); and I get Undefined index: cart, Undefined index: qty, and A session had already been started - ignoring session_start() . However I also did print_r($_SESSION); and I get Array ( [cart] => Array ( ) [image] => /images/java_book.gif [bktitle] => Java 2 for professional Developers [price] => 34.99 [qty] => 1 [totalqty] => 1 [totalprice] => 34.99 [total] => 34.99 ). I'm not real sure if the session is being set at all. Quote Link to comment Share on other sites More sharing options...
p2grace Posted March 14, 2008 Share Posted March 14, 2008 Looks like the session is set and working. Try this to make sure: <?php $test = "test"; $_SESSION['test'] = $test; echo $_SESSION['test']; ?> If it echoes test then sessions are working, then we can troubleshoot from there. Quote Link to comment Share on other sites More sharing options...
Ruth Posted March 14, 2008 Author Share Posted March 14, 2008 I show on the screen Array ( [cart] => Array ( ) [image] => /images/java_book.gif [bktitle] => Java 2 for professional Developers [price] => 34.99 [qty] => 1 [totalqty] => 1 [totalprice] => 34.99 [total] => 34.99 [test] => test ) Quote Link to comment Share on other sites More sharing options...
p2grace Posted March 14, 2008 Share Posted March 14, 2008 This logic doesn't make sense: $_SESSION['cart'] = array(); $_SESSION['cart'] ++; // what are you trying to increase? Quote Link to comment Share on other sites More sharing options...
Ruth Posted March 14, 2008 Author Share Posted March 14, 2008 If they order one book and decided to order more I'm trying to store that 2end order along with the first in the session. So I'm trying to get it to increment. If they don't have any order then set their selection as a session and keep adding to it as they order. Does that make sense? Quote Link to comment Share on other sites More sharing options...
p2grace Posted March 14, 2008 Share Posted March 14, 2008 To increment do this when saving new data: <?php $_SESSION['cart'] = array(); $_SESSION['cart'] [] = "item 1"; $_SESSION['cart'] [] = "item 2"; ?> Quote Link to comment Share on other sites More sharing options...
Ruth Posted March 14, 2008 Author Share Posted March 14, 2008 I'm using variables so can I do $_Session ['cart'] = arrray () $_Session['cart'] [] = $title Since I'm using require_once do I need to pass the variables to the page I'm requiring? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 14, 2008 Share Posted March 14, 2008 "Since I'm using require_once do I need to pass the variables to the page I'm requiring?" no. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.