Tuesday, December 7, 2010

Membuat komentar dengan php


Hai sobat blogger, di bawah ini adalah arikel tentang bagaimana cara membuat komentar mnggunakan php. Disini kita hanya membuat 4 php yaitu
·         komentar.php
·         addkomentar
·         viewkomentar1
·         viewkomentar2
sebelum kita membuat scriptnya pertama kita harus mebuat database dengan nama tutorial
CREATE TABLE `guestbook` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`comment` longtext NOT NULL,
`datetime` varchar(65) NOT NULL default '',


PRIMARY KEY (`id`)
Setelah itu kita buat komentar.php (script ini berisi form)
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>Test Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="GET" action="addkomentar.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name" type="text" size="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" rows="3" ></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><a href="viewkomentar1.php">View Guestbook</a> </strong></td>
</tr>
<tr>
<td><strong><a href="viewkomentar2.php">View Guestbook</a> </strong></td>
</tr>
</table>

Selanjutnya kita buat addkomentar.php

<?php
 $koneksi = mysql_connect("localhost", "root", "");
 $pilih_db = mysql_select_db("tutorial");

if ($_GET['Submit']=="Submit"){
                $name = $_GET["name"];
                $email = $_GET["email"];
                $comment = $_GET["comment"];
               
}
$datetime=date("y-m-d h:i:s"); //date time
$insert = "INSERT INTO guestbook (name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
if (!mysql_query($insert))
{
die('error: '. mysql_error());
}
echo "Successful";
echo "<BR>";
echo "<a href='viewkomentar1.php'>View guestbook</a>"; // link to view guestbook page
echo "<BR>";
echo "<a href='viewkomentar2.php'>View guestbook</a>"; // link to view guestbook page
mysql_close();
?>

Selanjutnya kita buat viewkomentar1.php ( view komentar 1 ini komentar diperlihatkan semua mulai dari awal sampai akhir)

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
</tr>
</table>
<br>

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="tutorial"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?
}
mysql_close(); //close database
?>

Selanjutnya viewkomentar2.php ( di dalam script ini komentarnya di perlihatkan sesuai dengan keinginan anda atau viewkomentar2.php ini menggunakan php paging, maksudnya yaitu komentarnya hanya diperlihatkan sesuai dengan keinginan dan komentar yang lainnya bias dilihat dengan tombol next 1 2 3 4 previous atau lebih jelasnya silahkan coba)

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="tutorial"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$dataPerPage = 4;
if(isset($_GET['page']))
{
    $noPage = $_GET['page'];
}
else $noPage = 1;
$offset = ($noPage - 1) * $dataPerPage;

$sql="SELECT * FROM $tbl_name LIMIT $offset, $dataPerPage";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>         
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?
}
$query   = "SELECT COUNT(*) AS jumData FROM guestbook";
$hasil  = mysql_query($query);
$data     = mysql_fetch_array($hasil);

$jumData = $data['jumData'];

// menentukan jumlah halaman yang muncul berdasarkan jumlah semua data

$jumPage = ceil($jumData/$dataPerPage);

// menampilkan link previous

if ($noPage > 1) echo  "<a href='".$_SERVER['PHP_SELF']."?page=".($noPage-1)."'>&lt;&lt; Prev</a>";

// memunculkan nomor halaman dan linknya

for($page = 1; $page <= $jumPage; $page++)
{
         if ((($page >= $noPage - 3) && ($page <= $noPage + 3)) || ($page == 1) || ($page == $jumPage))
         {
            if (($showPage == 1) && ($page != 2))  echo "...";
            if (($showPage != ($jumPage - 1)) && ($page == $jumPage))  echo "...";
            if ($page == $noPage) echo " <b>".$page."</b> ";
            else echo " <a href='".$_SERVER['PHP_SELF']."?page=".$page."'>".$page."</a> ";
            $showPage = $page;
         }
}

// menampilkan link next

if ($noPage < $jumPage) echo "<a href='".$_SERVER['PHP_SELF']."?page=".($noPage+1)."'>Next &gt;&gt;</a>";

?>
</p>

Sebernarnya viewkomentar.php hanya ada 1 namun karena saya ingin member tahi tentang ini jadi saya member viewkoentar.php ada 2 jadi jika anda lebih suka dengan viewkomentar1.php atau sebaliknya itu  terserah anda…..
Terima kasih

0 comments:

Post a Comment

luvne.com resepkuekeringku.com desainrumahnya.com yayasanbabysitterku.com