Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13514602 - Fiqie Ulya Sidiastahta #70

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions Answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<!DOCTYPE html>
<html>
<head>
<title>Answer</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="delete.js"></script>
<script>
function validateEmail(email) {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

return re.test(email.value);
}
function validateForm() {
var x = document.forms["myForm"]["Name"].value;
if (x == null || x == "") {
alert("Name must be filled out");
return false;
}
var x = document.forms["myForm"]["Email"].value;
if (x == null || x == "") {
alert("email must be filled out");
return false;
}
else if(!validateEmail(document.getElementById("email"))) {
alert('Please enter a valid email address.');
return false;
}
var x = document.forms["myForm"]["message"].value;
if (x == null || x == "") {
alert("Content must be filled out");
return false;
}
}
function voteQuestion(id, flag){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("question-"+id ).innerHTML = xhttp.responseText;
}
}
xhttp.open("POST", "vote.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//vote-up
if(flag==0){
xhttp.send("type=question&id="+id+"&flag="+flag);
}//vote-down
else{
xhttp.send("type=question&id="+id+"&flag="+flag);
}
}
function voteAnswer(id, flag){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("answer-"+id ).innerHTML = xhttp.responseText;
}
}
xhttp.open("POST", "vote.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//vote-up
if(flag==0){
xhttp.send("type=answer&id="+id+"&flag="+flag);
}//vote-down
else{
xhttp.send("type=answer&id="+id+"&flag="+flag);
}
}

</script>
</head>
<body>
<?php
function readQuestion($id_Question){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stackexchange";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT Content, Vote, Email, Date FROM question WHERE ID_Question ='$id_Question'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="kotak">';
echo "<img class='arrow' href='#' src='triangle-up.png' onclick='voteQuestion($id_Question,0)' >";
echo "<br><br><span id='question-". $id_Question ."'>" . $row["Vote"] ."</span><br><br>";
echo "<img class='arrow' id='down' href='#' src='triangle-up.png' onclick='voteQuestion($id_Question,1)' >";
echo '</div>';
echo '<div class= "question-box">';
echo '<div class= "resQuestion">';
echo "" . $row["Content"];
echo "</div>";
echo '<div class= "infoQuestion">';
echo " asked by " . $row["Email"]. " at " . $row["Date"] . "|<a href='AskHere.php?id=".$id_Question."' id='y'> edit </a> |<a id='r' href='javascript:deletePost(\"delete_question.php?id=$id_Question\")'>delete</a>|";
echo "</div>";
echo "</div>";
}
} else {
echo "0 results";
}
mysqli_close($conn);

}

function readAnswer($id_Question){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stackexchange";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sqlans = "SELECT count(*) AS SUM FROM answer WHERE ID_Question = '$id_Question' ";
$resultans = mysqli_query($conn, $sqlans);
$rowans = mysqli_fetch_array($resultans);
echo "<div class='SubTitle'>";
echo "<h2>" . $rowans["SUM"] . " Answer</h2>";
echo "</div>";


$sql = "SELECT ID_Answer, Content, Vote, Email, Date FROM answer WHERE ID_Question ='$id_Question' ORDER BY date";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<div class="content">';
echo '<div class="kotak">';
echo "<img class='arrow' href='#' src='triangle-up.png' onclick='voteAnswer(". $row["ID_Answer"] .",0)' >";
echo "<br><br><span id='answer-". $row["ID_Answer"] ."'>" . $row["Vote"] ."</span><br><br>";
echo "<img class='arrow' href='#' id='down' src='triangle-up.png' onclick='voteAnswer(". $row["ID_Answer"] .",1)' >";
echo '</div>';

echo '<div class= "container-answer">';
echo '<div class= "resAnswer">';
echo "" . $row["Content"];
echo "</div>";
echo '<div class= "infoAnswer">';
echo " asked by " . $row["Email"]. " at " . $row["Date"];
echo "</div>";
echo "</div>";
echo "</div>";
}
} else {
echo "0 results";
}
mysqli_close($conn);

}
?>

<div class="container">
<h1>Simple StackExchange</h1>
<br><br><br>

<div class="box1">
<div class="SubTitle">
<h2>The question topic goes here </h2>
</div>

<div class="content" id="q" >
<?php readQuestion( $_GET["id"]);?>
</div>
</div>
<div class="box2">



<?php readAnswer($_GET["id"]);?>

</div>

