prepare("SELECT punches.id as punchid, users.id as user, punches.intime as intime, punches.outtime as outtime, punches.notes as notes FROM punches INNER JOIN users ON punches.userid = users.id WHERE users.id = $userid ORDER BY punches.id DESC LIMIT 1"); $result->execute(); $last = $result->fetchObject(); // Let's build the page - this is the header with current status echo "

Current Status

"; if (!isset($last->user)) { echo "

You do not appear to have any punches on record.

"; $status = "Out"; } //!isset($last->user) else { if (!empty($last->outtime)) { $status = "Out"; $statustime = $last->outtime; } //!empty($last->outtime) else { $status = "In"; $statustime = $last->intime; $punchid = $last->punchid; $notes = $last->notes; } echo "

You have been Punched $status since " . date('g:i a \o\n M jS, Y', strtotime($statustime)) . ".

"; } echo "

Quick Punch

"; echo "

Clicking the button below will immediately enter a new punch for you depending on your current status. Any notes you enter will be attached to the punch for your administrator to review.

"; echo "
"; echo "
"; if (isset($notes)) { echo ""; } //isset($notes) else { echo ""; } echo "
"; if ($status == "In") { echo ""; echo ""; } //$status == "In" else { echo ""; echo ""; } echo "
"; // If the posted variables are not empty, we must be trying to insert a new punch. Use the form values to insert new record if (!empty($_POST)) { // Is the notes field set? If so, use, otherwise set to null if (isset($_POST['notes'])) { if (!empty($_POST['notes'])) { $p_notes = $_POST['notes']; } //!empty($_POST['notes']) else { $p_notes = NULL; } } //isset($_POST['notes']) else { $p_notes = NULL; } // Is the user currently punched in? If so, insert the punch out record, otherwise, insert a new punch in if ($status == "In") { $query = "UPDATE punches SET outtime = NOW(), notes = :p_notes WHERE id = :p_punchid"; $stmt = $yaptc_db->prepare($query); $stmt->execute(array( ':p_punchid' => $punchid, ':p_notes' => $p_notes )); } //$status == "In" else { $query = "INSERT INTO punches (userid, notes, intime) VALUES (:p_userid, :p_notes, NOW())"; $stmt = $yaptc_db->prepare($query); $stmt->execute(array( ':p_userid' => $_SESSION['user_id'], ':p_notes' => $p_notes )); } // And then send user back to this page to see the updates header('Location: ' . $_SERVER['PHP_SELF']); } //!empty($_POST) // Close out the form... echo "
"; echo "
"; ?>