-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsinglepost.php
More file actions
162 lines (149 loc) · 5.98 KB
/
Copy pathsinglepost.php
File metadata and controls
162 lines (149 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
session_start();
require_once 'functions/db_login.php';
// jika masuk tanpa idpost tendang
if (!isset($_GET['idpost'])) {
header('Location: index.php');
}
if (isset($_SESSION['penulis']) or isset($_SESSION['admin'])) {
// ambil data penulis
$email = $_SESSION['penulis'];
$query = "SELECT * FROM penulis WHERE email = '$email'";
$result = $conn->query($query);
if (!$result) {
die("Could not query the database: <br>" . $db->error . '<br>Query: ' . $query);
}
$row = $result->fetch_object();
$idpenulis = $row->idpenulis;
if (isset($_POST['komentar'])) {
$valid = true;
$isiKomentar = test_input($_POST['isiKomentar']);
$idpost = $_GET['idpost'];
$isiKomentar = $conn->real_escape_string($isiKomentar);
if (empty($isiKomentar)) {
echo "<script>alert('Komentar tidak boleh kosong!');</script>";
} else {
$query = " INSERT INTO komentar VALUES (NULL, $idpost, $idpenulis, '$isiKomentar', DEFAULT) ";
$result = $conn->query($query);
if (!$result) {
die("Could not query the database: <br>" . $db->error . '<br>Query: ' . $query);
} else {
echo "<script>alert('Komentar Anda berhasil ditambahkan');</script>";
}
}
}
}
$idpost = $_GET['idpost'];
$result = mysqli_query($conn, "SELECT * FROM post WHERE idpost = $idpost");
if (mysqli_num_rows($result) === 1){
$row = mysqli_fetch_assoc($result);
}
include_once 'template/meta.html';
?>
<!-- Style CSS -->
<link rel="stylesheet" href="assets/css/style.css">
<!-- Page Title -->
<title><?=$row['judul']?></title>
</head>
<?php include 'template/header.html' ?>
<!-- Jika sudah login sebagai penulis -->
<?php if (isset($_SESSION['penulis'])) { ?>
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<a href="author/dashboard.php" class="btn btn-success" role="button"><span class="fas fa-user"></span>Dashboard</a>
</li>
<li class="nav-item">
<a href="logout.php" class="btn btn-danger" style="margin-left: .5em" role="button"><span class="fas fa-sign-in-alt"></span>Logout</a>
</li>
</ul>
</div>
</nav>
<?php } else { ?>
<ul class="nav navbar-nav ml-auto">
<li class="nav-item">
<a href="register.php" class="btn btn-danger" role="button"><span class="fas fa-user-plus"></span>Register</a>
</li>
<li class="nav-item">
<a href="login.php" class="btn btn-warning" style="margin-left: .5em" role="button"><span class="fas fa-sign-in-alt"></span>Login</a>
</li>
</ul>
</div>
</nav>
<?php } ?>
<div class="container">
<?php
$idpost = $_GET["idpost"];
$post = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM post WHERE idpost=$idpost"));
?>
<br><br>
<div class="card">
<div class="card-body">
<h5 class="card-title"><?= $post["judul"] ?></h5>
<?php $idpenulis = $post["idpenulis"];
$penulis = mysqli_fetch_assoc(mysqli_query($conn, "SELECT nama FROM penulis WHERE idpenulis=$idpenulis"));
$namapenulis = $penulis["nama"] ?>
<h6 class="card-subtitle mb-2 text-muted"><small>By <?= $namapenulis ?>, <?= $post["tgl_insert"] ?><br></small></h6>
<p class="card-text"><?php echo $post["isipost"] ?><br><br></p>
<h6 class="card-text">Komentar<br><br></h6>
<?php
$comment = mysqli_query($conn, "SELECT * FROM komentar WHERE idpost=$idpost");
while ($row = mysqli_fetch_assoc($comment)) :
$idpekomen = $row['idpenulis'];
$penulis = mysqli_fetch_assoc(mysqli_query($conn, "SELECT nama FROM penulis WHERE idpenulis=$idpekomen"));
?>
<div class="row">
<div class="col">
<h7 class="card-text"><?= $penulis["nama"] ?></h7>
</div>
</div>
<div class="row mb-3">
<div class="col">
<p class="card-text"><small><?= $row["isi"] ?></small></p>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<!-- Form Komentar -->
<?php
if (isset($_SESSION['penulis'])) {
// ambil data penulis
$email = $_SESSION['penulis'];
$query = "SELECT * FROM penulis WHERE email = '$email'";
$result = $conn->query($query);
if (!$result) {
die("Could not query the database: <br>" . $db->error . '<br>Query: ' . $query);
}
$row = $result->fetch_object();
$idpenulis = $row->idpenulis;
$nama = $row->nama;
}
?>
<div class="row mt-3">
<div class="col">
<div class="card">
<div class="card-header">
<h5 class="h5">Tambahkan Komentar</h5>
</div>
<div class="card-body">
<!-- jika sudah login sebagai penulis maka dapat memberi komentar -->
<?php if (isset($_SESSION['penulis'])) : ?>
<form action="" method="post">
<div class="form-group">
<label for="isiKomentar">Komentar</label>
<textarea name="isiKomentar" class="form-control" id="isiKomentar" rows="3"></textarea>
</div>
<button type="submit" name="komentar" class="btn btn-primary">Komentari</button>
</form>
<!-- jika belum login diarahkan untuk mendaftar sebagai penulis -->
<?php else : ?>
<h5 class="h5 justify-content-center">Anda harus <a href="login.php">log in</a> terlebih dahulu untuk dapat berkomentar.</h5>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<?php
include_once 'template/footer.html';
?>