Agreaves Posted August 30, 2012 Author Share Posted August 30, 2012 The url for this page is localhost/cart/index.php. Let me post the whole code cuz maybe im doing something wrong further in the script. This is the whole script for the entire shopping cart. $product_id = array_key_exists('id', $_GET) ? $_GET['id']: NULL; $action = array_key_exists('action',$_GET) ? $_GET['action']: "empty"; ?> <?php //if there is an product_id and that product_id doesn't exist display an error message if($product_id && !productExists($product_id)) { die("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 it will show zero, then -1, -2 etc when the user keeps removing items. break; case "empty": default: unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; } ?> <?php if(isset($_SESSION['cart'])) { //if the cart isn't empty //show the cart echo "<form method=\"post\" name=\"body\" action=\"$_SERVER[php_SELF]\">"; echo "<table align=\"center\" id=\"tbl_bdr\" width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\"> <tr align=\"left\"> <td align=\"left\"><b>Name:</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"name\"/></td> <td align=\"left\"><b>Email:</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"email\"/></td> </tr> <tr align=\"left\"> <td align=\"left\"><b>Address:</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"address1\"/></td> <td align=\"left\"><b>Address(ext):</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"address2\"/></td> </tr> <tr align=\"left\"> <td align=\"left\"><b>Telephone:</b></td> <td><input id=\"txt_box\" type=\"text\" name=\"telephone\"/></td> </tr> <tr><td height=\"10\"></td></tr> </table>"; echo "<table id =\"cart_bdr\" align =\"center\" border=\"0\" padding=\"15\" width=\"950\">"; //format the cart using a HTML table echo "<tr id=\"hdg_bkgrnd\" height=\"40\"><td width=\"237\" align=\"center\"><b>Thumbnail</b></td><td width=\"237\" align=\"center\"><b>Id</b></td><td width=\"237\" align=\"center\"><b>Name</b></td><td width=\"237\" align=\"center\"><b>Quantity</b></td><td width=\"237\" align=\"center\"><b>Amount</b></td></tr>"; $total = 0; //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, price and pix 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 Id, Name, Price, Pix FROM products WHERE Id = %d;", $product_id); $result = mysqli_query($con,$sql) or die("Couldnt run query"); $row = mysqli_num_rows($result); // Smarty variables application // Initialize the array for the results $record = Array(); $sql2 = sprintf("SELECT Id, Name, Price, Pix FROM products WHERE Id = %d;", $product_id); $result2 = mysqli_query($con,$sql2) or die("Couldnt run query"); $rows = mysqli_fetch_assoc($result2); while ($rows = mysqli_fetch_assoc($result2)) { $record[] = $rows; } // Assign Smarty variables $smarty->assign('record',$record); //Only display the row if there is a product (though there should always be as we have already checked) if($row > 0) { list($Id, $Name, $Price, $Pix) = mysqli_fetch_row($result); $line_cost = $Price * $quantity; //work out the line cost $total += $line_cost; echo "<tr height=\"55\">"; echo "<td align=\"center\"><img id=\"thumb_bdr\" src=\"$Pix\" width=\"50\" height=\"50\"/></td>"; echo "<td align=\"center\" id=\"cols_bkgrnd\"><div name=\"id\">$Id</div></td>"; //show this information in table cells echo "<td align=\"center\" id=\"cols_bkgrnd\">$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\" id=\"cols_bkgrnd\">$quantity <a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">X</a></td>"; echo "<td align=\"right\" id=\"cols_bkgrnd\">$$line_cost</td>"; echo "</tr>"; } } //show the total echo "<tr id=\"hdg_bkgrnd\" height=\"40\">"; echo "<td></td>"; echo "<td></td>"; echo "<td></td>"; echo "<td align='right'><div id=\"total\"><b>Total</b></div></td>"; echo "<td align='right'>$$total</td>"; echo "</tr>"; echo "</table>"; //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 "<table width=\"950\" align=\"center\">"; echo "<tr>"; echo "<td width=\"237\"></td>"; echo "<td width=\"237\"></td>"; echo "<td width=\"237\"></td>"; echo "<td width=\"237\" align=\"right\"><input id=\"submit_btn\" value=\"Send\" type=\"submit\"/></td>"; echo "<td width=\"237\"><input id=\"cart_btn\" type=\"button\" value=\"Empty\" onclick=\"window.location.href='$_SERVER[php_SELF]?action=empty'\" /></td>"; echo "</tr>"; echo "</table>"; echo "</form>"; } else { //otherwise tell the user they have no items in their cart echo "You have no items in your shopping cart."; echo $message; } //function to check if a product exists function productExists($product_id) { global $product_id; global $con; global $host; global $user; global $pass; global $dbase; //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT * FROM products WHERE id = %d;", $product_id); $con = mysqli_connect($host,$user,$pass,$dbase) or die("couldnt connect to server"); $result = mysqli_query($con,$sql) or die ('Could not run query'); $row = mysqli_num_rows($result); return $row > 0; } Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 If the URL doesn't have any parameters, your $_GET array will be empty. I misunderstood the purpose of your "empty" string, change the last case to case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; default: break; Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 30, 2012 Author Share Posted August 30, 2012 It still does the same thing when I add a product and leave and return to the cart. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 30, 2012 Author Share Posted August 30, 2012 When I add products to the cart I get the following messages. Notice: Undefined index: cart in C:\xampp\htdocs\cart\index.php on line 269 Notice: Undefined offset: 6966 in C:\xampp\htdocs\cart\index.php on line 269 Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 30, 2012 Share Posted August 30, 2012 You answered half of my questions. Do you understand what your script is attempting to do.What's on those lines? What is each variable at each stage? Start doing some debugging. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 30, 2012 Author Share Posted August 30, 2012 I guess im reading the lines incorrectly, ill do some debugging to see whats going on. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 30, 2012 Share Posted August 30, 2012 took this from the PHP manual: Modification of the array during listexecution (e.g. using list($a, $b) = $b) results in undefined behavior. change: list($Id, $Name, $Price, $Pix) = mysqli_fetch_row($result); to: while(list($Id, $Name, $Price, $Pix) = mysqli_fetch_row($result)) { //code here } Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 1, 2012 Share Posted September 1, 2012 in my opinion i think using nested forech loops would possibly be better than using list. as it only does numerical arrays and i doubt pic or name is numerical. foreach($name,$pic as $key => $value) { foreach($id,$price as $key => $value) { } } Quote Link to comment Share on other sites More sharing options...
Agreaves Posted September 1, 2012 Author Share Posted September 1, 2012 I removed the the list function and stuck with the one foreach loop and everthing worked fine. My problem now is getting the smarty template done for email testing. Do you know how to get a testing email server? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 1, 2012 Share Posted September 1, 2012 @ OP: do you not have your own server or hosting also here are some links to help you get Smarty PHP configured and setup..... Install and configure smarty php also you can sign up for free hosting at 000webhost.com Quote Link to comment Share on other sites More sharing options...
Agreaves Posted September 1, 2012 Author Share Posted September 1, 2012 I do, but I havent uploaded any files as yet. im doing everything from the testing server. 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.