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 want to save this 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)
把session_start();在脚本开始时,您可以将任何想要的内容放入 $_SESSION 中,包括产品数组。
Put session_start(); at the start of your script, you can then put anything you want into $_SESSION, including arrays of products.