cmgmyr Posted January 20, 2007 Share Posted January 20, 2007 Hey Everyone,So I'm making a session shopping cart for one of my clients and I got it pretty much done but I ran into a few problems. If you go to http://kawellphotography.com/prints_main_categories.htm select a portfolio, then a picture and click "add to cart" you will see that this page gets added to the cart. Now, go back to that first page and add 2 (or more) different photos. So when you go back to the cart you should have 3 or more line items (all data is now correct). Now without changing anything in the shopping cart form click "Update Cart". You will now notice that I outputted a few lines to track the data. You can now see the values from the form, values of the session data before the update, and the values after the update. Notice that items 2 and 3 do NOT have the img_size variable in the session before the update, but then after the update the values are put in. BUT when you get down to the form area (where I printed out more information) you can see that just the second item doesn't have the img_size variable. Next, change the last item size to the opposite choice in the drop down and select "Update Cart" again. Doing this changes BOTH the 1st and the 3rd item, the 2nd item still doesn't have a size value. Lastly...go back to the portfolio choices and add another image to the cart (a totally new item). When you add it you will get an error of: Fatal error: [] operator not supported for strings in /var/www/html/cart.php on line 35. Line 35 is: $_SESSION["img_size"][] = 1; SO...I know it's probably simple (or maybe not) but I haven't worked with sessions much and I have no idea where to go from here. So I would greatly appriciate if I could get this solved as soon as possible. Please do not ask why I can't do this another way (like with a database) the customer wanted this done very cleanly without a database of any kind.I have included the full cart.php below. Thank you for your time,-Chris[code=php:0]<?php session_start();include "header.txt";switch($action){ case 'add': if (!isset($_SESSION["items"])) { $_SESSION["items"] = array(); $_SESSION["qty"] = array(); $_SESSION["img_size"] = array(); } $page = $_SERVER['HTTP_REFERER']; $page = str_replace('.htm', '', $page); $page = str_replace('www.', '', $page); $page = str_replace('http://', '', $page); //$page = str_replace('kawellphotography.com/', '', $page); $page = str_replace('localhost:8080/www/htdocs/kawell/', '', $page); $page = str_replace('prints_category_', '', $page); $page = str_replace('_', ' ', $page); $i=0; while ($i<count($_SESSION["items"]) && $_SESSION["items"][$i] != $page) $i++; if ($i < count($_SESSION["items"])) //increase current product's item quantity { $_SESSION["qty"][$i]++; } else //no such product in the cart - add it { $_SESSION["items"][] = $page; $_SESSION["qty"][] = 1; $_SESSION["img_size"][] = 1; } echo show_cart(); break; case 'remove': $i = $_GET['remove']; unset($_SESSION["items"][$i]); unset($_SESSION["qty"][$i]); unset($_SESSION["img_size"][$i]); echo show_cart(); break; case 'process': //see what the form wants to do $button = $_POST['button']; if($button == "Update Cart"){ //loop through items $count_items = count($_SESSION["items"]); echo "<br />This does the update.<br />"; $x = 0; while ($x < $count_items): $count = $_POST['count'.$x]; $img_size = $_POST['img_size'.$x]; echo "Info from form: Ses#: $x - img_size: $img_size - Count: $count<br />"; echo "Info from session: Ses#: $x - img_size: ".$_SESSION["img_size"][$x]." - Count: ".$_SESSION["qty"][$x]."<br />"; $_SESSION["qty"][$x] = $count; $_SESSION["img_size"][$x] = $img_size; echo "Info in session after update: Ses#: $x - img_size: ".$_SESSION["img_size"][$x]." - Count: ".$_SESSION["qty"][$x]."<br /><br />"; $x++; endwhile; echo show_cart(); }elseif($button == "Clear Cart"){ unset($_SESSION["items"]); unset($_SESSION["qty"]); unset($_SESSION["img_size"]); session_destroy(); echo show_cart(); }else{ //Complete checkout and clear $email = $_POST['email']; $youremail="XXXXXXXXXXX"; // This is the address that the information submitted will be sent to. $emailsubject="Order from KaWell Photography"; // This will the email's subject $from_who="$email"; $name = $_POST['name']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $phone = $_POST['phone']; if ($email>"" && $name>"" && $phone>"") { $mailbody="Name:\n=================\n$name\n\n"; $mailbody.="Email:\n=================\n$email\n\n"; $mailbody.="Phone #:\n=================\n$phone\n\n"; $mailbody.="Address:\n=================\n$address\n$city, $state $zip\n\n"; $count_items = count($_SESSION["items"]); $x = 0; $grand_total = 0; $mailbody.= "Item - Size - Qty - Total\n"; while ($x < $count_items): $mailbody.= $_SESSION["items"][$x]." - "; if($_SESSION["img_size"][$x] == 1){ $mailbody.= "16x20";}else{$mailbody.= "24x30";} //find total dollars if($_SESSION["img_size"][$x] == 1){ $total = $_SESSION["qty"][$x] * 700; $grand_total += $total; }else{ $total = $_SESSION["qty"][$x] * 900; $grand_total += $total; } $mailbody.= " - ".$_SESSION["qty"][$x]." - $$total\n"; $x++; endwhile; $mailbody.= "\n\nGrand Total - $$grand_total\n"; $pagebody="Name:\n<br />=================\n<br />$name\n\n<br /><br />"; $pagebody.="Email:\n<br />=================\n<br />$email\n\n<br /><br />"; $pagebody.="Phone #:\n<br />=================\n<br />$phone\n\n<br /><br />"; $pagebody.="Address:\n<br />=================\n<br />$address\n<br />$city, $state $zip\n\n<br /><br />"; $count_items = count($_SESSION["items"]); $x = 0; $grand_total = 0; $pagebody.= "<table>\n"; $pagebody.= "<tr bgcolor=\"#CCCCCC\"><td>Item</td><td>Size</td><td>Qty</td><td>Total</td></tr>\n"; while ($x < $count_items): $pagebody.= "<tr style=\"color:#FFFFFF;\">\n"; $pagebody.= "<td>".$_SESSION["items"][$x]."</td><td>"; if($_SESSION["img_size"][$x] == 1){ $pagebody.= "16x20";}else{$pagebody.= "24x30";} //find total dollars if($_SESSION["img_size"][$x] == 1){ $total = $_SESSION["qty"][$x] * 700; $grand_total += $total; }else{ $total = $_SESSION["qty"][$x] * 900; $grand_total += $total; } $pagebody.= "</td><td>".$_SESSION["qty"][$x]."</td><td>$$total</td>\n"; $pagebody.= "</tr>\n"; $x++; endwhile; $pagebody.= "<tr bgcolor=\"#CCCCCC\"><td></td><td></td><td>Grand Total</td><td>$$grand_total</td></tr>\n"; $pagebody.= "</table>"; mail("$youremail", "$emailsubject", "$mailbody", "From: $from_who"); // Send the email. echo "<br /><br />Please print the following form for you records.<br /><br />"; echo $pagebody; //clear cart unset($_SESSION["items"]); unset($_SESSION["qty"]); unset($_SESSION["img_size"]); session_destroy(); }else{ echo "<br /><br /><div style=\"color:red;\">You have not filled something in correctly.</div><br /><br />"; echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=cart.php\">"; } } break; default: echo show_cart(); break;}function show_cart(){ $count_items = count($_SESSION["items"]); if($count_items == 0){ echo "<br /><br />Your cart is empty.<br /><br />Please add something."; }else{ $x = 0; $grand_total = 0; echo "<br />Show cart:<br />"; echo "<form method=\"post\" action=\"cart.php\"><table>\n"; echo "<tr bgcolor=\"#CCCCCC\"><td>Item</td><td>Size</td><td>Qty</td><td>Total</td><td>Delete</td></tr>\n"; while ($x < $count_items): echo "<tr style=\"color:#FFFFFF;\">\n"; echo "Information to use in form: Ses#: $x - img_size: ".$_SESSION["img_size"][$x]." - Count: ".$_SESSION["qty"][$x]."<br />\n"; echo "<td>".$_SESSION["items"][$x]."</td><td>\n<select name=\"img_size$x\">\n"; if($_SESSION["img_size"][$x] == 1){ echo "<option value=\"1\">16x20</option>\n<option value=\"2\">24x30</option>\n";}else{echo "<option value=\"2\">24x30</option>\n<option value=\"1\">16x20</option>\n";} //find total dollars if($_SESSION["img_size"][$x] == 1){ $total = $_SESSION["qty"][$x] * 700; $grand_total += $total; }else{ $total = $_SESSION["qty"][$x] * 900; $grand_total += $total; } echo "</select>\n<br />SES[\"img_size\"][$x] = ".$_SESSION["img_size"][$x]."</td><td><input type=\"text\" name=\"count$x\" size=\"5\" value=\"".$_SESSION["qty"][$x]."\"></td><td>$$total</td><td><a href=\"cart.php?action=remove&remove=$x\"><img src=\"remove.jpg\" border=0 alt=\"Delete\"></a></td>\n"; echo "</tr>\n"; $x++; endwhile; echo "<tr bgcolor=\"#CCCCCC\"><td></td><td></td><td>Grand Total</td><td>$$grand_total</td><td></td></tr>\n"; echo "<tr> <td colspan=\"4\"> <input type=\"submit\" name=\"button\" value=\"Update Cart\"> <input type=\"submit\" name=\"button\" value=\"Clear Cart\"> </td> </tr> <tr> <td colspan=\"4\"> <table border=\"0\" cellspacing=\"2\" cellpadding=\"4\" style=\"color:#FFFFFF;\"> <tr> <td colspan=2><br /><br />To complete your order please fill out the following and click \"Complete Checkout\"</td> </tr> <tr> <td style=\"text-align:right;\">Name: </td> <td style=\"text-align:left;\"><input type=\"text\" name=\"name\" size=\"20\"> *</td> </tr> <tr> <td style=\"text-align:right;\">Phone #: </td> <td style=\"text-align:left;\"><input type=\"text\" name=\"phone\" size=\"20\"> *</td> </tr> <tr> <td style=\"text-align:right;\">E-Mail: </td> <td style=\"text-align:left;\"><input type=\"text\" name=\"email\" size=\"20\"> *</td> </tr> <tr> <td style=\"text-align:right;\">Address: </td> <td style=\"text-align:left;\"><input type=\"text\" name=\"address\" size=\"20\"></td> </tr> <tr> <td style=\"text-align:right;\">City: </td> <td style=\"text-align:left;\"><input type=\"text\" name=\"city\" size=\"20\"></td> </tr> <tr> <td style=\"text-align:right;\">State: </td> <td style=\"text-align:left;\"><input type=\"text\" name=\"state\" size=\"20\"></td> </tr> <tr> <td style=\"text-align:right;\">Zip: </td> <td style=\"text-align:left;\"><input type=\"text\" name=\"zip\" size=\"20\"></td> </tr> </table> </td> </tr> <td colspan=\"4\"> <input type=\"submit\" value=\"Complete Checkout\"> <input type=\"hidden\" name=\"action\" value=\"process\"> </td> </tr> </table> </form><br /><br />"; }}include "footer.txt";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34962-session-messing-up-data/ Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 tldr;Why are you using multi-dimensional arrays there? Quote Link to comment https://forums.phpfreaks.com/topic/34962-session-messing-up-data/#findComment-164872 Share on other sites More sharing options...
cmgmyr Posted January 20, 2007 Author Share Posted January 20, 2007 What other things could I use? Quote Link to comment https://forums.phpfreaks.com/topic/34962-session-messing-up-data/#findComment-164875 Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 A single array...It seems like a weird approach to it. But as I said - too long, didn't read. Quote Link to comment https://forums.phpfreaks.com/topic/34962-session-messing-up-data/#findComment-164877 Share on other sites More sharing options...
cmgmyr Posted January 20, 2007 Author Share Posted January 20, 2007 So how would I set that up if I want 3 variables in the same "layer" of the array Quote Link to comment https://forums.phpfreaks.com/topic/34962-session-messing-up-data/#findComment-164885 Share on other sites More sharing options...
trq Posted January 20, 2007 Share Posted January 20, 2007 Placing URGENT in your subject will get you nowhere fast. Quote Link to comment https://forums.phpfreaks.com/topic/34962-session-messing-up-data/#findComment-164889 Share on other sites More sharing options...
Jessica Posted January 20, 2007 Share Posted January 20, 2007 That's the part that is a weird approach to it. You should think more object oriented.Have an array of items.Each item is a smaller array of those three vars you want.This is still a multi-dimensional array, but it's more logical to have items with properties than large lists of properties, which as far as I can tell from your code is how it's written.So every time they add an item to the cart, you do$items = $_SESSION['items'];$item = array('size'=>'whatever', 'quantity'=>1); //(etc)$items[] = $item;$_SESSION['items'] = $items;Maybe this is similar to what you're doing, but you posted too much code to read.You should look into OO-PHP, it would help with this sort of app.On Preview: Thorpe is right. Read the article in mine and his signatures. Quote Link to comment https://forums.phpfreaks.com/topic/34962-session-messing-up-data/#findComment-164890 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.