April 20, 2012

PHP Session Variables – passing through pages

Question by ez007

Im hoping someone can point me in the right direction of where im going wrong as I feel like im going around in circles!

Im putting together a simple shopping applications – its only very basic at the moment to demonstrate techniques.

The scenario is that there is one database table with items in. They have been split into a blue and red range of items.

On the index page the user has the option of going to either the blue or red items.

Once on the red (or blue) items page, items are displayed and current price and stock level is pulled from the database (MySQL). The user then selects one item and clicks the buy button to add it into their cart.

The page then redirects to the shopping cart page where the user can either update the quantity of the item, proceed to the checkout page or return to the ‘red’ or ‘blue’ ranges.

My issue is this…..

1) How do I set up my session array to capture the items as they are added on the buy ‘click’?

So far I have this on the top of all pages…

<?php session_start();
?>

However only one item seems to be able to be added to the ‘cart’.

This is how im pulling items from my DB:

<?php
$result = mysql_query ('SELECT * FROM items WHERE colour="red"');
// fetch the resulting row as an associative array
while ($row = mysql_fetch_assoc ($result)){
  echo '£', number_format( $row ['price'] / 100, 2, '.', ' ' );
  }
?></p>

2) This is the code for the form action under each item on either the red or blue page.

<form method="post" action="cart.php">
                    <p>
<input type="submit" name="submit" value="Buy" />
<input type="hidden" name="cart" value="add" />
<input type="hidden" name="item" value="redplate" />
                    </p>
                </form>

3) How do I display the ‘ordered’ item in the checkout page after any quantity updates on the shopping cart page?

So far this is what it on the shopping cart page – would I repeat this on the checkout page pulling with it the updated quantity??….

<?php
$submit = $_POST["submit"];

//Call the function and save all data into the array $_SESSION['form'] 
if($submit == "Buy"){setSessionVars();} 

function setSessionVars() {

    $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(5)" name="value" value="1" />

            <input type="submit" name="puchasedquan" value="Click to update" /> 

             </td> 
            </tr> 
                            </table>";
}
?>

Any help would be greatly appreciated!! I feel as if i’m traveling around in circles!

Answer by Starx

The big mistake here is, you are putting all you datas on one session variable altogether, i.e. $_SESSION['cart'].

Since you want to insert multiple item on the sessions, you have use $_SESSION['cart'][] to insert the items.

Whenever you are trying to get the values stored, again use a for loop to read as well as .

foreach($_SESSION['cart'] as $cartItem) {
  $cartItem; // will have all the item individually on each pass
}
...

Please fill the form - I will response as fast as I can!