<div class="answerform">
Your Answer
<form name="myForm" onsubmit="return validateForm()" action="add_answer.php" method="post">
<input type="text" name="Name" placeholder="Name" >
<input type="text" id="email" name="Email" placeholder="Email">
<input type="hidden" name ="id-question" value="<?php echo $_GET["id"];?>">
<textarea placeholder= "Content" name="message" ></textarea>
<input class="button" type="submit" name="Post" value="Post">
</form>
</div>
</div>
</body>
</html>
90 changes: 90 additions & 0 deletions AskHere.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!DOCTYPE html>
<html>
<head>
<title>AskHere</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script>
function validateEmail(email) {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

return re.test(email.value);
}
function validateForm() {
var x = document.forms["myForm"]["Name"].value;
if (x == null || x == "") {
alert("Name must be filled out");
return false;
}

var x = document.forms["myForm"]["Email"].value;
if (x == null || x == "") {
alert("email must be filled out");
return false;
}
else if(!validateEmail(document.getElementById("email"))) {
alert('Please enter a valid email address.');
return false;
}

var x = document.forms["myForm"]["QuestionTopic"].value;
if (x == null || x == "") {
alert("Topic must be filled out");
return false;
}

var x = document.forms["myForm"]["message"].value;
if (x == null || x == "") {
alert("Content must be filled out");
return false;
}
}
</script>
</head>
<body>
<?php

if(isset($_GET['id'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stackexchange";
$id=$_GET["id"];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT ID_Question, Topic, Content, Email, Author FROM question WHERE ID_Question = '$_GET[id]' ORDER BY Date DESC";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$name = $row["Author"];
$email = $row["Email"];
$topic = $row["Topic"];
$text = $row["Content"];
}else{
$id="-99";
$name = "";
$email = "";
$topic = "";
$text = "";
}
?>

<div class="container">
<h1>Simple StackExchange</h1>
<h2>What's your question?</h2>
<hr>
<div class="questionform">
<form name= "myForm" action="add_question.php" onsubmit="return validateForm()" method="post">
<input type="text" name="Name" placeholder="Name" value="<?php echo $name; ?>">
<input type="text" id="email" name="Email" placeholder="Email" value="<?php echo $email?>">
<input type="text" name="QuestionTopic" placeholder="Question Topic" value="<?php echo $topic?>">
<textarea placeholder= "Content" name="message"><?php echo $text?></textarea>
<input type="hidden" name="id" value="<?php echo $id?>">
<input class="button" type="submit" name="Post" value="Post">
</form>
</div>
</div>
</body>
</html>
67 changes: 67 additions & 0 deletions AskedQuestion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<script type="text/javascript" src="delete.js"></script>
<?php

function listQuestion($key){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stackexchange";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if($key== "empty"){
$sql = "SELECT ID_Question, Topic, Content, Vote, Author, Date FROM question ORDER BY Date DESC";
}else{
$sql = "SELECT ID_Question, Topic, Content, Vote, Author, Date FROM question WHERE Topic LIKE '%".$key."%' OR Content LIKE '%".$key."%' ORDER BY Date DESC";
}

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo '<div class= "asked-question">';
echo '<div class= "kotak">';
echo "" . $row["Vote"]."<br>";
echo "Vote" ;
echo "</div>";
echo '<div class= "kotak">';
$sqlans = "SELECT count(*) AS SUM FROM answer WHERE ID_Question = '$row[ID_Question]' ";
$resultans = mysqli_query($conn, $sqlans);
$rowans = mysqli_fetch_array($resultans);
echo "" . $rowans["SUM"]."<br>";
echo "Answer<br>";

echo "</div>";

echo '<div class= "container-question">';
echo '<div class= "resAnswer">';
echo '<h3><a href="Answer.php? id=' . $row["ID_Question"] . '" id=link-question>' . $row["Topic"]."</h3><br>";
if(strlen($row["Content"] )>100){
$text = substr($row["Content"], 0, 70) . "...";
}
else{
$text = $row["Content"];
}
echo $text . "</a>";
echo "</div>";
echo '<div class= "infoQuestion">';
echo "asked by<span id=blue> $row[Author] </span>| <a href='AskHere.php?id=$row[ID_Question]' id='y'> edit </a> | <a id='r' href='javascript:deletePost(\"delete_question.php?id=$row[ID_Question]\")'>delete</a>";
echo "</div>";
echo "</div>";


echo "</div>";

}
}
else{

}

}
?>
15 changes: 15 additions & 0 deletions DB_connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stackexchange";

// Create connection
$conn = mysql_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_connect_error());
}
mysql_select_db($dbname, $conn) or die("Tidak ada database yang dipilih");
//$conn->close();
?>
Loading