<?
include '../connect.php';
//include '../protect.php';
$memberid = $_SESSION['userid'];
//must protect & connect.php
$topicid = $_REQUEST['topicid'];


$topicstuff= $dbh->prepare("SELECT 3304_topics.topicname, DATE_FORMAT( dateposted, '%M %d, %Y %h:%i %p' ) , members.fname, members.lname
FROM 3304_topics, members
WHERE members.id = 3304_topics.memberid
AND topicid =?"); 
$topicstuff->bindValue(1,$topicid);

$topicstuff->execute();
$topic = $topicstuff->fetch();
$title = $topic[0];
$time = $topic[1];
$member = $topic[2].' '.$topic[3];
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?= $title;?></title>
</head>
<body>
<h3><?= $title.'</h3><br/>Posted by '.$member.' at '.$time.'<br/>'; ?>


<?
include 'search.php';
$submit = $_POST['submit'];
if ($submit == 'Post Event')
    {
$posttext = nl2br($_POST['posttext']);
$title = $_POST['title'];
/*
CREATE TABLE IF NOT EXISTS `3304_posts` (
  `title` varchar(255) NOT NULL,
  `posttext` text NOT NULL,
  `memberid` int(5) NOT NULL,
  `dateposted` datetime NOT NULL,
  `topicid` int(5) NOT NULL,
  PRIMARY KEY  (`postid`)
)*/
$inssql = $dbh->prepare("insert into 3304_posts ( title,posttext,memberid,dateposted,topicid) values (?,?,?,now(),?)");
$inssql->execute(array($title,$posttext,$memberid,$topicid)); 
	}
$dispsql = $dbh->prepare("SELECT 3304_posts.posttext,DATE_FORMAT( 3304_posts.dateposted, '%M %d, %Y %h:%i %p' ) , members.fname, members.lname
FROM 3304_posts, members
WHERE members.id = 3304_posts.memberid
AND 3304_posts.topicid = ?");
$dispsql->bindValue(1,$topicid);
$dispsql->execute();
while ($disprow = $dispsql->fetch()){
$text = $disprow['posttext'];
$date = $disprow[1];
$member = $disprow['fname'].' '.$disprow['lname'];
  echo $text.'<br/>';
  echo 'Posted by '.$member.' at '.$date.'<br/><br/>';
  }

	
?>
<h4>Respond!</h4>
<form method="post" action="<?= $_SERVER['PHP_SELF'];?>?<?= $_SERVER['QUERY_STRING'];?>" class="blog_form">
            <p>
                <label for="title">Title: <span>*</span></label>
                <input class="inputText" type="text" id="title" name="title" />
            </p>
            <div class="clear"></div>
            <p>
                <label for="category">Category: </label>
                <input type = "hidden" name = "topicid" value = "<?= $topicid;?>"/>
            </p>
            <div class="clear"></div>
            <p>
                <label for="post">Body: <span>*</span></label>
                <textarea class="inputTextarea" cols="88" rows="6" id="post" name="posttext"></textarea>
            </p>
            <div class="clear"></div>
            <p class="submit">
            <input name="submit" type="submit" value="Post Event" class="button white">
            </p>
            </form>
<?
//add search functionality

//get the value of the topic id!
//display posts most recent limit 5
//paginate
//give users ability to respond to post
 
?>
</body>
</html>
