PHP:将变量放入会话中?

发布于 2024-11-04 04:13:08 字数 932 浏览 1 评论 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'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 技术交流群。

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

发布评论

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

评论(1

锦爱 2024-11-11 04:13:08
$_SESSION["foo"] = "bar";

另请确保在将 ANY 输出到文档之前调用 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");

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

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..

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

and

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

etc.

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