<?
include 'protect.php';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div id = "wrapper">
<?
//show categories in dropdown
/*
CREATE TABLE IF NOT EXISTS `forum_rating` (
  `userid` int(5) NOT NULL,
  `postid` int(5) NOT NULL,
  `rating` int(5) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
//topicid=1&rat=4&postid=6
*/
$rat = $_GET['rat'];
if($rat){
	$postid = $_GET['postid'];
	$id = $_SESSION['id'];
$topicid = $dbh->prepare(" insert into forum_rating (userid,postid,rating) values ( ?,?,? )");
$topicid->execute( array( $id,$postid,$rat ));
	}


$sql = $dbh->prepare("select * from topics");
$sql->execute();
while ($carolyn  = $sql->fetch()){
	 echo '<a href = "forum.php?topicid='.$carolyn[0].'">'.$carolyn[1].'</a><br/>'."\n";
}
$topicid = $_GET['topicid'];
if ($topicid){
$submit  = $_POST['submit'];
if ($submit == 'Post Response'){
	
	$posttitle	 = $_POST['posttitle'];
		$posttext	 = $_POST['posttext'];
		$insql = $dbh->prepare("insert into posts (posttitle,posttext,topicid,uid,timeofpost)
		 values (?,?,?,?,now())");
$insql->execute(array($posttitle,$posttext,$topicid,$uid));

}


$numperpage = '2';
$start = $_GET['start'];
$page =$start * $numperpage ;
if (!$page)  $page = 0;


$getnum = $dbh->prepare("select  id  from posts  where topicid = ?");
$getnum->bindvalue(1,$topicid);
$getnum->execute();
$numresults = 0;
while ($getrow = $getnum->fetch()){
	$numresults++;
}
$numlinks = ceil($numresults/$numperpage);


//need to find number of links



$postsql = $dbh->prepare("select posts.id,posts.posttitle, posts.posttext,date_format(posts.timeofpost,' %M %d, %Y %h : %i') ,posts.uid,users.fullname
from posts,users
where posts.uid = users.id  and topicid = ? limit $page,$numperpage");




$postsql->bindValue(1,$topicid);
$postsql->execute();
while ($maruja  = $postsql->fetch()){
	$postid = $maruja[0];
	$posttitle = $maruja[1];
		$posttext = $maruja[2];
		$time = $maruja[3];
		$userid = $maruja[4];
		$fullname = $maruja[5];
echo '<h3>'.$posttitle.'</h3>';
echo '<p>'.$posttext.'</p>';
if (file_exists('userpics/'.$userid.'.jpg')){
	echo '<img src = "thumbnail.php?pic=userpics/'.$userid.'.jpg&ht=75&wd=75"/>';
}
//to attack!!!
$ratsql = $dbh->prepare("select avg(rating) from  forum_rating  where postid = ?");
$ratsql->bindValue(1,$postid);
$ratsql->execute();
$ratrow = $ratsql->fetch();
$avgrat = ceil($ratrow[0]);
for ($i=1;$i<=5;$i++ )
{
	if ($i <= $avgrat){
		echo '<a href = "forum.php?topicid='.$topicid.'&rat='.$i.'&postid='.$postid.'"><img  src = "star_dark_on.gif" /></a>';
	}
	else {
			echo '<a href = "forum.php?topicid='.$topicid.'&rat='.$i.'&postid='.$postid.'"><img src = "star_dark_off.gif"/></a>';
	}
}

echo 'Posted by '.$fullname.' at '.$time.'<br/><br/><br/>';

}
//pagination!
if ($start > 0){
$last = $start-1;
$url =  '<a href ="forum.php?topicid='.$topicid.'&start='	.$last.'">prev</a>  |';
}
for ($i = 0; $i<$numlinks;$i++){
	$j = $i+1;
$url .=  '<a href ="forum.php?topicid='.$topicid.'&start='	.$i.'">'.$j.'</a>  |';
}
if ($start < $numlinks - 1){
$next= $start+1;
$url   .=  '<a href ="forum.php?topicid='.$topicid.'&start='	.$next.'">next</a>  |';
}

echo substr($url,0,strlen($url)-1); //if you want to strip the last character
?>
<table style="width:400px;margin-left:auto;margin-right:auto">
<form action="forum.php?<?= $_SERVER['QUERY_STRING'];?> " method="post">
<tbody><tr><td colspan="2"><h2>Respond</h2></td></tr>
<tr><td>
<tr><td>*Post Title</td><td><input type="text" name="posttitle" required></td></tr>
<tr><td colspan="2">*Post Text</td></tr>
<tr><td colspan="2"><textarea name="posttext" required rows="10" cols="35"></textarea></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Post Response" ></td></tr>
  </tbody>
  </form>
</table>
<?
}
?>
</div>
</body>
</html>

