tags:

views:

86

answers:

2

Problem, I want to use sessions, but I am having issues with implementing sessions from my $food array such that if the user has selected a Pizza, I don't offer him or her a sandwich, likewise if the user selects a Sandwitch, I don't offer the user a Pizza.

I also want to know if the user has already selected a Pizza or Sandwich.

  $food = array("Pizza" => $_POST["pepperoni"], "Sandwitch"=>$_POST["chickensandwitch"]);

//How do I set up each $food element such that it gets its own session?
// In addition to checking if  $food["Pizza"] has been selected or not?
if (isset($_SESSION('$food["Pizza"]')))
{
$_SESSION('$food["Pizza"]') = $_SESSION('$food["Pizza"]') 
echo "You have already selected Pepperoni";
}
else
{
echo "Please select  a Pizza";
}

PS, with regards to the structure of the above code, and syntax I know there are numerous problems, that's why I am asking for help, thank you for not flaming, the Newb.

+2  A: 
//How do I set up each $food element such that it gets its own session?

Structure should be something like this:

$_SESSION['food']['pizza'] = 'pepperoni';
$_SESSION['food']['sandwich'] = 'chicken sandwich';

// checking if food[pizza]has been selected/defined
if( isset($_SESSION['food']['pizza']) ) {
    echo "You've already selected " . $_SESSION['food']['pizza'];
} else {
    echo "Please select a Pizza";
}
lyrae
+1  A: 

Your code in fact has some problems and I also didn't understand your problem. This is an example of what I think is what you need.

if( !isset($_SESSION['food']) ) {
 if( isset($_POST['food']) ) {
  $_SESSION['food'] = $_POST['food'];
 } else {
  die( "You'll have to select whicht type of food you want." );
 }
}

if( $_SESSION['food']=="Pizza" ) {
 // User selected pizza
 if( isset($_POST['pizza']) ) {
  // User selected a type of pizza
  $_SESSION['pizza'] = $_POST['pizza'];
 } else if( !isset($_SESSION['pizza']) ) {
  // No pizza selected by user and also none in session
  die( "You'll have to select a pizza." );
 } else {
  // User selected type of pizza previously
  echo htmlspecialchars( "You selected pizza '{$_SESSION['pizza']}'." ) . "<br />\n";
 }
} else if( $_SESSION['food']=="Sandwhich" ) {
 // Same as above just for sandwhiches..
} else {
 echo "No pizza or other food selected.";
}
svens
Thank you guys.
Newb