-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpdo-iterator.php
149 lines (125 loc) · 3.47 KB
/
pdo-iterator.php
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* This example demonstrates conversion of a PDO resultset into a tabular view.
* It does not currently support
*/
class TableRows extends RecursiveIteratorIterator {
// store the iterator
protected $_it;
// the PDO
protected $_dsn;
// teh table name
protected $_table;
/**
* Load the iterator.
*/
public function __construct($it, $dsn, $table) {
$this->_it = $it;
$this->_dsn = $dsn;
$this->_table = $table;
parent::__construct($this->_it, self::LEAVES_ONLY);
}
/**
* Generates a table header based on the metadata.
*
* @access public
* @return string
*/
public function getHeader()
{
$output = '<table cellpadding="0" cellspacing="0" style="width:100%;border:1px solid #000; padding: 4px;">' . PHP_EOL;
$output .= '<thead>' . PHP_EOL;
$output .= '<tr>';
$results = $this->_dsn->query(sprintf('SHOW COLUMNS FROM %s', $this->_table));
foreach ($results as $r) {
$output .= '<th>' . $r['Field'] . '</th>';
}
$output .= '</tr>' . PHP_EOL;
$output .= '</thead>' . PHP_EOL;
$output .= '<tbody>' . PHP_EOL;
echo $output;
}
/**
* Get the body.
*
* @access public
* @return string
*/
public function getBody()
{
$output = '';
while ($this->valid()) {
echo '<td>' . $this->current() . '</td>';
$this->next();
}
}
/**
* Generates the table footer.
*
* @access public
* @return string
*/
public function getFooter()
{
echo '</tbody></table>';
}
/**
* Create a new table row.
*
* @access public
* @return string
*/
public function beginChildren() {
echo '<tr>';
}
/**
* Close a table row.
*
* @access public
* @return string
*/
public function endChildren() {
echo '</tr>' . PHP_EOL;
}
}
try {
// load the database via PDO
$dsn = new PDO('mysql:dbname=testdb;host=127.0.0.1');
// the result only implements Traversable
$stmt = $dsn->prepare('SELECT * FROM test');
// exceute the query
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
// get the results
$results = $stmt->fetchAll();
// generate the recursive iterator
$it = new RecursiveArrayIterator($results);
$TableRows = new TableRows($it, $dsn, 'test');
// output the table
$TableRows->getHeader();
$TableRows->getBody();
$TableRows->getFooter();
} catch (PDOException $e) {
die($e->getMessage());
}
/*
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE test (
`id` int(11) unsigned auto_increment primary key,
`name` varchar(32),
`description` varchar(255),
`created` int(11) unsigned
);
INSERT INTO test (id, name, description, created) VALUES
(1, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW())),
(2, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW())),
(3, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW())),
(4, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW())),
(5, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW())),
(6, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW())),
(7, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW())),
(8, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW())),
(9, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW())),
(10, 'Entry 1', 'Description', UNIX_TIMESTAMP(NOW()));
*/