forked from if-itb/IF3110-01-Simple-Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
975c060
commit 345f228
Showing
12 changed files
with
854 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
function formatDateDiff($start, $end=null) { | ||
if(!($start instanceof DateTime)) { | ||
$start = new DateTime($start); | ||
} | ||
|
||
if($end === null) { | ||
$end = new DateTime(); | ||
} | ||
|
||
if(!($end instanceof DateTime)) { | ||
$end = new DateTime($start); | ||
} | ||
|
||
$interval = $end->diff($start); | ||
|
||
$format = array(); | ||
if($interval->y !== 0) { | ||
$format[] = "%y TAHUN"; | ||
} | ||
if($interval->m !== 0) { | ||
$format[] = "%m BULAN"; | ||
} | ||
if($interval->d !== 0) { | ||
$format[] = "%d HARI"; | ||
} | ||
if($interval->h !== 0) { | ||
$format[] = "%h JAM"; | ||
} | ||
if($interval->i !== 0) { | ||
$format[] = "%i MENIT"; | ||
} | ||
if($interval->s !== 0) { | ||
if(!count($format)) { | ||
return "BARU SAJA"; | ||
} else { | ||
$format[] = "%s DETIK"; | ||
} | ||
} | ||
|
||
if(count($format) > 1) { | ||
$format = array_shift($format)." dan ".array_shift($format); | ||
} else { | ||
$format = array_pop($format); | ||
} | ||
|
||
return $interval->format($format." YANG LALU"); | ||
} | ||
//creating connection to database | ||
$link = mysqli_connect('localhost', 'kevhnmay94', "", 'simpleblog'); | ||
$nama = mysqli_escape_string($link, $_POST['nama']); | ||
$email = mysqli_escape_string($link, $_POST['email']); | ||
$id = mysqli_escape_string($link, $_POST['pid']); | ||
$komentar = mysqli_escape_string($link, $_POST['komentar']); | ||
date_default_timezone_set('Asia/Jakarta'); | ||
$tanggal = date("Y-m-d H:i:s"); | ||
|
||
$query = "INSERT INTO `comments`(`pid`, `name`, `date`, `comment`, `email`) | ||
VALUES ($id,'$nama','$tanggal','$komentar','$email')"; | ||
|
||
//insert these data to database | ||
if(!mysqli_query($link, $query)) | ||
{ | ||
die('Error ' + mysqli_errno($link)); | ||
} | ||
|
||
//tampilkan data ke screen | ||
$query = "SELECT * FROM `comments` WHERE pid=$id ORDER BY date DESC"; | ||
if(!($results = mysqli_query($link, $query))) | ||
{ | ||
die('Error ' + mysqli_errno($link)); | ||
} | ||
|
||
foreach($results as $result) | ||
{ | ||
?> | ||
<li class="art-list-item"> | ||
<div class="art-list-item-title-and-time"> | ||
<h2 class="art-list-title"><?php echo $result['name']; ?></h2> | ||
<?php | ||
//formatting tanggal | ||
$date = date_create($result['date']); | ||
|
||
?> | ||
<div class="art-list-time"><?php | ||
echo formatDateDiff($date);?></div> | ||
</div> | ||
<p><?php echo nl2br($result['comment']); ?></p> | ||
</li> | ||
<?php | ||
} | ||
mysqli_close($link); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
function Validation() | ||
{ | ||
var tanggal = document.getElementById('Tanggal').value; | ||
document.getElementById('errormsg').innerHTML=""; | ||
var tanggal1 = parseDate(tanggal); | ||
if(tanggal1 == null) | ||
{ | ||
return false; | ||
} | ||
else | ||
{ | ||
var tanggal2 = new Date(); | ||
var result = tanggal1 > tanggal2; | ||
if(result) | ||
{ | ||
var tanggal_reverse = tanggal.split("-").reverse().join("-"); | ||
document.getElementById('Tanggal').value=tanggal_reverse; | ||
return true; | ||
} | ||
else | ||
{ | ||
document.getElementById('errormsg').innerHTML="*Tanggal sudah lewat. Masukkan tanggal yang valid."; | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
function parseDate(str) { | ||
var m = str.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/); | ||
var tanggal3; | ||
if(m) | ||
{ | ||
var tanggal3 = new Date(m[3],m[2]-1,m[1]-(-1)); | ||
} | ||
else | ||
{ | ||
document.getElementById("errormsg").innerHTML="*format tanggal salah. (format:dd-mm-yyyy)"; | ||
var tanggal3 = null; | ||
} | ||
return tanggal3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* function addcomment(id) | ||
{ | ||
var content = document.getElementById('ajaxcontent'); | ||
var nama = document.getElementById('nama').value; | ||
var komentar = document.getElementById('komentar').value; | ||
var email = document.getElementById('email').value; | ||
xmlhttp.onreadystatechange = function() | ||
{ | ||
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) | ||
{ | ||
content.innerHTML = xmlhttp.responseText; | ||
} | ||
}; | ||
xmlhttp.open("GET","addcomment.php?id="+id+"&nama="+nama+"&komentar="+komentar+"&email="+email,true); | ||
xmlhttp.send(); | ||
} */ | ||
|
||
function ajaxRequest(){ | ||
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE | ||
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken) | ||
for (var i=0; i<activexmodes.length; i++){ | ||
try{ | ||
return new ActiveXObject(activexmodes[i]) | ||
} | ||
catch(e){ | ||
//suppress error | ||
} | ||
} | ||
} | ||
else if (window.XMLHttpRequest) // if Mozilla, Safari etc | ||
return new XMLHttpRequest() | ||
else | ||
return false | ||
} | ||
|
||
function loadpost(id) | ||
{ | ||
var xmlhttp = new ajaxRequest(); | ||
var content = document.getElementById('ajaxcontent'); | ||
xmlhttp.onreadystatechange=function() | ||
{ | ||
if(xmlhttp.readyState==4 && xmlhttp.status==200) | ||
{ | ||
content.innerHTML=xmlhttp.responseText; | ||
} | ||
}; | ||
xmlhttp.open("GET", "getdatabase.php?id=" + id, true); | ||
xmlhttp.send(); | ||
} | ||
|
||
function validatecomment() | ||
{ | ||
var email = document.getElementById('email').value; | ||
var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
if(email != "") | ||
{ | ||
var valid = regex.test(email); | ||
var errormessage = document.getElementById('errormsg'); | ||
|
||
if(valid) | ||
{ | ||
errormessage.innerHTML=""; | ||
var xmlhttp = new ajaxRequest(); | ||
var content = document.getElementById('ajaxcontent'); | ||
var nama = encodeURIComponent(document.getElementById('nama').value); | ||
var komentar = encodeURIComponent(document.getElementById('komentar').value); | ||
var id = encodeURIComponent(document.getElementById('pid').value); | ||
console.log(id); | ||
var email2 = encodeURIComponent(email); | ||
xmlhttp.onreadystatechange = function() | ||
{ | ||
if(xmlhttp.readyState == 4 && ((xmlhttp.status == 200) || (window.location.href.indexOf("http")==-1))) | ||
{ | ||
content.innerHTML = xmlhttp.responseText; | ||
} | ||
}; | ||
|
||
xmlhttp.open("POST","addcomment.php",true); | ||
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); | ||
xmlhttp.send("nama="+nama+"&komentar="+komentar+"&email="+email+"&pid="+id); | ||
|
||
} | ||
else | ||
{ | ||
errormessage.innerHTML="Format email tidak valid"; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function hapus(id) | ||
{ | ||
var konfirmasi = confirm('Apakah anda yakin menghapus post ini?'); | ||
if(konfirmasi) | ||
{ | ||
var formObjects = document.forms['post']; | ||
var formElements = formObjects.elements['id']; | ||
formElements.value = id.substring(1); | ||
formObjects.action = "deletePost.php"; | ||
formObjects.submit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
//establish connection to database | ||
$con = mysqli_connect('localhost', 'kevhnmay94', "", 'simpleblog'); | ||
$id = mysqli_real_escape_string($con, $_POST['id']); | ||
|
||
//query for deleting entry | ||
$query = "DELETE FROM `posts` | ||
WHERE `id` = $id"; | ||
|
||
if(!mysqli_query($con, $query)) | ||
{ | ||
die('Error ' . mysqli_errno($con)); | ||
} | ||
|
||
echo "<br>entry has been deleted."; | ||
|
||
//close connection with database | ||
mysqli_close($con); | ||
header('Location: index.php'); | ||
?> |
Oops, something went wrong.