Jump to content

GD

New Members
  • Posts

    7
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

GD's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi there, Sorry, I'm just having difficulty applying it. The problem is I have to use the database table as is without adding any columns to it and it only contains a column for name, stock and price. I have to also populate the shopping cart from two separate product pages each with a few products on it which are posted to the cartaction page via a submit button. Because the code is using intval I will always get an invalid item as my column is a varchar product name rather than an integer. That's why I'm having trouble getting my head around how to get each product to appear as a new variable array on the shopping cart page without replacing the item that's in there.
  2. Hi all - hoping somebody can see this clearer than me... Goal: to create a very simple shopping cart page that receives $_POST info and stores it as an array in a session variable so items will be displayed in cart on clients return to shopping cart page. Problem: I can get the array to display the first item that I add to the shopping cart but each susequest item just overwrites the first because it is using the same session variable name. How do I store the next product info in a DIFFERENT session variable and display it on the shopping cart page without writing over the first? I have two pages with products on them which need to submit orders to the shopping cart page. They use a simple form with these fields: <form method="post" action="cartaction.php"> <p> <input type="submit" name="submit" value="Buy" /> <input type="hidden" name="cartaction" value="add" /> <input type="hidden" name="item" value="coppercasserole" /> </p> </form> This is what I have so far on the cartaction page which can display the first product added to the cart: <?php //If form is submitted, call the function and save all data into the array $_SESSION['form'] if($_POST['submit'] == "Buy"){ setSessionVars(); } function setSessionVars() { if ( !isset($_SESSION['cart']) ) $_SESSION['cart'] = array(); $item = array(); foreach($_POST as $fieldname => $fieldvalue) { $item[$fieldname] = $fieldvalue; } $_SESSION['cart'] = $item; echo "<table> <tr> <td>" .'<img src="images/'.$item['item'].'.jpg"' . "<td/> <td>" . $item['item'] . "</td> <td>" . '<input type="text(30)" name="value" value="1" /> <input type="submit" name="puchasednum" value="Update This One Item" />' . "</td> </tr> </table>"; } ?> I am new to PHP and have hit a brick wall as to how to display each product array as a new session variable in the shopping cart. I would like to keep it as simple as possible as I am getting easily confused! Is there a way that I can increment the session variable with something like this: <?php //session counter $counter = count($_SESSION['cart']); if (($_POST['name'] !="")){ $counter = $counter + 1; $_SESSION['cart'][$counter] = array(); } ?> Or can I pass a field as a product ID and use this somehow to create a new session variable for each product or clear the post variables somehow? Any help would be much appreciated? Thanks
  3. Thank's very much - that's a massive help. The application I'm building specifies that there has to be a copper range page and a steel range page but there will only be one table for all items for all items as they will only be pans on sale. Your expertise has been a great help and I will work through your suggestions. Thanks again Graham
  4. Thanks everyone - your great help is very much appreciated! The table showing a single item purchased is showing up nicely but I still have the problem that I need to be able to go back and purchase further items and have them appear in the shopping cart underneath the item(s) already in there. At the moment when I go back and try to add another item to the cart it just replaces the item that was in there rather than adding an additional table underneath. Any ideas on how I can retain the original session variable on the shopping cart page and add another instance of them for a new product underneath? Many thanks
  5. Hi again, I have modified the code as below but not sure if I have implemented properly as I am still having the same problem. Probably looking at the problem too long has turned my brain to mush! <?php $submit = $_POST["submit"]; //If form is submitted, call the function and save all data into the array $_SESSION['form'] if($submit = "Buy"){setSessionVars();} function setSessionVars() { $_SESSION['cart'] = array(); $item = array(); foreach($_POST as $fieldname => $fieldvalue) { $item[$fieldname] = $fieldvalue; } $_SESSION['cart'][] = $item; echo "<table> <tr> <td>" .'<img src="images/'.$item['item'].'.jpg"' . "<td/> <td>" . $item['item'] . "</td> <td>" . '<input type="text(30)" name="value" value="1" /> <input type="submit" name="puchasednum" value="Update This One Item" />' . "</td> </tr> </table>"; } ?> Thank you for your help its much appreciated.
  6. Thanks very much for the reply - I'll give it a bash!
  7. Hi everyone, I am very new to PHP and trying to learn so please bear with me. I have a simple form which passes fields to a PHP script: <form method="post" action="cartaction.php"> <p> <input type="submit" name="submit" value="Buy" /> <input type="hidden" name="cartaction" value="add" /> <input type="hidden" name="item" value="steelcasserole" /> </p> </form> I am trying to produce a shopping cart type page which will display the item(s) purchased. The problem is I have more than one submit button on the page for different products but with the same fields. I have tried to create the cartaction.php script to save the information from the form in a session variable which will allow me to output the item(s) purchased into this page prior to sending everything to a checkout page. I can get the information to appear for a single item but the problem is I want to output the details for each product added to the cart. At the moment, when I go back to the products page and try to add a new item to the shopping cart it just replaces the previous item that was there rather than retaining it and adding a new item underneath. I realise its probably really obvious but I am really new to this and getting myself confused! Any help on how to add an item then be able to go back to the products page (with the submit buttons for each product) and add a new item underneath the existing item in the shopping cart would be much appreciated! The code I have so far is: $submit = $_POST["submit"]; //If form is submitted, call the function and save all data into the array $_SESSION['form'] if($submit = "Buy"){setSessionVars();} function setSessionVars() { foreach($_POST as $fieldname => $fieldvalue) { $_SESSION['form'][$fieldname] = $fieldvalue; } echo "<table> <tr> <td>" .'<img src="images/'.$_SESSION['form']['item'].'.jpg"' . "<td/> <td>" . $_SESSION['form']['item'] . "</td> <td>" . '<input type="text(30)" name="value" value="1" /> <input type="submit" name="puchasednum" value="Update This One Item" />' . "</td> </tr> </table>"; }; ?> I have the session_start() function at the very top of every page. I have also attached the entire files for the two pages I talk about above. Thanks Graham 17972_.php 17973_.php
×
×
  • 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.