我正在创建一个购物车类,它以前在我使用全局 db 变量时工作,但现在在我在构造函数中建立连接后,get 变量变为空。这导致一个空篮子。
<?php
require_once 'config.class.php';
class cart
{
private $db;
public function __construct()
{
$this->db = new db();
$this->db = $this->db->connect();
}
function writeShoppingCart() {
$cart = $_SESSION['cart'];
var_dump($cart);
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="index.php?page=cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}
function showCart() {
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="index.php?page=cart.php&action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$quantity) {
if($result = $this->db->prepare('SELECT * FROM book WHERE id = '.$id))
{
$result->execute();
$result->bind_result($id, $title, $author, $price);
$result->fetch();
$output[] = '<tr>';
$output[] = '<td><a href="index.php?page=cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$title.' by '.$author.'</td>';
$output[] = '<td>£'.$price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$quantity.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($price * $quantity).'</td>';
$total += $price * $quantity;
$output[] = '</tr>';
}
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>£'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return implode('',$output);
}
function addChart(){
$sql = 'SELECT * FROM book ORDER BY id';
$result = $this->db->prepare($sql);
$output[] = '<ul>';
while ($row = $result->fetch())
{
$output[] = '<li>"'.$row['title'].'" by '.$row['author'].': £'.$row['price'].'<br /><a href="index.php?page=cart.php&action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ul>';
return implode('',$output);
}
}
?>
<?php
session_start();
require_once('../resources/library/cart.class.php');
$obj_cart = new cart();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
//
case 'add':
if ($cart) {
$cart .= ','.$_GET['id'];
} else {
$cart = $_GET['id'];
}
break;
//in this case the foreach searches for product id which we arent deleting, and puts them in the $newcart
case 'delete':
if ($cart)
{
$items = explode(',',$cart);
$newcart = '';
//
foreach ($items as $item) {
if ($_GET['id'] != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
//
case 'update':
if ($cart) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$id = str_replace('qty','',$key);
if($newcart != '')
{
$items = explode(',',$newcart);
}
else
{
$items = explode(',',$cart);
}
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
$newcart = '';
//
foreach ($items as $item) {
if ($id != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
for ($i=1;$i<=$value;$i++) {
if ($newcart != '') {
$newcart .= ','.$id;
} else {
$newcart = $id;
}
}
}
}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
$output[] = '<div id=\'shoppingcart\'>';
$output[] = '<h1>Your Shopping Cart</h1>';
$output[] = $obj_cart->writeShoppingCart();
$output[] = '</div>';
$output[] = '<div id=\'contents\'>';
$output[] = '<h1>Please check quantities...</h1>';
$output[] = $obj_cart->showCart();
$output[] = '<p><a href=\'index.php?page=add.php\'>Back to bookshop...</a></p>';
$output[] = '</div>';
echo implode($output);
?>
<?php
class db
{
public $db = '';
function connect()
{
try
{
$this->db = new mysqli('localhost', 'root', '', 'webgame');
}
catch (Exception $e)
{
echo 'cannot connect to database: ', $e->getMessage(), "\n";
}
return $this->db;
}
}
?>