2011-04-29 76 views
0

我想將這些數據保存到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>"; 


?> 

回答

2

Put session_start();在腳本開始時,您可以將所需的任何內容放入$ _SESSION中,包括產品陣列。

$_SESSION['cart'] = $whatever_variable_you_want; 
+0

感謝您的幫助大衛,不過我有點卡住了,這裏是我做過什麼: $ _SESSION [ '購物車'] [1] = $項目; $ _SESSION ['cart'] [2] = $ quantity; $ _SESSION ['cart'] [3] = $ price; 我得到以下錯誤爲每個語句: 注意:未定義變量: – 2011-04-29 01:30:03

+0

對不起加布裏埃,整個錯誤沒有通過。我猜測$ item,$ quantity和$ price變量是它所說的未定義的。也許用您當前的腳本編輯您的文章?試試這個:$ _SESSION ['cart'] [] = array('price'=> $ price,'item'=> $ item,'qty'=> $ quantity); – 2011-04-29 01:38:42

+0

謝謝大衛,現在正在工作!現在我試圖弄清楚如何回顯這些數組值,即使var_dump顯示所有內容,我也會得到空格: echo「1.」。$ _ SESSION ['cart'] ['1']。「」 $ _ SESSION ['cart'] ['2']。「units」。「,」。$ _ SESSION ['cart'] ['3']。「USD。」; – 2011-04-29 01:59:08