2011-04-29 155 views
1

我在分配工作,我想將數據保存到SESSION中,以便用戶可以修改它(作爲原始購物車),但是我需要在這裏使用一些光源。PHP:將變量變爲SESSION?

A)信息來自POST表單。

B)的輸出應該是這樣的:

SHOPING LIST 
1. Coffe 5 units, 6 USD. 
2. Banana 3 units, 3 USD. 
3. Etc (The list can be infinite) 

C)這是我當前的代碼,你可以看到有沒有會話。而且我需要用戶能夠添加更多項目。

<?php 

//Variables 
$item= $_POST['item']; 
$quantity= $_POST['quantity']; 
$code= $_POST['code']; 


//List 
$articulos = array(

    'Pinaple' => 1, 'Banana' => 2, 'Aple' => 3, 
    'Milk' => 1, 'Coffe' => 3, 'Butter' => 1, 
    'Bread' => 2, 'Juice' => 1, 'Coconuts' => 1, 
    'Yogurt' => 2, 'Beer' => 1, 'Wine' => 6, 
); 

//Price 
$price = $items[$item] * $quantity; 

//Shoping List 

echo "<b>Shopping List</b></br>"; 


echo "1. ".$item." ".$quantity." units".", ".$price." USD."; 

//Back to index 
echo "</br> <a href='index.html'>Back to Index</a>"; 


?> 

回答

1
$_SESSION["foo"] = "bar"; 

還要確保您撥打session_start()之前任何輸出到文件。我無法強調這一點。我甚至在DOCTYPE減速之前說話。

那麼你應該能夠從任何地方做以後..

echo $_SESSION["foo"]; // output: bar 
$_SESSION["foo"] = "new bar"; 
$_SESSION["new foo"] = $_SESSION["foo"]; 

$_SESSION["items"] = array("pants", "hat"); 
array_push($_SESSION["items"], "shirt");