<?
    try
    {
        $dbh = new PDO("sqlite:lunch.db");
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch (PDOException $e) 
    {
        die("App is offline.  Please contact David.");
    }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>

    <link href="http://yui.yahooapis.com/2.7.0/build/reset-fonts/reset-fonts.css" rel="stylesheet" type="text/css" />
    <link href="styles.css" rel="stylesheet" type="text/css" />

    <link rel="shortcut icon" href="http://www.cs50.net/favicon.ico" type="image/x-icon" />

    <title>CS50 Apps / Lunch</title>
  </head>
  <body style="padding: 40px;">

    <table align="center">
      <tr>
        <td align="center" style="padding: 1.8em;">
          <form action="<?= $_SERVER["PHP_SELF"] ?>" method="get" onchange="this.submit();">
            <select name="date" size="1">
              <?
                  $friday = strtotime("2009-01-09 12:00:00");
                  $now = time();
                  while ($friday < $now + 7 * 24 * 60 * 60)
                  {
                      $date = date("Y-m-d", $friday);
                      print("<option ");
                      if ((isset($_GET["date"]) && $_GET["date"] == $date) || 
                          (!isset($_GET["date"]) && $now - $friday < 7 * 24 * 60 * 60))
                          print("selected='selected' ");
                      print("value='" . date("Y-m-d", $friday) . "'>");
                      print(date("l, j F Y", $friday));
                      print("</option>");
                      $friday += 7 * 24 * 60 * 60;
                  }
                  if (isset($_GET["date"]))
                      $date = $_GET["date"];
              ?>
            </select>
          </form>
        </td>
      </tr>
      <tr>
        <td style="border: 1px #000 solid; padding: .9em;">

          <? foreach ($dbh->query("SELECT * FROM orders WHERE date = " . $dbh->quote($date)) as $row): ?>

            <strong><?= $row["name"] ?></strong>
            <br />
            <?= $row["item"] ?>
            <? if (strlen($row["requests"]) > 0): ?>
              <br />
              <em><?= $row["requests"] ?></em>
            <? endif ?>

            <br /><br />

          <? endforeach ?>

        </td>
      </tr>
    </table>

  </body>
</html>
