【问题标题】:PHP: Save variables into a SESSION?PHP:将变量保存到会话中?
【发布时间】:2011-04-29 00:50:34
【问题描述】:

我想将这些数据保存到 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>";


?>

【问题讨论】:

    标签: arrays session variables save


    【解决方案1】:

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

    $_SESSION['cart'] = $whatever_variable_you_want;
    

    【讨论】:

    • 感谢您的帮助,大卫,但是我有点卡住了,这就是我所做的:$_SESSION['cart'][1] = $item; $_SESSION['cart'][2] = $数量; $_SESSION['cart'][3] = $price;我收到每个语句的以下错误:注意:未定义变量:
    • 对不起,加布里埃尔,整个错误没有出现。我猜$item、$quantity 和$price 变量就是它所说的未定义。也许用您当前的脚本编辑您的帖子?试试这个: $_SESSION['cart'][] = array('price' => $price, 'item' => $item, 'qty' => $quantity);
    • 谢谢大卫,现在正在工作!现在我试图弄清楚如何回显这些数组值,即使 var_dump 显示所有内容,我也会得到空格: echo "1.".$_SESSION['cart']['1']." "。 $_SESSION['cart']['2']." units".", ".$_SESSION['cart']['3']." USD.";
    • 在我给你的结构中,它会是:$_SESSION['cart'][0]['item'],等等
    猜你喜欢
    • 2013-09-08
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 2013-08-06
    • 1970-01-01
    相关资源
    最近更新 更多