Skip to content

Commit

Permalink
Replies implementation(tested on local machine)
Browse files Browse the repository at this point in the history
Changed the icon directory(now under main directory)
To do list:
-stuff in comment.php aren't exactly aligned
  • Loading branch information
hiimdoublej committed May 31, 2016
1 parent 324c6ab commit 5e93388
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 127 deletions.
Binary file modified .DS_Store
Binary file not shown.
95 changes: 95 additions & 0 deletions comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
include_once 'header.php';
echo "<br>";
if(isset($_POST['view_reply_parent']))
{
$_SESSION['view_reply_parent'] = $_POST['view_reply_parent'];
}
$parent_id = $_SESSION['view_reply_parent'];
if(isset($_POST['submit_comment']))
{
try
{
$uid = $_SESSION['user'];
$msg = $_POST['comment'];
$time = date('Y/m/d H:i:s');
$insert = "INSERT INTO comments(cmt,uid,cmt_time,parent_id) VALUES(?,?,?,?)";
$stmt = $conn->prepare($insert);
$stmt->bindValue(1,$msg);
$stmt->bindValue(2,$uid);
$stmt->bindValue(3,$time);
$stmt->bindValue(4,$parent_id);
$stmt->execute();
?>
<script>alert('successfully sent reply');</script>
<?php
echo "<meta http-equiv='refresh' content='0'>";//refresh the page
}
catch(Exception $e)
{
die(var_dump($e));
}
}

//display the parent message
$sql_select = "SELECT msg_id,msg, username,msg_time,uid
FROM messages, users
WHERE messages.uid = users.user_id AND messages.msg_id = $parent_id";
$res=$conn->prepare($sql_select);
$res->execute();
?>
<table class = "table-fill">
<tr><th>Message</th>
<th>By</th>
<th>Time</th>
<?php
while($row=$res->fetch(PDO::FETCH_OBJ))
{
echo "<tr><td>".$row -> msg."</td>";
echo "<td>".$row -> username."</td>";
echo "<td>".$row -> msg_time."</td>";
echo "</tr>";
}
echo "</table><br>";

//display the replys to that message

$sql_select = "SELECT cmt,cmt_id,username,cmt_time
FROM comments
INNER JOIN messages ON $parent_id=comments.parent_id AND parent_id = messages.msg_id
INNER JOIN users ON users.user_id = comments.uid
ORDER BY cmt_time DESC ";
$res=$conn->prepare($sql_select);
$res->execute();
if($res->rowCount()!=0)
{
?>
<table class = "table-fill">
<tr><th>Replies</th>
<th>By</th>
<th>Time</th>
<?php
while($row=$res->fetch(PDO::FETCH_OBJ))
{
echo "<tr><td>".$row -> cmt."</td>";
echo "<td>".$row -> username."</td>";
echo "<td>".$row -> cmt_time."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "<div class='table-title'>";
echo "<h3>No replies here</h3>";
echo "</div>";
}
?>
<form action="comment.php" method="POST">
<input type="hidden" name="submit_comment" value="1" />
<?php echo "<input type='hidden' name='view_reply_parent' value=".$_SESSION['view_reply_parent'].">"; ?>
<textarea name="comment" id = 'msg'></textarea><br />
<button type="submit" id ='msg_submit'>Submit reply to this message</button>
</form>
<br>
<a href="home.php" id="back_to_home">Go Back</a>
2 changes: 2 additions & 0 deletions dbconnect.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
date_default_timezone_set("Asia/Taipei");
$user = "root";
$pwd = "Ladiesman41&";
try {
$conn = new PDO( "mysql:dbname=dbtest;host=127.0.0.1", $user, $pwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);//sql injection prevention
}
catch(Exception $e){
die(var_dump($e));
Expand Down
File renamed without changes
26 changes: 26 additions & 0 deletions header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
?>
<!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>Welcome - <?php echo $userRow['email']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>410221009 DBMS final project</label>
</div>
<div id="right">
<div id="content">
hi' <?php echo $_SESSION['username'];?>&nbsp;<a href="logout.php?logout">Sign Out</a>
</div>
</div>
</div>
60 changes: 30 additions & 30 deletions home.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php
session_start();
include_once 'dbconnect.php';
date_default_timezone_set("Asia/Taipei");

include_once 'header.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
Expand All @@ -12,7 +9,7 @@
$res=$conn->query($sql_select);
$userRow=$res->fetch(PDO::FETCH_BOTH);

if(isset($_POST["delete_action"]))
if(isset($_POST["delete_action"]))//deleting message part
{
if($_POST["delete_action"]=="delete")
{
Expand All @@ -25,9 +22,6 @@
$stmt = $conn->prepare($sql_del);
$stmt->bindValue(1,$_POST["del_msg_id"]);
$stmt->execute();
?>
<script>alert('message deleted');</script>
<?php
}
else
{
Expand All @@ -37,24 +31,6 @@
}

?>
<!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>Welcome - <?php echo $userRow['email']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>410221009 DBMS final project</label>
</div>
<div id="right">
<div id="content">
hi' <?php echo $userRow['username'];?>&nbsp;<a href="logout.php?logout">Sign Out</a>
</div>
</div>
</div>
<div class="table-title">
<h3>Bulletin Board</h3>
</div>
Expand All @@ -81,6 +57,7 @@
<th>By</th>
<th>Time</th>
<th id="actions">Delete</th>
<th>Replies</th>
<?php
while($row=$result->fetch(PDO::FETCH_OBJ))
{
Expand All @@ -95,20 +72,43 @@
<button type="submit" id="del_btn" name="delete_action" value ="delete"
style="border:0;background transparent;"
onclick="return confirm('Delete this message?')"/>
<img style ="width:30px;height:30px;"src="icons/delete.png" class = "invert" title = "Delete This Message" alt="submit" />
<img style ="width:30px;height:30px;"src="delete.png" class = "invert" title = "Delete This Message" alt="submit" />
<?php
echo "<input type='hidden' name='del_msg_id' value=".$row->msg_id.">"
?>
</form>
</a>
</td>
</tr>
<?php
}
else
{
echo "<td></td></tr>";
echo "<td></td>";
}
$sql_count = "SELECT COUNT(cmt)
FROM comments
INNER JOIN messages ON comments.parent_id = $row->msg_id AND msg_id = comments.parent_id
INNER JOIN users ON users.user_id=messages.uid
ORDER BY cmt_time DESC ";
$num_of_replies = $conn->query($sql_count);
$num_result = $num_of_replies->fetch(PDO::FETCH_NUM)[0];
if($num_result%2==1|$num_result==0)
{
echo "<td id='actions'>";
echo "<form action='comment.php' method='POST'>";
echo "<button type='submit' id='view_replies' name='view_reply_parent'
value=".$row->msg_id.">".$num_result." reply</button>";
echo "</form></td></tr>";
}
else
{
echo "<td id='actions'>";
echo "<form action='comment.php' method='POST'>";
echo "<button type='submit' id='view_replies' name='view_reply_parent'
value=".$row->msg_id.">".$num_result." replies</button>";
echo "</form></td></tr>";
}

}
echo "</table>";
}
Expand Down Expand Up @@ -141,7 +141,7 @@
?>
<script>alert('successfully sent message');</script>
<?php
echo "<meta http-equiv='refresh' content='0'>";
echo "<meta http-equiv='refresh' content='0'>";//refresh the page
}
catch(Exception $e)
{
Expand Down
Loading

0 comments on commit 5e93388

Please sign in to comment.