【问题标题】:PHP: Make variables into SESSION?PHP:将变量放入会话中?
【发布时间】:2011-08-15 03:46:36
【问题描述】:

我正在从事一项工作,我想将数据保存到 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>";


?>

【问题讨论】:

    标签: session variables shopping-cart


    【解决方案1】:
    $_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");
    

    等等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 2010-11-09
      • 2013-09-08
      相关资源
      最近更新 更多