php oturumları ve x değeri oturumları ile seçilmiş olup olmadığını belirleme

2 Cevap php

Sorun, ben oturumları kullanmak istiyorum, ama ben kullanıcı bir Pizza seçtiyse kullanıcı bir Sandwitch seçerse, ben aynı şekilde, ona bir sandviç onu sunmak ya yok böyle benim $ gıda dizi oturumları uygulama ile ilgili sorunlar yaşıyorum Ben kullanıcı bir Pizza sunmuyoruz.

Ben de kullanıcı zaten bir Pizza veya sandviç seçili olup olmadığını bilmek istiyorum.

  $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, yukarıdaki kod yapısı ve sözdizimi açısından ben, Newb yanan için teşekkür ederim, ben yardım için soruyorum, bu yüzden çok sayıda sorunlar var biliyorum.

2 Cevap

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

Yapı böyle bir şey olmalı:

$_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";
}

Aslında kod bazı sorunları var ve ben de sorunu anlamadı. Bu sana ne gerek olduğunu düşünüyorum ne bir örnektir.

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.";
}