forked from jstolpe/blog_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_data.php
248 lines (236 loc) · 5.38 KB
/
global_data.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
// global defines
define( 'DB_HOST', '<DATABASE-HOST>' );
define( 'DB_USERNAME', '<DATABASE-USERNAME>' );
define( 'DB_PASSWORD', '<DATABASE-PASSWORD>' );
define( 'DB_NAME', '<DATABASE-NAME>' );
// get connection to database
$database = getDatabase();
// get all table data
$allDrinks = getAllDrinks( $database );
/**
* Get PDO database connection.
*
* @return object
*/
function getDatabase() {
// PDO data source name
$dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME;
$options = array( // PDO options we want to return a nice associative array where the keys are the column names
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
);
try { // try hard
// return the database connection
return new PDO( $dsn, DB_USERNAME, DB_PASSWORD, $options );
} catch ( PDOException $e ) { // yikes
// die and print out the error
die( $e->getMessage() );
}
}
/**
* Select all data from the drinks table.
*
* @param object $database
* @return array
*/
function getAllDrinks( $database ) {
// SELECT ALL from drinks table
$sql = '
SELECT
*
FROM
drinks
';
// prepare the sql
$statement = $database->prepare( $sql );
// execute the sql
$statement->execute();
// return the query data
return $statement->fetchAll();
}
/**
* Display view html.
*
* @param string $sql sql string with values replaced with variables
* @param string $pageTitle title for the page
* @param string $allDrinks all the data in the drinks table
* @param string $queryDrinks drinks retured from the query to the database
* @param string $statement pdo object
* @return void
*/
function displayViewHtml( $sql, $allDrinks, $queryDrinks, $statement ) {
// generate page title
$pageTitle = 'MySQL SELECT ' . strtoupper( pathinfo( $_SERVER['SCRIPT_FILENAME'], PATHINFO_FILENAME ) ) . ' | PHP Tutorial | PDO Tutorial';
?>
<!DOCTYPE html>
<html>
<head>
<title>
<?php echo $pageTitle; // display page title ?>
</title>
</head>
<body>
<?php displayNavBar(); // display the navigation bar ?>
<h2>
<?php echo $pageTitle; // display page title ?>
</h2>
<hr />
<?php displayDrinks( $allDrinks ); // display all drinks in a table ?>
<hr />
<div>
<h2>
<?php echo $sql; // display the sql statement we are running ?>
</h2>
<?php displayDrinks( $queryDrinks ); // display query drinks in a table ?>
<h2>
PDO Debug
</h2>
<pre><?php $statement->debugDumpParams(); // dump out pdo params ?></pre>
<h2>
Results
</h2>
<pre><?php print_r( $queryDrinks ); // print out the raw php result array ?></pre>
</div>
</body>
</html>
<?php
}
/**
* Display table data in an html table.
*
* @param array drinks for display in the table
* @return object
*/
function displayDrinks( $drinks ) {
?>
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>type</th>
<th>flavor</th>
<th>sugar_free</th>
<th>size</th>
<th>expiration_date</th>
</tr>
<?php foreach ( $drinks as $drink ) : // loop over the data and display each row ?>
<tr>
<td><?php echo $drink['id']; ?></td>
<td><?php echo $drink['name']; ?></td>
<td><?php echo $drink['type']; ?></td>
<td><?php echo $drink['flavor']; ?></td>
<td><?php echo $drink['sugar_free']; ?></td>
<td><?php echo $drink['size']; ?></td>
<td><?php echo $drink['expiration_date']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<br />
<?php
}
/**
* Display html nav bar
*
* @return void
*/
function displayNavBar() {
?>
<table>
<tr>
<td>
<a href="index.php">
SELECT ALL
</a>
</td>
<td>
<a href="where.php">
WHERE
</a>
</td>
<td>
<a href="and.php">
AND
</a>
</td>
<td>
<a href="or.php">
OR
</a>
</td>
<td>
<a href="in.php">
IN
</a>
</td>
</tr>
<tr>
<td>
<a href="in.php?filter=NOTIN">
NOT IN
</a>
</td>
<td>
<a href="distinct.php">
DISTINCT
</a>
</td>
<td>
<a href="between.php">
BETWEEN
</a>
</td>
<td>
<a href="between.php?filter=NOTBETWEEN">
NOT BETWEEN
</a>
</td>
<td>
<a href="like.php">
LIKE
</a>
</td>
</tr>
<tr>
<td>
<a href="like.php?filter=LIKEEND">
LIKE END
</a>
</td>
<td>
<a href="like.php?filter=LIKESTART">
LIKE START
</a>
</td>
<td>
<a href="like.php?filter=NOTLIKESTART">
NOT LIKE START
</a>
</td>
<td>
<a href="order_by.php">
ORDER BY ASC
</a>
</td>
<td>
<a href="order_by.php?filter=ORDERBYDESC">
ORDER BY DESC
</a>
</td>
</tr>
<tr>
<td>
<a href="limit.php">
LIMIT
</a>
</td>
<td>
<a href="limit.php?filter=LIMITOFFSET">
LIMIT OFFSET
</a>
</td>
</tr>
</table>
<hr />
<?php
}
?>