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