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

13512023-Junita Sinambela #94

Open
wants to merge 5 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
16 changes: 16 additions & 0 deletions backend/add_answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
include('db_connector.php');

$id = $_POST['id'];
$name = $_POST['name'];
$email = $_POST['email'];
$content = $_POST['content'];
if($id != null){
$sql = "INSERT INTO answer (id, name, email, content) VALUES ('$id', '$name', '$email', '$content')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
mysqli_close($con);
header('Location: ../question.php?id='.$id);
?>
14 changes: 14 additions & 0 deletions backend/add_question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
include('db_connector.php');

$name = $_POST['name'];
$email = $_POST['email'];
$topic = $_POST['topic'];
$content = $_POST['content'];
$sql = $sql="INSERT INTO question (name, email, topic, content) VALUES ('$name', '$email', '$topic', '$content')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
header('Location: ../index.php');
?>
27 changes: 27 additions & 0 deletions backend/ajax_answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<head>
<?php
// escape variables for security
include('db_connector.php');

$id = $_GET['id'];
$sql="SELECT * FROM answer WHERE id = '$id' ORDER BY id_answer DESC LIMIT 1";
$result = mysqli_query($con,$sql);
mysqli_close($con);
$row = mysqli_fetch_array($result);
?>
</head>
<body>
<div class="divider"></div>
<div class="answer">
<div class="content">
<a href="javascript:upvoteAnswer(<?php echo $row['id_answer'];?>)">Up</a>
<div class="votes" id="votes<?php echo $row['id_answer'];?>"><?php echo $row['votes'];?></div>
<a href="javascript:downvoteAnswer(<?php echo $row['id_answer'];?>)">Down</a>
<div class=""><?php echo $row['content'];?></div>
</div>
<div class="details">answered by <?php echo $row['name'];?> at <?php echo $row['timestamp'];?></div>
</div>
</body>
</html>

12 changes: 12 additions & 0 deletions backend/ajax_count_answers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
include('db_connector.php');

$id = $_GET['id'];
$sql = "SELECT COUNT(*) AS count FROM answer WHERE id = '$id'";
$answer = mysqli_query($con,$sql);
$count = mysqli_fetch_array($answer);
mysqli_close($con);
?>
<body>
<?php echo $count['count']." Answer";?>
</body>
27 changes: 27 additions & 0 deletions backend/ajax_vote_answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<head>
<?php
include('db_connector.php');
// escape variables for security
$id_answer = $_POST['id'];
$vote = $_POST['vote'];
$result = mysqli_query($con, "SELECT votes FROM answer WHERE id_answer = '$id_answer'");
$row = mysqli_fetch_array($result);
if($vote == 1){
$vote += $row['votes'];
if(!mysqli_query($con,"UPDATE answer SET votes = '$vote' WHERE id_answer = '$id_answer'")){
die('Error: ' . mysqli_error($con));
}
} else if($vote == -1 && $row['votes'] > 0){
$vote += $row['votes'];
if(!mysqli_query($con,"UPDATE answer SET votes = '$vote' WHERE id_answer = '$id_answer'")){
die('Error: ' . mysqli_error($con));
}
}
else $vote = $row['votes'];
mysqli_close($con);
?>
</head>
<body>
<?php echo $vote;?>
</body>
</html>
26 changes: 26 additions & 0 deletions backend/ajax_vote_question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<head>
<?php
include('db_connector.php');
// escape variables for security
$id = $_POST['id'];
$vote = $_POST['vote'];
$result = mysqli_query($con, "SELECT votes FROM question WHERE id = '$id'");
$row = mysqli_fetch_array($result);
if($vote == 1){
$vote += $row['votes'];
if(!mysqli_query($con,"UPDATE question SET votes = '$vote' WHERE id = '$id'")){
die('Error: ' . mysqli_error($con));
}
} else if($vote == -1 && $row['votes'] > 0){
$vote += $row['votes'];
if(!mysqli_query($con,"UPDATE question SET votes = '$vote' WHERE id = '$id'")){
die('Error: ' . mysqli_error($con));
}
}
else $vote = $row['votes'];
mysqli_close($con);
?>
</head>
<body>
<?php echo $vote;?>
</body>
5 changes: 5 additions & 0 deletions backend/all_questions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
include('db_connector.php');
$result = mysqli_query($con,"SELECT DISTINCT question.id, question.name, question.topic, question.content, question.timestamp, question.votes, (SELECT COUNT(answer.id) as count FROM answer WHERE answer.id = question.id) as count FROM question ORDER BY id DESC");
mysqli_close($con);
?>
7 changes: 7 additions & 0 deletions backend/answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
include('db_connector.php');
$id = $_GET['id'];
$sql = "SELECT * FROM answer WHERE id ='$id'";
$result_answer = mysqli_query($con,$sql);
mysqli_close($con);
?>
7 changes: 7 additions & 0 deletions backend/db_connector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
$con=mysqli_connect("localhost","root","","db_stackexchange");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
11 changes: 11 additions & 0 deletions backend/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
include('db_connector.php');

$id = $_GET['id'];
$sql = "DELETE FROM question WHERE id ='$id'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
header('Location: ../index.php');
?>
8 changes: 8 additions & 0 deletions backend/question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
include('db_connector.php');
$id = $_GET['id'];
$sql = "SELECT question.id, question.name, question.topic, question.content, question.timestamp, question.votes, COUNT(answer.id) AS count FROM (question LEFT JOIN answer ON question.id = answer.id) WHERE question.id ='$id'";
$result = mysqli_query($con,$sql);
$question = mysqli_fetch_array($result);
mysqli_close($con);
?>
12 changes: 12 additions & 0 deletions backend/update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
include('db_connector.php');
$id = $_POST['id'];
$topic = $_POST['topic'];
$content = $_POST['content'];
$sql = "UPDATE question SET topic = '$topic', content = '$content' WHERE id = '$id'";
if(!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
header('Location: ../question.php?id='.$id);
?>
152 changes: 152 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
html{
font-size: 20px;
}
a{
text-decoration: none;
color: black;
}
form{
text-align: center;
}
body{
margin-left: 100px;
margin-right: 100px;
}
h1{
margin-top: 30px;
text-align: center;
font-size: 40px;
}
.search{
text-align: center;
}
.yellow{
display: inline-block;
color: #E68416;
}
.name{
display: inline-block;
color: #12107E;
}
.delete{
display: inline-block;
color: #FF1515;
}
.search_form{
padding: 4px;
margin: auto;
width: 50%;
border-style: solid;
border-width: 2px;
border-color: #000000;
border-radius: 5px;
}
.button{
padding: 4px;
border-style: solid;
border-width: 2px;
border-color: #000000;
border-radius: 5px;
}
.list{
margin-left: 50px;
}
ul{
list-style-type: none;
margin-left: 0px;
}
.title{
font-size: 25px;
}
hr {
display: inline-block;
position: relative;
padding: 0;
margin: 8px auto;
height: 0;
width: 100%;
max-height: 0;
font-size: 1px;
line-height: 0;
clear: both;
border: none;
border-top: 1px solid #aaaaaa;
border-bottom: 1px solid #ffffff;
}
.votes{
display: inline-block;
width: 50px;
padding: 0px;
border: none;
margin: 25px;
text-align: center;
}
.count{
display: inline-block;
width: 50px;
padding: 0px;
border: none;
margin: 25px;
text-align: center;
}
.content{
display: inline-block;
width: 300px;
padding: 0px;
border: none;
text-align: center;
}
.credential{
position: absolute;
right: 100px;
font-size: 20px;
}
.votes-count{
display: inline-block;
}
.vote-up{
height: 30px;
text-indent: -9999em;
font-size: 1px;
display: block;
margin: 0px 2px;
width: 40px;
background-image: url("http://cdn.sstatic.net/stackoverflow/img/sprites.svg?v=a7723f5f7e59"), none;
background-size: initial;
background-repeat: no-repeat;
background-position: 0px -170px;
overflow: hidden;
}
.vote{
height: 30px;
display: block;
margin: 0px 2px;
width: 40px;
text-align: center;
}
.vote-down{
height: 30px;
text-indent: -9999em;
font-size: 1px;
display: block;
margin: 0px 2px;
width: 40px;
background-image: url("http://cdn.sstatic.net/stackoverflow/img/sprites.svg?v=a7723f5f7e59"), none;
background-size: initial;
background-repeat: no-repeat;
background-position: 0px -220px;
overflow: hidden;
}
.inputform{
text-align: center;
padding: 4px;
margin: auto;
width: 50%;
border-style: solid;
border-width: 2px;
border-color: #000000;
border-radius: 5px;
}
.new-answer{
margin: auto;
}
21 changes: 21 additions & 0 deletions edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<head>
<title>Simple StackExchange</title>
<link rel="stylesheet" href="css/style.css" />
<script src="js/validation.js"></script>
<?php include('backend/question.php');?>
</head>
<body>
<a href="index.php"><h1>Simple StackExchange</h1></a><br>

<div class="list">
<div class="title">Edit your question</div>
<hr></hr>
<form name="edit" method="post" action="backend/update.php">
<input type="hidden" name="id" value=<?php echo $question['id']?>><br>
<input class="inputform" type="text" name="topic" placeholder="Question Topic" value="<?php echo $question['topic']?>"><br>
<textarea class="inputform" name="content" placeholder="Content" value="<?php echo $question['content']?>"></textarea><br>
<input type="submit" class="button" value="Update" onclick="return validateFormEdit()">
</form>
</div>
</body>
Loading