购物车 $_SESSION 问题

发布于 2024-10-14 18:53:03 字数 3154 浏览 2 评论 0原文

我正在开发一个基本的购物车。但是,$_SESSION 变量似乎未正确存储或访问。例如,如果您前往此处,它将显示项目名称。但是在没有任何 $_GET 变量的情况下刷新 cart.php 时,它不会返回任何内容。我做错了什么?

<?php
include "tickets/config.php";

if (isset ($_GET['action']) && isset($_GET['item'])) {
    $cart = new Cart($_GET['item']);
    if ($_GET['action'] == "addItem") {
        $cart->addItem();
        $cart->get_items_code();
        $cart->populate();
    }
    if($_GET['action'] == "removeItem") {
        $cart->removeItem();
        $cart->get_items_code();
        $cart->populate();
    }
    $cart->postAllItems();
}
else {
    $cart = new Cart(null);
    $cart->get_items_code();
    $cart->populate();
    $cart->postAllItems();
}

class Cart {
    protected $all_items = array();
    protected $request_item;
    protected $content;
    protected $item_obj;

    public function __construct($request_item) {
        $this->request_item = $request_item;
        if ($this->request_item) 
            $this->item_obj = new Item($this->request_item);
        $this->all_items = $this->getAllItems();
    }

    public function getAllItems() {
        if ($_SESSION['cart'])
            $request = $_SESSION['cart'];
        else
            $request = array();
        return $request;
    }

    public function postAllItems() {
        $_SESSION['cart'] = $this->all_items;
    }

    public function addItem () {
        array_push($this->all_items, $this->item_obj);
    }

    public function removeItem() {
        unset($this->all_items[$this->item_obj->get_item()]);
    }

    public function get_items_code() {

        //for($i = 0; $this->all_items[$i]; $i++) {
        foreach($this->all_items as $item) {
            $name = $item->get_name();
            $this->content .= <<<HTML
<div class="item">
$name
</div>      
HTML;
        }
    }

    public function populate() {
        echo <<<HTML
<div id="list">     
$this->content
<div>
HTML;
    }
}


class Item {
    protected $id;
    protected $name;
    protected $price;
    protected $desc;
    protected $colors;
    protected $sizes;
    protected $pic_url;
    protected $all_info;

    public function __construct($id) {
        $this->id = $id;
        $this->get_item_info();
    }

    public function get_item() {
        return ($this);
    }

    public function get_name() {
        return $this->name;
    }

    protected function get_item_info() {
        $sql = "SELECT * FROM catalog WHERE id = ".$this->id;
        $this->all_info = mysql_query($sql);
        $this->all_info = mysql_fetch_array($this->all_info);
        $this->name = $this->all_info['name'];
        $this->price = $this->all_info['price'];
        $this->desc = $this->all_info['description'];
        $this->colors = $this->all_info['colors'];
        $this->sizes = $this->all_info['sizes'];
        $this->pic_url = $this->all_info['picture'];
    }

}




?>

I am working on a basic shopping cart. However, it appears as though the $_SESSION variable is not properly being stored or accessed. For instance if you travel to here it will display the item name. But on refresh to cart.php without any $_GET variables it returns nothing. What am I doing wrong?

<?php
include "tickets/config.php";

if (isset ($_GET['action']) && isset($_GET['item'])) {
    $cart = new Cart($_GET['item']);
    if ($_GET['action'] == "addItem") {
        $cart->addItem();
        $cart->get_items_code();
        $cart->populate();
    }
    if($_GET['action'] == "removeItem") {
        $cart->removeItem();
        $cart->get_items_code();
        $cart->populate();
    }
    $cart->postAllItems();
}
else {
    $cart = new Cart(null);
    $cart->get_items_code();
    $cart->populate();
    $cart->postAllItems();
}

class Cart {
    protected $all_items = array();
    protected $request_item;
    protected $content;
    protected $item_obj;

    public function __construct($request_item) {
        $this->request_item = $request_item;
        if ($this->request_item) 
            $this->item_obj = new Item($this->request_item);
        $this->all_items = $this->getAllItems();
    }

    public function getAllItems() {
        if ($_SESSION['cart'])
            $request = $_SESSION['cart'];
        else
            $request = array();
        return $request;
    }

    public function postAllItems() {
        $_SESSION['cart'] = $this->all_items;
    }

    public function addItem () {
        array_push($this->all_items, $this->item_obj);
    }

    public function removeItem() {
        unset($this->all_items[$this->item_obj->get_item()]);
    }

    public function get_items_code() {

        //for($i = 0; $this->all_items[$i]; $i++) {
        foreach($this->all_items as $item) {
            $name = $item->get_name();
            $this->content .= <<<HTML
<div class="item">
$name
</div>      
HTML;
        }
    }

    public function populate() {
        echo <<<HTML
<div id="list">     
$this->content
<div>
HTML;
    }
}


class Item {
    protected $id;
    protected $name;
    protected $price;
    protected $desc;
    protected $colors;
    protected $sizes;
    protected $pic_url;
    protected $all_info;

    public function __construct($id) {
        $this->id = $id;
        $this->get_item_info();
    }

    public function get_item() {
        return ($this);
    }

    public function get_name() {
        return $this->name;
    }

    protected function get_item_info() {
        $sql = "SELECT * FROM catalog WHERE id = ".$this->id;
        $this->all_info = mysql_query($sql);
        $this->all_info = mysql_fetch_array($this->all_info);
        $this->name = $this->all_info['name'];
        $this->price = $this->all_info['price'];
        $this->desc = $this->all_info['description'];
        $this->colors = $this->all_info['colors'];
        $this->sizes = $this->all_info['sizes'];
        $this->pic_url = $this->all_info['picture'];
    }

}




?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

回忆凄美了谁 2024-10-21 18:53:03

您需要使用顶部的 session_start()-function 初始化会话页面(在修改或读取会话内容之前)。

You need to initialize the session using the session_start()-function at the top of the page (before the contents of the sessions are modified or read).

泡沫很甜 2024-10-21 18:53:03

您的代码看起来不错,但您必须在修改会话或从会话读取的每个页面上调用 session_start() 。只需将其放在页面顶部即可。

session_start();

Your code looks fine, but you must call session_start() on every page that modifies the session or reads from the session. Just putting it at the top of the page will work.

session_start();
挽你眉间 2024-10-21 18:53:03

我最近还遇到了 $_SESSION 变量在页面重定向后维护自身的问题,这可能是您的问题。

http://www.example.com 重定向到 http://example.com 重定向。因此,您使用带有相对或绝对路径的重定向,但不使用站点名称:

使用

header('Location: /new_page.php');

而不是

header('Location: http://www.exmple.com/new_page.php')

“也许”在这里不适用,但确实让我很困惑。

I also recently had a problem with $_SESSION vars maintaining themselves after page redirects, which could be your problem.

Session variables will not survive a redirect from a http://www.example.com to a http://example.com redirect. So, you use redirects with relative or absolute paths, but do it without the sitename:

use

header('Location: /new_page.php');

as opposed to

header('Location: http://www.exmple.com/new_page.php')

Maybe doesn't apply here, but did trip me up quite a bit.

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