Membuat Artikel & Komentar di PHP dengan MySQL

Halo sobat apa kabar?? kali ini kita akan membuat aplikasi untuk menampilkan Artikel & Komentar. Pertama kita siapkan dulu tabel-tabelnya. Cekidot

tabel pengguna (bisa disesuaikan)

mysql> create table pengguna(
    -> id int(11) not null primary key auto_increment,
    -> email varchar(100) not null,
    -> nama varchar(100) not null,
    -> password varchar(32) not null);
Query OK, 0 rows affected (0,19 sec)

tabel artikel

mysql> create table artikel(
    -> id int(11) not null primary key auto_increment,
    -> idpengguna int(11) not null,
    -> judul varchar(100) not null,
    -> isi text not null,
    -> tanggal date not null,
    -> jam time not null);
Query OK, 0 rows affected (0,21 sec)

tabel komentar_artikel

mysql> create table komentar_artikel(
    -> id int(11) not null primary key auto_increment,
    -> idartikel int(11) not null,
    -> idpengguna int(11) not null,
    -> isi text not null,
    -> tanggal date not null,
    -> jam time not null);
Query OK, 0 rows affected (0,21 sec)

kemudian kita buat file-filenya dengan struktur sebagai berikut

kita mulai koding satu per satu filenya okee...

artikel.php

<h1>List of Article</h1>
<table>
	<thead>
		<th>No.</th>
		<th>Title</th>
		<th>Date Time</th>
		<th>Action</th>
	</thead>
	<?php
	$artikel = $pdo->prepare('SELECT * FROM artikel WHERE idpengguna = "'.$_SESSION['idpengguna'].'"  ORDER BY id desc');
	$artikel->execute();

	$row = $artikel->fetchAll();
	if(sizeof($row)==0){
	?>
	<tbody>
		<td colspan="4">No Article Found</td>
	</tbody>
	<?php
	}else{
		$i = 1;
		foreach ($row as $key) {
			?>
			<tbody>
				<td><?php echo $i;?></td>
				<td><?php echo $key['judul'];?></td>
				<td><?php echo $key['tanggal']." ".$key['jam'];?></td>
				<td><a href="?p=view&id=<?php echo $key['id'];?>">View</a></td>
			</tbody>
			<?php
			$i++;
		}
	}
	?>
</table>

create.php

<h1>Create Article</h1>
<form method="POST" action="admin/setarticle.php">
<table>
	<tr>
		<td>Title</td>
		<td><input type="text" required name="judul"></td>
	</tr>
	<tr>
		<td>Content</td>
		<td><textarea type="text" required name="isi"></textarea></td>
	</tr>
	<tr>
		<td><a href="?p=home">Cancel</a></td>
		<td><button type="submit">Simpan</button></td>
	</tr>
</table>
</form>

setartikel.php

<?php
include '../koneksi.php';

$artikel = $pdo->prepare('INSERT INTO artikel (`idpengguna`,`judul`,`isi`,`tanggal`,`jam`) VALUES ("'.$_SESSION['idpengguna'].'","'.$post['judul'].'","'.$post['isi'].'","'.date("Y-m-d").'","'.date("H:i:s").'")');
$artikel->execute();

header("location:../?p=home");
?>

setkomentar.php

<?php
include '../koneksi.php';

$komentar_artikel = $pdo->prepare('INSERT INTO komentar_artikel (`idartikel`,`idpengguna`,`isi`,`tanggal`,`jam`) VALUES ("'.$post['idartikel'].'","'.$_SESSION['idpengguna'].'","'.$post['isi'].'","'.date("Y-m-d").'","'.date("H:i:s").'")');
$komentar_artikel->execute();

header("location:../?p=view&id=".$post['idartikel']);
?>

view.php

<?php
	$id = htmlentities($_GET['id']);
	if(is_numeric($id)){
		$art = $pdo->prepare("SELECT * FROM artikel LEFT JOIN pengguna on pengguna.id=artikel.idpengguna WHERE artikel.id = '".$id."'");
		$art->execute();

		$row = $art->fetch(PDO::FETCH_ASSOC);
		?>
		<h1><?php echo $row['judul'];?></h1>
		<span><?php echo $row['isi'];?></span>
		<pre><?php echo $row['tanggal']." ".$row['jam'];?></pre>
		<pre><?php echo $row['nama']." (".$row['email'].")";?></pre>
		<hr>
		<strong>Komentar</strong>
		<form method="POST" action="admin/setkomentar.php">
			<table>
				<tr>
					<td><input type="hidden" value="<?php echo $id;?>" name="idartikel"><textarea name="isi"></textarea></td>
				</tr>
				<tr>
					<td><button type="submit">Send</button></td>
				</tr>
			</table>
		</form>
		<?php

		$kom = $pdo->prepare("SELECT * FROM komentar_artikel LEFT JOIN pengguna on pengguna.id=komentar_artikel.idpengguna WHERE idartikel = '".$id."' ORDER BY komentar_artikel.id desc");
		$kom->execute();
		$datarow = $kom->fetchAll();
		foreach ($datarow as $key) {
			?>
			<h3><?php echo $key['nama']." (".$key['email'].")";?></h3>
			<span><?php echo $key['isi'];?></span>
			<pre><?php echo $key['tanggal']." ".$key['jam'];?></pre>
			<hr>
			<?php
		}

	}else{
		echo "<h1>Page Not Found</h1>";
	}
