Guest User

Untitled

a guest
Jun 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. <?
  2. session_start();
  3.  
  4. class cart {
  5. private $items;
  6. function __construct() { // init
  7. $this->items = array();
  8. }
  9. function add($id,$amount,$price,$pages){
  10. $this->items[$id]['amount'] += $amount;
  11. $this->items[$id]['price'] = $price;
  12. $this->items[$id]['pages'] = $pages;
  13. }
  14. function remove($id){
  15. unset($this->items[$id]);
  16. }
  17. function update($id,$what,$val){
  18. $this->items[$id][$what] = $val;
  19. }
  20. function save(){
  21. $_SESSION['cart'] = $this;
  22. }
  23. function items(){
  24. return $this->items;
  25. }
  26.  
  27. public static saveToDB($cart) {
  28. $query = sprintf("INSERT INTO Carts (timeCreated, cartObject) VALUES(UNIX_TIMESTAMP(), '%s')", mysql_real_escape_string(serialize($cart)));
  29. mysql_query($query);
  30. return mysql_insert_id();
  31. }
  32.  
  33. public static loadFromDB($id) {
  34. $query = sprintf("SELECT cartObject FROM Carts WHERE ID = %d", $id);
  35. $data = mysql_fetch_assoc(mysql_query($query));
  36. return unserialize($data['cartObject']);
  37. }
  38. }
  39. ?>
Add Comment
Please, Sign In to add comment