SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    $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:

    Code:
    $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!

  2. #2
    SitePoint Zealot Jaanboy's Avatar
    Join Date
    Sep 2007
    Location
    UK
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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'].

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have:
    PHP Code:
    session_start(); 
    at the top of both script files?
    www.forkaya.com - Web Development, PHP Scripting

  4. #4
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  5. #5
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    $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.

    Code:
    $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
    }}

  6. #6
    SitePoint Zealot Jaanboy's Avatar
    Join Date
    Sep 2007
    Location
    UK
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the second script do:
    PHP Code:
    var_dump($_SESSION['qty']);
    var_dump($_SESSION['spice_level']); 
    to see where your'e going wrong.

  7. #7
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jaanboy View Post
    In the second script do:
    PHP Code:
    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.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    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.

    Code php:
    <?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>";
    ?>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •