Jump to content

swissbeets

Members
  • Posts

    57
  • Joined

  • Last visited

    Never

Everything posted by swissbeets

  1. i just changed my form to have the values be hidden, rather than in my URL this is the form while($row = mysql_fetch_array($product_set)) { ?> <form method="POST" action="shoppingcart.php"> <input type="hidden" name="prod" value="<?php echo $row['product_id']; ?>" /> <input type="hidden" name="action" value="add" /> <p> <?php echo $row['product_name']; ?><br /> $<?php echo $row['product_price']; ?><br /><br /> <?php show_picture($row); ?><br /> Size: <p> <select name="size"> <?php $size = array('na'=>'Select','small'=>'S','medium'=>'M','large'=>'L','xlarge'=>'XL'); showOptionsDrop($size, $active); ?> </select> </p> <input type="submit" value="Add to Shopping Cart" /> </p> </form> <?php } these are then sent to my shopping cart but this is where the problem is, not only is the size not being saved in the function, the session is not allowing me to change anything, i cannot unset it to empty the cart or anything and anytime i add a new item the whole size says which ever size i chose here is my code $product_id = $_POST[prod]; //the product id from the URL $action = $_POST[action]; //the action from the URL $size = $_POST['size']; echo $product_id; echo $action; echo $size; //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": if ($size!="na"){ $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id //echo "success"; } else { echo "<br/>"; echo "you did not choose a size for this item"; ?> <form method="POST" action="shoppingcart.php"> <input type="hidden" name="prod" value="<?php echo $row['product_id']; ?>" /> <input type="hidden" name="action" value="add" /> <p> <select name="size"> <?php $size = array('na'=>'Select','small'=>'S','medium'=>'M','large'=>'L','xlarge'=>'XL'); showOptionsDrop($size, $active); ?> </select> <input type="submit" value="Add to Shopping Cart" /> </p> <?php } 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; } ?> 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 product_name, product_price FROM products WHERE product_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, $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&prod=$product_id\">Remove</a></td>"; echo "<td align=\"center\">$line_cost</td>"; echo "<td align=\"center\">$size</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."; } please i feel like it is a only a few variables i am missing but any help is greatly appreciated
  2. ok i understand what your saying with this and did so, but it is only displaying the top product and and not displaying a submit button these variables will be POSTed and then i can easily GET them at a later date is this correct?
  3. does this go inside my first while loop? sorry i am very new to this ? this is my current while loop while($row = mysql_fetch_array($product_set)){ echo "<p>"; echo $row['product_name']."<br />"; echo "$".$row['product_price']."<br />"."<br />"; show_picture($row); echo "<br/>"; echo "Size: "; ?><p> <select name="size"> <?php $size = array('Select','S','M','L','XL'); showOptionsDrop($size); ?> </select> </p> <?php echo "<a href=\"shoppingcart.php?prod=".urlencode($row['product_id'])."?&size=['$size']"."?&action=add\">Add to Shopping Cart"."</a>"; } your saying i should make it a form with hidden values rather than even use a session?
  4. kind of i do not know how to use a hidden field but can find out but as for the rest, product id, and action are already being passed to the next page, i have up to this far working but then realized i needed to put sizes in there any chance you could clarify please i am still very new but want to learn
  5. I am trying to save the shirt size and then bring it back up in the shopping cart page, but i cannot get it to come up in the URL this is the function that displays the drop down menu, the array is ('S','M','L','XL') i cannot figure out what variable to use or how to use it function showOptionsDrop($array, $active, $echo=true){ $string = ''; foreach($array as $k => $v){ $s = ($active == $k)? ' selected="selected"' : ''; $string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n"; } if($echo) echo $string; else return $string; } this is my hyperlink to save this information cho "<a href=\"shoppingcart.php?prod=".urlencode($row['product_id'])."?&size=$string[$k]"."?&action=add\">Add to Shopping Cart"."</a>"; i will then make a GET and display the size in the shopping cart but my URL will not work do i have to do something witth the session? any help is greatly appreciated
  6. do i have to have this on every single form that is being used? what must be called each time?
  7. i have gone through this tutorial http://whoyouknow.co.uk/php/shop/ and put everything in its expected places but 1: the product id is not being shown, it will constantly only bring up the first product of my database. (name and price) 2: when i go back to the products.php and click another product, nothing is updated. i have little experience with sessions but have been reading a lot about them still with no prevail this is the products.php <?php if (!isset($_SESSION['cart'])) { session_start(); }; require_once("includes/connection.php"); require_once("includes/functions.php"); while($row = mysql_fetch_array($product_set)){ echo "<p>"; echo $row['product_name']."<br />"; echo "$".$row['product_price']."<br />"."<br />"; show_picture($row); echo "<br/>"; ?> <form action="shoppingcart.php?prod=<?php echo urlencode($row['product_id']);?>&action=add" method="post"> <input type="submit" name="submit" value="Add to Shopping Cart" /><?php echo "</p>"; } ?>// the values are being transferred in the URL to my shoppingcart.php this is my shopping cart <?php require_once("includes/connection.php"); ?> <?php require_once("includes/functions.php"); ?> <?php include("includes/header.php"); ?> <?php $product_id = $_GET[prod]; //the product id from the URL $action = $_GET[action]; //the action from the URL //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 echo "success"; 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 product_name, product_price FROM products WHERE product_id = $product_id;", $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, $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&prod=$product_id\">Remove</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."; } ?> alot of information but any help would be greatly appreciated since i have been trying to find the problem for hours
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.