<?
session_start();
$hostname = 'localhost';
$username = 'mmaaaco_ksecor';
$password = 'hamilton';
$dbh = new PDO("mysql:host=$hostname;dbname=mmaaaco_ksecor", $username, $password);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?
echo 'Welcome, '. $_SESSION['fname'];
$userid = $_SESSION['userid'];
$catid = $_GET['catid'];
$postid = $_POST['postid'];
if ($postid){
$title = $_POST['title'];
$post = $_POST['post'];
$ins =$dbh->prepare("insert into 3304_posts (title,post,catid,userid,posted) values (?,?,?,?,now())");
$ins->execute(array( $title,$post,$catid,$userid));
echo "<br/>response posted!<br/>";	
}


if($catid){
	$catsql = $dbh->prepare("select title,post from 3304_posts where catid = '$catid'");
	$catsql->execute();
	$i=0;
	while ($catrow = $catsql->fetch()){
		$i++;
	}
	
	$numberofresults = $i;
	$numberperpage = 2;
	$numberlinks = ceil($numberofresults/$numberperpage);
	$start = $_GET['page'] * $numberperpage;
	if ($start == '') $start = 0;
//this is how you format in query
	$trueget= $dbh->prepare("SELECT 3304_posts.id, 3304_posts.title, 3304_posts.post, DATE_FORMAT( 3304_posts.posted,  '%M %d, %Y at %h : %i %p' ) , sp13members.uname
FROM 3304_posts, sp13members
WHERE 3304_posts.catid =  '1'
AND sp13members.id = 3304_posts.userid
LIMIT 0 , 2");
	  
	
	$trueget->execute();
while ($truerow= $trueget->fetch()){
	$postid = $truerow[0];
$postitle = $truerow[1];
$postname = $truerow[2];
$date =  $truerow[3];
$uname = $truerow[4];

echo '<h3>'.$postitle.'</h3><br/><p>'.$postname.'</p><br/>Posted by <strong>'.$uname.'</strong> at '.$date.'<br/>';
?>
<form action = "posts.php?catid=<?= $catid;?>" method="post">
Title:<input type = "text" name= "title"/><br/>
<input type =  "hidden" name = "postid" value = "<?= $postid;?>"/>
Post:<br/><textarea name = "post"></textarea><br/>
<input type = "submit" name = "submit" value="Respond"/>
</form>
<?
echo '<br/><br/><br/>';
}
	for ($y=0;$y<$numberlinks;$y++){
		$z = $y+1;
	$url .= '<a href = "posts.php?catid='.$catid.'&page='.$y .'">'.$z.'</a>  ';
	}
	echo $url;
}

?>

</body>
</html>
