Last active
January 3, 2016 11:08
-
-
Save thewinterwind/8453763 to your computer and use it in GitHub Desktop.
Interview test answer - January 15, 2014
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Email a URL to colleague so he can visit the Admin sessions page | |
// Current page: /admin/sessions (GET route) | |
$type = 'type'; | |
$start_date = '2012-01-01'; | |
$end_date = '2012-03-01'; | |
$session_id = '123456'; | |
$url = 'http://domain.com/admin/sessions?type=' . urlencode($type); | |
$url .= '&start_date=' . urlencode($start_date); | |
$url .= '&end_date=' . urlencode($end_date); | |
$url .= '&session_id=' . urlencode($session_id); | |
$to = 'colleague@domain.com'; | |
$subject = 'Admin Link'; | |
$message = '<a href="' . $url . '">Please review data for session: ' . $session_id . '</a>'; | |
$headers = 'From: webmaster@domain.com' . "\r\n" . 'Reply-To: webmaster@domain.com' . "\r\n"; | |
if (!mail($to, $subject, $message, $headers)) die('Unable to send admin email'); | |
?> | |
<!DOCTYPE html> | |
<form method="POST" action="/admin/sessions/"> | |
<input type="hidden" value="<?php echo $_GET['type']; ?>" name="type"> | |
<input type="hidden" value="<?php echo $_GET['start_date']; ?>" name="start_date"> | |
<input type="hidden" value="<?php echo $_GET['end_date']; ?>" name="end_date"> | |
<input type="hidden" value="<?php echo $_GET['session_id']; ?>" name="session_id"> | |
<button type="submit">Share Page</button> | |
</form> | |
<?php | |
// Current controller action: /admin/sessions (POST route) | |
$type = urldecode($_POST['type']); | |
$start_date = urldecode($_POST['start_date']); | |
$end_date = urldecode($_POST['end_date']); | |
$session_id = urldecode($_POST['session_id']); | |
$dsn = 'mysql:host=localhost;dbname=testdb'; | |
$username = 'username'; | |
$password = 'password'; | |
$dbh = new PDO($dsn, $username, $password); | |
$sql = 'SELECT * FROM sessions WHERE sess_id=' . $session_id; | |
$sql .= ' AND WHERE type = ' . $type; | |
$sql .= ' AND start_date >= ' . $start_date; | |
$sql .= ' AND end_date <= ' . $end_date; | |
$sql .= ' ORDER BY start_date LIMIT 500'; | |
?> | |
<table> | |
<thead> | |
<tr> | |
<th>Session ID</th> | |
<th>Type</th> | |
<th>Start Date</th> | |
<th>End Date</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach($dbh->query($sql) as $row) { ?> | |
<tr> | |
<td><?php echo $row['sess_id'] ?></td> | |
<td><?php echo $row['type'] ?></td> | |
<td><?php echo $row['start_date'] ?></td> | |
<td><?php echo $row['end_date'] ?></td> | |
</tr> | |
<?php } ?> | |
</tbody> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment