PHP:将变量保存到会话中?

发布于 2024-11-04 04:15:30 字数 924 浏览 7 评论 0原文

我想将这些数据保存到会话中,以便用户可以对其进行修改(作为原始购物车),但我在这里需要一些说明。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2024-11-11 04:15:31

把session_start();在脚本开始时,您可以将任何想要的内容放入 $_SESSION 中,包括产品数组。

$_SESSION['cart'] = $whatever_variable_you_want;

Put session_start(); at the start of your script, you can then put anything you want into $_SESSION, including arrays of products.

$_SESSION['cart'] = $whatever_variable_you_want;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文