问题,我想使用会话,但是我遇到了从$ food数组实现会话的问题,如果用户选择了Pizza,我不会给他或她提供三明治,同样如果用户选择Sandwitch,我不向用户提供比萨饼。
我还想知道用户是否已经选择了比萨饼或三明治。
$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,关于上面代码的结构和语法,我知道有很多问题,这就是为什么我要求帮助,谢谢你没有火红,Newb。
答案 0 :(得分:2)
//How do I set up each $food element such that it gets its own session?
结构应该是这样的:
$_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";
}
答案 1 :(得分:1)
您的代码实际上存在一些问题,我也不明白您的问题。这是我认为你需要的一个例子。
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.";
}