<?php

$base_href = "<base href=http://www.pbs.org/moyers/faithandreason/>";
$html_template = file ('../quote_of_week.html');

function db () {
  $link = mysql_connect ("mysql1.online.pbs.org", "wnet", "Baveidtyb1");
  if ($link && mysql_select_db ("wnet")) {
  return ($link);
  return (FALSE);
  }
}
db();

if ($posted) {
  $query = "insert into faith_and_reason_qow (name, quote_date, caption) VALUES (\"$name\",\"$quote_date\",\"$caption\")";
  // echo $query;
  mysql_query($query);
}

$getenv = "select * from faith_and_reason_env";
$data_env = mysql_db_query("wnet",$getenv);
$env = mysql_fetch_object($data_env);
$get = "select * from faith_and_reason_quotes where quote_date='$quote_date'";
$d = mysql_db_query("wnet",$get);
//$whatever = mysql_fetch_object($d);
	
if (!($whatever = mysql_fetch_object($d)) || !$quote_date) {
  $get2 = "select * from faith_and_reason_quotes order by quote_date DESC limit 1";
  $d2 = mysql_db_query("wnet",$get2);
  $whatever2 = mysql_fetch_object($d2);
  $quote_date = $whatever2->quote_date;
  $get = "select * from faith_and_reason_quotes where quote_date='$quote_date'";
  $d = mysql_db_query("wnet",$get);
  $whatever = mysql_fetch_object($d);
}

// populate template with content
while (list ($line_num, $line) = each ($html_template)) {	
  if (ereg("<!-- base href -->", $line)) {
    echo $base_href;
  }
  else if (ereg("<!-- pbs nav -->", $line)) {
      echo "<!--#include virtual=\"/includes/global.html\" -->";
  }
  else if (ereg("<!-- drop down -->", $line)) {
    echo "<form action=\"$PHP_SELF\" method=\"post\">
    <select name=\"quote_date\" style=\"width:230px\">
    <option>Select a quote</option>\n";
    $query = "SELECT quote_date, author FROM faith_and_reason_quotes order by author";
    $pull = mysql_query($query);
    while ($row=mysql_fetch_array($pull)) {
      printf("<option value=\"%s\">%s</option>\n", $row["quote_date"], $row["author"]);
    }
    echo "</select>
    <input type=\"submit\" name=\"submit\" value=\"go\"></form>";
    }
    else if (ereg("<!-- quote -->", $line)) {
      echo $whatever->quote;
    }
    else if (ereg("<!-- author -->", $line)) {
      echo "<br>-- $whatever->author";
    }
    else if (ereg("<!-- quote_date -->", $line)) {
      $line = str_replace("<!-- quote_date -->", $quote_date, $line);
      echo $line;	
    }
    else if (ereg("<!-- print captions -->", $line)) {

      $query = "SELECT name, caption FROM faith_and_reason_qow WHERE quote_date='$quote_date' and approved='Y'";
      $pull = mysql_query($query);
      while ($row=mysql_fetch_array($pull)) {
        printf("<B>Poster: %s</B><BR><b>Caption:</b> %s<BR><BR>", $row["name"], $row["caption"]);
      }
    }
    else {
    echo $line;
  }
}
?>
