PHP Session Data Problem!

Hello,

I have the following code processing a form located at tandoorpgh.com/placeanorder which declares two variables, $qty and $spice_level from the array. I try saving them as Session variables and then passing them onto the following script but nothing prints out.

They echo here perfectly:

$items = $_POST['items'];
foreach($items as $item){
    $qty = $_POST['qty_' . $item]; // now the quantity of that particular item will be stored here
    $spice_level = $_POST['spice_level_' . $item]; // now the spice_level of that particular item will be stored here
	
		$_SESSION['qty'] = $_POST['qty_' . $item]; 
		$_SESSION['spice_level'] = $_POST['spice_level_' . $item];

	
	


$query = mysql_query("SELECT * FROM `menu` WHERE id = '$item'");
	while($row = mysql_fetch_array($query)) {
		
		
		if ($qty > 0) {
		
		$total = $total + $row['price'] * $qty;
		echo "<p>$qty - " . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span> - Spice Level: $spice_level</p>";
		} else {
			$total = $total + $row['price'];
			echo "<p>1 - " . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span> - Spice Level: $spice_level</p>";
		}

But when called up on the following script here within $finalitems, they display as blank. This script here is for faxing the variables:

$items = $_SESSION['items'];
foreach ($items as $item) {
	
	$spice_level = $_SESSION['spice_level'];
	$qty = $_SESSION['quantity'];
	
		$sql = mysql_query("SELECT * FROM `menu` WHERE id = '$item'");
			while($row = mysql_fetch_array($sql)){
				
        $finalitems .= $qty . ' - ' . $row['title'] . ' - Spice Level: ' . $spice_level . '<br><br />'; //changed this line
}}

Any help is highly appreciated. I’ve been working on this for days. Thanks!

Is the second piece of code on a separate page? If so, you’ll need to call session_start() to let PHP know you want to use sessions.

Also, in the first section you use $_SESSION[‘qty’], but in the second section your using $_SESSION[‘quantity’].

Do you have:

session_start();

at the top of both script files?

Maybe I’m going blind, but you also don’t define the $_SESSION[‘items’] anywhere.
So if that one is empty, no loop will be executed in the second page.

Here’s more of a complete script and yes I do have session_start() at the beginning. This is the first script that processes the form and displays the info for the customer to review.

$pickup_delivery = $_POST['pickup_delivery'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$notes = $_POST['notes'];
$items = $_POST['items'];

$_SESSION['pickup_delivery'] = $pickup_delivery;
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['email'] = $email;
$_SESSION['phone'] = $phone;
$_SESSION['address'] = $address;
$_SESSION['city'] = $city;
$_SESSION['zip'] = $zip;
$_SESSION['notes'] = $notes;
$_SESSION['items'] = $items;



echo "<span id=text_black>Order is for $pickup_delivery</span><br><br>$first_name $last_name<br>$email<br>$phone<br>$address<br>$city, PA $zip<br><br><span id=text_black>Order Notes:</span><br>$notes";
?>

<h3>Items Ordered</h3>

<?php

$items = $_POST['items'];
foreach($items as $item){
    $qty = $_POST['qty_' . $item]; // now the quantity of that particular item will be stored here
    $spice_level = $_POST['spice_level_' . $item]; // now the spice_level of that particular item will be stored here
	
		$_SESSION['qty'] = $_POST['qty_' . $item]; 
		$_SESSION['spice_level'] = $_POST['spice_level_' . $item];

	
	


$query = mysql_query("SELECT * FROM `menu` WHERE id = '$item'");
	while($row = mysql_fetch_array($query)) {
		
		
		if ($qty > 0) {
		
		$total = $total + $row['price'] * $qty;
		echo "<p>$qty - " . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span> - Spice Level: $spice_level</p>";
		} else {
			$total = $total + $row['price'];
			echo "<p>1 - " . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span> - Spice Level: $spice_level</p>";
		}
			
		


$_SESSION['total'] = $total;
		}
	}

Then here is the script that actually faxes the information. This is the one where right now quantity and spice_level just display the word Array.

$items = $_SESSION['items'];
foreach ($items as $item) {
	
	$spice_level = $_SESSION['spice_level'];
	$qty = $_SESSION['qty'];
	
		$sql = mysql_query("SELECT * FROM `menu` WHERE id = '$item'");
			while($row = mysql_fetch_array($sql)){
				
        $finalitems .= $qty . ' - ' . $row['title'] . ' - Spice Level: ' . $spice_level . '<br><br />'; //changed this line
}}

In the second script do:

var_dump($_SESSION['qty']);
var_dump($_SESSION['spice_level']);

to see where your’e going wrong.

The problem seems to be if I select that I want the following:

1 Chicken Kebab Spice Level: Hot
2 Chicken Rolls Spice Level: Medium

It displays correctly in the first script yet the second script it displays as:

2 Chicken Kebab Spice Level: Medium
2 Chicken Rolls Spice Level: Medium

As if only the second selection’s quantity and spice level are displayed for both items. It seems to only read the last selections values.

Here is a testing version that I put together the other day, that uses an array of values instead of the database call.

I think that you’ll find that it works better. Replace the $rolls parts of the code with your normal database calls, and you should be set.


<?php
session_start();
//Rolls

$rolls = array(
    'hot roll' => array(
        'price' => 10,
        'description' => 'These are hot rolls'
    ),
    'steamy roll' => array(
        'price' => 11,
        'description' => 'These are steamy rolls'
    )
);

echo '<h3>Rolls</h3>';
echo '<form method="post">';
foreach ($rolls as $title => $row) {
    echo '<input type="checkbox" name="title[]" value="' . $title . '" /><b><span id=text_black>' . $title . '</span> - Quantity: <input type=text name="quantity['.$title.']" maxlength=1 size=1> - Spice Level: <select name="spice_level['.$title.']">
	<option value=Mild>Mild</option>
    <option value=Medium>Medium</option>
    <option value=Hot>Hot</option>
    <option value=Extra Hot>Extra Hot</option>
</select></b>
<span id="text_black">- $' . $row['price'] . '<br><i>' . $row['description'] . '</i></span>
<br><br>';
}
echo '<input type="submit"></form>';

$item = $_POST['title'];
$quantity = $_POST['quantity'];
$spice_level = $_POST['spice_level'];

$_SESSION['items'] = $item;
$_SESSION['quantity'] = $quantity;
$_SESSION['spice_level'] = $spice_level;

$total = 0;
if (!empty($item)) {
// end addition
    foreach ($item as $index => $itemname) {
        $row = $rolls[$itemname];
        $total = $total + $row['price'];
    echo "<p>" . $quantity[$itemname] . " - " . $row['title'] . " - <span style=color:#000;>$" . $row['price'] . "</span> - Spice Level: " . $spice_level[$itemname] . "</p>";
    $_SESSION['total'] = $total;
    }
}
echo "<h3>ORDER TOTAL: $ $total</h3>";
?>