简体   繁体   English

当用户离开数据库的“购物车”页面时,如何保存购物车

[英]How can I keep a shopping cart populated when the user navigates away from the 'cart' page, wihtout using a database

I am building a shopping cart with php, and i have all the items able to pass through to the cart page. 我正在用php构建购物车,并且我所有的物品都可以传递到购物车页面。 However, if the user were to navigate away from the page, all the items disappear. 但是,如果用户要离开页面导航,则所有项目都会消失。 I know that I can use to begin a session, but how do i persist data in the session without using a database? 我知道可以用来开始会话,但是如何在不使用数据库的情况下将数据持久化?

Should I learn HTML5 session-storage? 我应该学习HTML5会话存储吗?


Thanks, Eric. 谢谢,埃里克。 I agree, 'if it aint broke then dont try to fix it'...so, how exactly do I read/ write data to the $_SESSION array. 我同意,“如果它还没有损坏,请不要尝试对其进行修复”。。。因此,我如何准确地将数据读/写到$ _SESSION数组。 I have read the php manual page, but still am at a loss...my data doesnt transfer. 我已经阅读了php手册页,但仍然不知所措...我的数据没有传输。 Here's my code: 这是我的代码:

    <?php session_start();
$mytext = $_SESSION['mytext']; 
    $mytext1 = $_SESSION['mytext1']; 
    $mytext2 = $_SESSION['mytext2']; 
    $mytext3 = $_SESSION['mytext3'];

$price = $_SESSION['price']; 
    $price1 = $_SESSION['price1']; 
    $price2 = $_SESSION['price2']; 
    $price3 = $_SESSION['price3']; 

    $total = $mytext * $price;     
    $total1 = $mytext1 * $price1; 
    $total2 = $mytext2 * $price2; 
    $total3 = $mytext3 * $price3; 

?>' ?>”

By default, the PHP $_SESSION variable doesn't use a proper database or need you to install anything in addition to PHP. 默认情况下,PHP $ _SESSION变量不使用适当的数据库,或者除PHP外,您不需要安装其他任何东西。 It will simply store each users session data in a file on your server. 它将仅将每个用户的会话数据存储在服务器上的文件中。

After calling session_start() you can simply read/write data to the $_SESSION array as you would any other array. 调用session_start()您可以像其他数组一样简单地将数据读/写到$_SESSION数组。 Look at the session_start() page for an example. 查看session_start()页面中的示例。

No, I wouldn't do this in HTML 5. PHP sessions have been working well for years and won't have any browser compatibility issues. 不,我不会在HTML 5中执行此操作。PHP会话多年来一直运行良好,不会出现任何浏览器兼容性问题。

EDIT 编辑

Are you writing to the array first? 您是先写数组吗? If you haven't put the items in, you won't get anything out. 如果您没有放入物品,您将一无所获。 You'll notice that on the example in the linked page, there are two pages in play. 您会注意到,在链接页面的示例中,有两个页面在播放。 The first page assigns to the session array. 第一页分配会话数组。 In your code, you only try to take things out of the session array. 在代码中,您仅尝试将内容从会话数组中取出。 Until you add items to it, you won't get anything out of it. 在向其中添加项目之前,您将一无所获。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户离开页面时,如何重置会话? - How can I reset session when user navigates away from page? 如何使用数据库和PHP会话来存储用户的购物车? - How can I use a database and PHP sessions to store a user's shopping cart? 如何在不刷新产品列表的情况下将产品添加到购物车? - how can i add products to the cart wihtout refreshing the product list? 如果用户关闭浏览器窗口或浏览php中的页面,如何销毁会话? - How can I destroy sessions if user closes the browser window or navigates away from page in php? 当用户从站点 A 导航到站点 B 时,我们如何将 woocommerce 购物车详细信息从一个站点发送到另一个站点 - How we can send woocommerce cart details from one website to another when user navigates from site A to site B PHP-如何将购物车中的多个项目插入数据库? - PHP - How can I insert multiple items from a shopping cart into the database? 如何将购物车中的多个产品作为单独的行插入数据库中 - How can i insert multiple products from the shopping cart into the database as individual rows 如何使用 Gloudemans Shopping Cart 在数据库中获取“列中的单个购物车项目” - How to get “individual cart items from a column” in the database using Gloudemans Shopping Cart 我如何通过电子邮件发送此购物车数组? - How can I email this shopping cart array? 我不断收到购物车是空的……? - I keep getting the shopping cart is empty…?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM