Alexhoward Posted September 28, 2008 Share Posted September 28, 2008 Hi guys, Really stuck here... Found some shopping basket code, and i'm assuming it's in PHP4 or something. Could someone please point me in the right direction? i'm using some code found here: http://whoyouknow.co.uk/php/shop/ however i'm getting the error in the title at this point : switch($action) { case "add": $_SESSION['cart'][$product_id]++; break; Thanks in Advance! Quote Link to comment Share on other sites More sharing options...
trq Posted September 28, 2008 Share Posted September 28, 2008 What error are you actually getting? Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted September 28, 2008 Author Share Posted September 28, 2008 Thanks Thorpe, I'm getting the error in the title, it's on the "$_SESSION['cart'][$product_id]++;" line Quote Link to comment Share on other sites More sharing options...
trq Posted September 28, 2008 Share Posted September 28, 2008 You need to make sure that all the classes stored within the session array (ie; your cart object) are included prior to your call to session start. Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted September 28, 2008 Author Share Posted September 28, 2008 Hi, thanks for the reply, but i don't really follow... this is used in a shopping basket script, and therefore when the basket is empty surely nothing will be included...? also couldn't there be any number of classes...? cheers Quote Link to comment Share on other sites More sharing options...
trq Posted September 28, 2008 Share Posted September 28, 2008 The cart object is stored within the $_SESSION array. You need to make sure the class definition for the cart object is in scope (included) prior to calling session_start(). eg; <?php include 'cart.class.php'; session_start(); // its now safe to use $_SESSION and the cart object it may contain. ?> Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted September 28, 2008 Author Share Posted September 28, 2008 hi, hold on.. it says something about hash tables, should i have a seperate file to save all this? i though the session would save it? sorry, but i'm now getting more errors...? here's the whole lot... i sure i'm just being a numpty. <?php include 'cart.class.php'; session_start(); include("config.php"); //connect to the mysql server $link = mysql_connect($host, $db, $pass) or die ("Could not connect to mysql because ".mysql_error()); //select the database mysql_select_db($db) or die ("Could not select database because ".mysql_error()); $product_id = $_GET['id']; //the product id from the URL $action = $_GET['action']; //the action from the URL //function to check if a product exists function productExists($product_id) { //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT id FROM products "); return mysql_num_rows(mysql_query($sql)) > 0; } //if there is an product_id and that product_id doesn't exist display an error message if($product_id && !productExists($product_id)) { die("Error. Product Doesn't Exist"); } switch($action) { //decide what to do case "add": $_SESSION['cart'][$product_id] ++; //add one to the quantity of the product with id $product_id break; case "remove": $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. break; case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; } ?> <?php if($_SESSION['cart']) { //if the cart isn't empty //show the cart echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table //iterate through the cart, the $product_id is the key and $quantity is the value foreach($_SESSION['cart'] as $product_id => $quantity) { //get the name, description and price from the database - this will depend on your database implementation. //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT name, description, price FROM php_shop_products WHERE id = %d;", $product_id); $result = mysql_query($sql); //Only display the row if there is a product (though there should always be as we have already checked) if(mysql_num_rows($result) > 0) { list($name, $description, $price) = mysql_fetch_row($result); $line_cost = $price * $quantity; //work out the line cost $total = $total + $line_cost; //add to the total cost echo "<tr>"; //show this information in table cells echo "<td align=\"center\">$name</td>"; //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product echo "<td align=\"center\">$quantity <a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">X</a></td>"; echo "<td align=\"center\">$line_cost</td>"; echo "</tr>"; } } //show the total echo "<tr>"; echo "<td colspan=\"2\" align=\"right\">Total</td>"; echo "<td align=\"right\">$total</td>"; echo "</tr>"; //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation echo "<tr>"; echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[php_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>"; echo "</tr>"; echo "</table>"; }else{ //otherwise tell the user they have no items in their cart echo "You have no items in your shopping cart."; } ?> i'm now getting the errors: Warning: include(cart.class.php) [function.include]: failed to open stream: No such file or directory in E:\******\basket.php on line 3 Warning: include() [function.include]: Failed opening 'cart.class.php' for inclusion (include_path='.;C:\php5\pear') in E:\******\basket.php on line 3 Fatal error: Cannot use object of type __PHP_Incomplete_Class as array in E:\******\basket.php on line 37 Thanks for all the help Quote Link to comment Share on other sites More sharing options...
trq Posted September 28, 2008 Share Posted September 28, 2008 is there actually a file called cart.class.php? I was just giving an example. Quote Link to comment Share on other sites More sharing options...
Alexhoward Posted September 28, 2008 Author Share Posted September 28, 2008 haha, oh right... no there isn't... 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.