function add_items_to_cart($id, $count) { if (empty($_SESSION['simple_cart']) || !isset($_SESSION['simple_cart'])) { $c = new Cart(); $c->add_item($id, $count); $_SESSION['simple_cart'] = $c; } else { $_SESSION['simple_cart']->add_item($id, $count); } }
public static function create_from_signup($signup, $note = "") { $hash = self::hash_items($signup); $cart = Cart::find("carts.hash = '{$hash}' AND carts.hash IS NOT NULL"); if ($cart) { return $cart; } $cart = new Cart(); $cart->user_id = $signup->user->id; $cart->note = $note; $cart->hash = $hash; if ($cart->save()) { // Add Signup $added = false; if (!$signup->paid) { $cart->add_item($signup); $added = true; } foreach ($signup->event_services() as $service) { if (!$service->paid) { $cart->add_item($service); $added = true; } } $cart = Cart::find_by_id($cart->id); if ($added) { return $cart; } else { $cart->destroy(); return false; } } return false; }
function remove_item($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } } } ?> <?php error_reporting(E_ALL & ~E_NOTICE); $cart = new Cart(); $cart->add_item("10", 1); $another_cart = new Cart(); $another_cart->add_item("0815", 3); ?> <?php // correct, single $ $cart->items = array("10" => 2); var_dump($cart->items); // invalid, because $cart->$items becomes $cart->"" //$cart->$items = array("10" => 1); // correct, but may or may not be what was intended: // $cart->$myvar becomes $cart->items $myvar = 'items'; $cart->{$myvar} = array("10" => 1); var_dump($cart->items);
[expect] Undefined offset (1) [expect] 1 [file] <?php class Cart { var $items; // Items in our shopping cart // Add $num articles of $artnr to the cart function add_item($artnr, $num) { $this->items[$artnr] += $num; } // Take $num articles of $artnr out of the cart function remove_item($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } } } $c = new Cart(); $c->add_item(1, 5); $b = $c->remove_item(1, 6); echo $b; $b = $c->remove_item(1, 4); echo $b;
public function user_index() { $user_id = mysql_real_escape_string(Site::CurrentUser()->id); // Code from Cart->create() // First check CRSF if ($this->post) { // From the post data, build the cart items $raw_items = array(); if (isset($_POST['items']['signups'])) { foreach ($_POST['items']['signups'] as $id => $value) { $id = mysql_real_escape_string($id); $signup = EventSignup::find("event_signups.user_id = '{$user_id}' AND event_signups.id = '{$id}'"); if ($signup && !$signup->paid && !$signup->is_soldout()) { $raw_items[$signup->id]['signup'] = $signup; } } } if (isset($_POST['items']['services'])) { $service_count = array(); foreach ($_POST['items']['services'] as $id => $value) { $id = mysql_real_escape_string($id); $service = EventService::find("event_signups.user_id = '{$user_id}' AND event_services.id = '{$id}'"); if ($service && !$service->paid && $service->service->available()) { if (isset($service_count[$service->service->id])) { $service_count[$service->service->id]++; } else { $service_count[$service->service->id] = 1; } if ($service->service->available() == -1 || $service_count[$service->service->id] <= $service->service->available()) { if ($service->event_signup->paid || isset($raw_items[$service->event_signup->id]['signup'])) { $raw_items[$service->event_signup->id]['services'][] = $service; } } } } } // Sort properly $items = array(); foreach ($raw_items as $id => $parts) { if (isset($parts['signup'])) { $items[] = $parts['signup']; } if (isset($parts['services'])) { $items = array_merge($items, $parts['services']); } } if (count($items) == 0) { Site::Flash("error", "None of the items you selected could be paid for."); Redirect("bookings"); } $hash = Cart::hash_items($items); $cart = Cart::find("carts.hash = '{$hash}' AND carts.hash IS NOT NULL"); if (!$cart) { $cart = new Cart(); $cart->user_id = $user_id; $cart->hash = $hash; if ($cart->save()) { foreach ($items as $item) { $cart->add_item($item); } } else { Site::Flash("error", "Unable to create cart."); Redirect("bookings"); } } Redirect("bookings/pay/{$cart->id}"); } elseif ($this->post) { global $site; $site['flash']['error'] = "Invalid form submission"; } // Fetch all signups in event order and iterate through them $items = array(); $signups = EventSignup::find_all("event_signups.user_id = '{$user_id}' AND events.enddate >= NOW()", "events.startdate DESC"); foreach ($signups as $signup) { if (!$signup->paid) { $items[] = $signup; } else { foreach ($signup->event_services() as $service) { if (!$service->paid) { $items[] = $signup; break; } } } } $this->assign("items", $items); // Traditional My Bookings Page $unpaid = EventSignup::find_all("event_signups.user_id = '{$user_id}' AND event_signups.paid = false", "events.startdate DESC"); $paid = EventSignup::find_all("event_signups.user_id = '{$user_id}' AND event_signups.paid = true AND voucher = false", "events.startdate DESC"); $vouchers = EventSignup::find_all("event_signups.user_id = '{$user_id}' AND event_signups.paid = true AND voucher = true", "events.startdate DESC"); $this->assign("unpaid", $unpaid); $this->assign("paid", $paid); $this->assign("vouchers", $vouchers); $this->title = "My Bookings"; $this->render("event_signup/user_index.tpl"); }
var $items; var $name; function __construct($myName) { echo "{$myName} 's cart, charge process: <br>"; $this->name = $myName; } function add_item($artnr, $num) { $this->items[$artnr] += $num; echo "<br>add {$num} {$artnr}, {$artnr} 's total num:" . $this->items[$artnr]; } function remove_item($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; echo "<br>remove {$num} {$artnr}, {$artnr} 's total num: " . $this->items[$artnr]; return true; } elseif ($this->items[$artnr] == $num) { unset($this->items[$artnr]); echo "<br>remove {$num} {$artnr}, {$artnr} 's total num is zero"; } else { return false; } } } $myCart = new Cart("zhang san"); $myCart->add_item("apple", 9); $myCart->add_item("banana", 8); $myCart->remove_item("apple", 2); $myCart->remove_item("banana", 3);