session_db.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * @class SessionDB
  4. * Fake Database. Stores records in $_SESSION
  5. */
  6. class SessionDB {
  7. public function __construct() {
  8. if (!isset($_SESSION['pk'])) {
  9. $_SESSION['pk'] = 10; // <-- start fake pks at 10
  10. $_SESSION['rs'] = getData(); // <-- populate $_SESSION with data.
  11. }
  12. }
  13. // fake a database pk
  14. public function pk() {
  15. return $_SESSION['pk']++;
  16. }
  17. // fake a resultset
  18. public function rs() {
  19. return $_SESSION['rs'];
  20. }
  21. public function insert($rec) {
  22. array_push($_SESSION['rs'], $rec);
  23. }
  24. public function update($idx, $attributes) {
  25. $_SESSION['rs'][$idx] = $attributes;
  26. }
  27. public function destroy($idx) {
  28. return array_shift(array_splice($_SESSION['rs'], $idx, 1));
  29. }
  30. }
  31. // Sample data.
  32. function getData() {
  33. return array(
  34. array('id' => 1, 'first' => "Fred", 'last' => 'Flintstone', 'email' => 'fred@flintstone.com'),
  35. array('id' => 2, 'first' => "Wilma", 'last' => 'Flintstone', 'email' => 'wilma@flintstone.com'),
  36. array('id' => 3, 'first' => "Pebbles", 'last' => 'Flintstone', 'email' => 'pebbles@flintstone.com'),
  37. array('id' => 4, 'first' => "Barney", 'last' => 'Rubble', 'email' => 'barney@rubble.com'),
  38. array('id' => 5, 'first' => "Betty", 'last' => 'Rubble', 'email' => 'betty@rubble.com'),
  39. array('id' => 6, 'first' => "BamBam", 'last' => 'Rubble', 'email' => 'bambam@rubble.com')
  40. );
  41. }