?>

ceklogin.php

<?php
include '../koneksi.php';

$user = $pdo->prepare('SELECT * FROM pengguna WHERE email = "'.$post['email'].'" AND password="'.md5($post['password']).'"');
$user->execute();

$row = $user->fetch(PDO::FETCH_ASSOC);
if(!empty($row['id'])){
	$_SESSION['login'] = true;
	$_SESSION['email'] = $row['email'];
	$_SESSION['nama'] = $row['nama'];
	$_SESSION['idpengguna'] = $row['id'];
	header("location:../?p=home");
}else{
	header("location:../?p=login&err=yes");
}
?>

login.php

<?php
if(isset($_GET['err'])){
	echo "<h3>E-Mail or Password not registered</h3>";
}
?>
<h1>Login First</h1>
<form method="POST" action="auth/ceklogin.php">
<table>
	<tr>
		<td>E-Mail</td>
		<td><input type="email" required name="email"></td>
	</tr>
	<tr>
		<td>Password</td>
		<td><input type="password" required name="password"></td>
	</tr>
	<tr>
		<td><a href="index.php?p=register">Register</a></td>
		<td><button type="submit">Login</button></td>
	</tr>
</table>
</form>

logout.php

<?php
	include '../koneksi.php';
	$_SESSION['login'] = false;
	$_SESSION['email'] = "";
	$_SESSION['nama'] = "";
	$_SESSION['idpengguna'] = "";

	header("location:../");
?>

register.php

<?php
if(isset($_GET['err'])){
	echo "<h3>E-Mail already registered</h3>";
}
?>
<h1>Register</h1>
<form method="POST" action="auth/setregister.php">
<table>
	<tr>
		<td>Name</td>
		<td><input type="text" required name="nama"></td>
	</tr>
	<tr>
		<td>E-Mail</td>
		<td><input type="email" required name="email"></td>
	</tr>
	<tr>
		<td>Password</td>
		<td><input type="password" required name="password"></td>
	</tr>
	<tr>
		<td><a href="index.php?p=login">Login</a></td>
		<td><button type="submit">Register</button></td>
	</tr>
</table>
</form>

setregister.php

<?php
include '../koneksi.php';

$user = $pdo->prepare('SELECT * FROM pengguna WHERE email = "'.$post['email'].'"');
$user->execute();

$row = $user->fetch(PDO::FETCH_ASSOC);
if(empty($row['id'])){

	$user = $pdo->prepare('INSERT INTO pengguna (`email`,`nama`,`password`) VALUES ("'.$post['email'].'","'.$post['nama'].'","'.md5($post['password']).'")');
	$user->execute();
	$id = $pdo->lastInsertId();
	$_SESSION['login'] = true;
	$_SESSION['email'] = $post['email'];
	$_SESSION['nama'] = $post['nama'];
	$_SESSION['idpengguna'] = $id;
	header("location:../?p=home");
}else{
	header("location:../?p=register&err=yes");
}
?>

home.php

<?php
	$artikel = $pdo->prepare('SELECT artikel.*, artikel.id as idnya,(select count(id) from komentar_artikel where idartikel=idnya ) as jumlah FROM artikel  ORDER BY id desc');
	$artikel->execute();

	$row = $artikel->fetchAll();
	foreach ($row as $key) {
	?>
	<div>
		<h3><a href="?p=view&id=<?php echo $key['id'];?>"><?php echo $key['judul'];?></a></h3>
		<span><?php echo $key['isi'];?></span>
		<pre><?php echo $key['jumlah'];?> comments</pre>
		<pre><?php echo $key['tanggal']." ".$key['jam'];?></pre>
	</div>
	<hr>
	<?php
	}
?>

index.php

<?php
include 'koneksi.php';
if(!$_SESSION['login']){
	if($get=="register"){
		include "auth/register.php";
	}else{
		include "auth/login.php";
	}
}else{
	include 'menu.php';
	switch ($get) {
		case 'home':
			include "home.php";
			break;
		case 'login':
			include "auth/login.php";
			break;
		case 'register':
			include "auth/register.php";
			break;
		case 'artikel':
			include "admin/artikel.php";
			break;
		case 'create':
			include "admin/create.php";
			break;
		case 'view':
			include "admin/view.php";
			break;
		default:
			include 'home.php';
			break;
	}
}
?>

koneksi.php

<?php

session_start();
date_timezone_set("Asia/Jakarta");

$pdo = NULL;


$get = isset($_GET['p'])?$_GET['p']:"home";
$post = $_POST;

$dsn = "mysql:dbname=database;host=localhost;charset=utf8";
$user = "username";
$pass = "password";
try {
	$pdo = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
	echo "Koneksi gagal. Pesan : " .$e->getMessage();
}
?>

menu.php

<a href="?p=home">Home</a> | <a href="?p=create">Add Article</a> | <a href="?p=artikel">Article</a> | <a href="auth/logout.php">Logout</a>

Selamat mencoba.. wink




Thursday, 17 Sep 2015 12:33 PM Arief Setya
Sign In
Sign In to Your Account :)
Sign In or Sign Up
 
Follow Us on Twitter
Find Us on Facebook