PHP:将变量放入会话中?
我正在进行一项作业,我想将数据保存到会话中,以便用户可以对其进行修改(作为原始购物车),但我在这里需要一些说明。
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>";
?>
I'm working in a assigment and I want to save the data into a SESSION so it can be modified by the user(as a primitive shopping cart), but I need some light in here.
A) The information comes from a POST form.
B) The output should look like this:
SHOPING LIST
1. Coffe 5 units, 6 USD.
2. Banana 3 units, 3 USD.
3. Etc (The list can be infinite)
C) This is my current code, as you can see there is no session. And I need the user to be able to add more items.
<?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>";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另请确保在将 ANY 输出到文档之前调用
session_start()
。我怎么强调都不为过。我什至在 DOCTYPE 减速之前就已经说过了。然后你应该能够从任何地方做..
等等
。
Also make sure you call
session_start()
before ANY output to the document. I can't stress this enough. I'm talking even before the DOCTYPE deceleration.Then you should be able to from anywhere afterwards do..
and
etc.