Jump to content

incrementing session variable name


GD

Recommended Posts

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

 

Link to comment
Share on other sites

I suggest you re-read the series of posts I made in your previous thread about this.

 

One of the pieces of information that was mentioned is that your form can only submit one item at a time via the 'Buy' submit buttons. Therefore, the following code only has one product to loop over, is overwriting the $_SESSION['cart'] variable, not adding items to it, and is pointless -

 

foreach($_POST as $fieldname => $fieldvalue) {
        $item[$fieldname] = $fieldvalue;
     
        } 
$_SESSION['cart'] = $item;

 

In fact, someone handed you working add to cart and display cart functions in that thread.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.