Friday, October 14, 2011

Chapter 5 No wrong numbers


Mr.James Scott has a garage in Silbury and now he has just bought another garage in Pinhurst. Pinhurst is only five miles from Silbury, but Mr. Scott cannot get a telephone for his new garage, so he has just bought twelve pigeons. Yesterday, a pigeon carried the first message from Pinhurst to Silbury. The bird covered the distance in three minutes. Up to now, Mr.Scott has sent a great many requests for spare parts and other urgent messages from one garage to the other. In this way, he has begun his own private ‘telephone’ service.

Membuat Guestbook PHP


Buat Database >> db_belajar
Buat Nama Tabel >> tb_guestbook
Field >> 7
1. id         INT       3   auto_increment (primary key)
2. nama       VARCHAR   30
3. tgl_lhr    VARCHAR   30
4. jk         VARCHAR   10
5. agama      VARCHAR   10
6. hobi       VARCHAR   30
7. pesan      LONGTEXT

Buat folder baru dengan nama belajarguestbook, kemudian buat 11 file php yang dibawah, lalu file 11 php tersebut dimasukkan ke dalam folder belajarguestbook. File 11 php tersebut harus ditaruh dalam satu folder.
Buat file php nya :


No
Nama File Php nya
Keterangan
1
guestbook.php
Untuk tampilan halaman utama guestbooknya
2
koneksi.php
Untuk koneksi ke database (apabila tidak ada file php ini makan tidak akan bisa koneksi ke databasenya)
3
proses_input_guestbook.php
Untuk proses inputan data guestbook ke databasenya (terjadi proses inputan atau laporan bahwa data yang diinputkan sudah tersimpan), sehingga data yang telah di ketik pada halaman utama guestbook akan tersimpan ke database db_belajar dengan nama tabelnya tb_guestbook dapat di lihat di http://localhost/phpmyadmin/
Apabila tidak ada file php ini maka tidak bisa memasukkan data ke dalam databasenya
4
link.php
File ini hanya untuk membantu menampilkan atau mengelingkan ke file-file php yang lainya.
5
tampil1.php
File ini untuk menampilkan model halaman tampilan 1 guestbook yang telah diinput pada guestbook.php,
apabila pada waktu proses input berhasil tersimpan pada proses_input_guestbook.php, maka data yang diinput bisa dilihat pada halaman ini.
Apabila tidak ada file php ini maka data yang telah berhasil tetap disimpan di databasenya, tetapi data yang telah diinput tidak bisa ditampilkan (hanya tersimpan di databasenya saja)
6
tampil2.php
Sama dengan tampilan 1,  perbedaanya terdapat tools Edit dan Hapus. Edit untuk mengedit data yang telah diinput dan Hapus untuk menghapus data yang telah diinput. Tinggal menyesuaikan kebutuhannya.
7
hal_edit_guestbook.php
File ini berfungsi untuk menampilkan halaman yang akan di edit. Apabila pada tampil2.php  memilih Edit maka akan dirujukkan ke hal_edit_guestbook.php disitu akan dapat mengedit atau merubah data guestbook yang telah dibuat. Apabila file ini tidak ada maka kita tidak bisa merubah data.
8
proses_edit_guestbook.php
File ini berfungsi mengupdate data(script update data), apabila hal_edit_guestbook.php di buat tapi file proses_edit_guestbook.php tidak dibuat maka tidak bisa melaksanakan proses update.
9
proses_hapus_guestbook.php
File ini berfungsi untuk menghapus data yang telah diinput. Apabila file ini tidak ada maka tidak bisa melaksanakan proses hapus data,  sehingga pada tampil2.php jika klik hapus maka tidak bisa menghapus datanya.
10
hal_cari_guestbook.php
Apabila kita klik cari guestbook pada guestbook.php maka akan di rujukan ke hal_cari_guestbook.php. File ini fungsinya untuk mencari nama yang akan di cari, jika ingin mencari nama pada data yang telah di buat hanya ketikkan di kotak kosong nama yang akan di cari lalu klik cari.
11
hasil_cari_guestbook.php
File ini berfungsi untuk proses pencarian data, pada file php ini untuk proses pencarian nama, apabila kita masukkan nama yanga akan di cari lalu klik cari maka akan mencari nama yang sesuai yang diketik. Apabila yang di cari tidak ada maka data belum ada. Apabila nama yang dicari ditemukan akan ditampilkan.


1. guestbook.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
#Layer1 {
                position:absolute;
                left:13px;
                top:52px;
                width:313px;
                height:20px;
                z-index:1;
}
.style1 {font-size: 18px}
-->
</style>
</head>

<body>
<div id="Layer1">
  <div align="center" class="style1">GUESTBOOK</div>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form id="form1" name="form1" method="post" action="proses_input_guestbook.php">
  <table width="317" border="1">
    <tr>
      <td width="85">Nama</td>
      <td width="3">:</td>
      <td width="207"><label>&nbsp;
        <input name="nama" type="text" id="nama" />
      </label></td>
    </tr>
    <tr>
      <td>TTL</td>
      <td>:</td>
      <td><label>&nbsp;
        <input name="tgl_lahir" type="text" id="tgl_lahir" />
      </label></td>
    </tr>
    <tr>
      <td>Jenis Kelamin </td>
      <td>:</td>
      <td><label>
        <input name="jk" type="radio" value="Perempuan" />
      Perempuan
      <input name="jk" type="radio" value="Laki-Laki" />
      Laki-laki</label></td>
    </tr>
    <tr>
      <td>Agama</td>
      <td>:</td>
      <td> &nbsp;
        <label>
        <select name="agama" id="agama">
          <option>Pilih Agama</option>
          <option value="Islam">Islam</option>
          <option value="Non Islam">Non Islam</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Hobi</td>
      <td>:</td>
      <td> <label>&nbsp;
        <input name="hobi" type="text" id="hobi" />
      </label></td>
    </tr>
    <tr>
      <td>Pesan</td>
      <td>:</td>
      <td>&nbsp;
        <label>
        <textarea name="pesan" id="pesan"></textarea>
      </label></td>
    </tr>
    <tr>
      <td colspan="3"><label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="submit" name="Submit" value="Submit" />
      </label>
        <label>
        <input type="reset" name="Submit2" value="Cancel" />
      </label></td>
    </tr>
  </table>
</form>
<p><?php include "link.php";?>&nbsp;</p>
</body>
</html>


2. koneksi.php
<?php
mysql_connect ("localhost","root","");
mysql_select_db ("db_belajar");
?>


3. proses_input_guestbook.php
<?php
include"koneksi.php";
$nama=$_POST["nama"];
$tgl_lahir=$_POST["tgl_lahir"];
$jk=$_POST["jk"];
$agama=$_POST["agama"];
$hobi=$_POST["hobi"];
$pesan=$_POST["pesan"];


$input="INSERT INTO tb_guestbook (nama,tgl_lahir,jk,agama,hobi,pesan)VALUES('$nama','$tgl_lahir','$jk','$agama','$hobi','$pesan')";
$hasil=mysql_query($input);
if ($hasil)
{
                echo "Proses Input Data Berhasil<hr>";
                include "link.php";
//header(“location:tampil.php”);
//header(“location:tampil2.php”);
}
else
{
                echo "Proses Input Data Gagal";

}
?>



4. link.php
<p><a href="guestbook.php">isi guestbook</a> | <a href="tampil1.php">lihat guestbook 1</a> | <a href="tampil2.php">lihat guestbook 2</a> | <a href="hal_cari_guestbook.php">cari guestbook</a>
</p>


5. tampil1.php
<title>Lihat Guestbook</title>
<div align="center"><? include "link.php"; ?></div>
<hr>
<?php
echo "<H3>DATA TAMU</H3>";

include "koneksi.php";

$tampil="select * from tb_guestbook order by id desc";
$hasil=mysql_query($tampil);

while ($data=mysql_fetch_array($hasil))
{
echo "ID : <font color=red>$data[id]</font><BR>";
echo "Nama : <font color=red>$data[nama]</font><BR>";
echo "Tanggal Lahir : <font color=red>$data[tgl_lahir]</font><BR>";
echo "Jenis Kelamin : <font color=red>$data[jk]</font><BR>";
echo "Agama : <font color=red>$data[agama]</font><BR>";
echo "Hobi : <font color=red>$data[hobi]</font><BR>";
echo "Pesan : <font color=red>$data[pesan]</font><BR><HR>";
}
?>

6. tampil2.php
<title>Lihat Guestbook</title>
<?php
echo "<H3 ALIGN=CENTER>DATA TAMU</H3>";

include "koneksi.php";

$tampil="SELECT * FROM tb_guestbook ORDER BY id DESC";
$hasil=mysql_query($tampil);

echo "<TABLE BORDER=1 align=center>
<TR><TH>ID</TH><TH>Nama</TH>
<TH> Tanggal Lahir </TH>
<TH>Jenis Kelamin</TH><TH>Agama</TH><TH>Hobi</TH>
<TH>Pesan</TH><TH>Tools</TH></TR>";

while ($data=mysql_fetch_array($hasil))
{
echo "<TR><TD align=center>$data[id]</TD>";
echo "<TD align=center>$data[nama]</TD>";
echo "<TD align=center>$data[tgl_lahir]</TD>";
echo "<TD align=center>$data[jk]</TD>";
echo "<TD align=center>$data[agama]</TD>";
echo "<TD align=center>$data[hobi]</TD>";
echo "<TD align=center>$data[pesan]</TD>";
echo "<TD align=center> <a href='hal_edit_guestbook.php?id=$data[id]'>Edit </a> ||
                                    <a href='proses_hapus_guestbook.php?id=$data[id]'>Hapus </a> </TD></TR>";
}
echo "</TABLE><hr>";
echo "<div align=center>";
include "link.php";
echo "</div>";
?>




7.      hal_edit_guestbook.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
#Layer1 {
                position:absolute;
                left:13px;
                top:52px;
                width:313px;
                height:20px;
                z-index:1;
}
.style1 {font-size: 18px}
-->
</style>
</head>

<body>
<?php
include"koneksi.php";
$id=$_GET["id"];
$tampil="select * from tb_guestbook where id='$id'";

$hasil=mysql_query($tampil);
while ($data=mysql_fetch_array($hasil))
{
$id=$data[id];
$nama=$data[nama];
$tgl_lahir=$data[tgl_lahir];
$jk=$data[jk];
$agama=$data[agama];
$hobi=$data[hobi];
$pesan=$data[pesan];


}
?>



<div id="Layer1">
  <div align="center" class="style1">EDIT GUESTBOOK</div>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form id="form1" name="form1" method="post" action="proses_edit_guestbook.php">
  <table width="317" border="1">
    <tr>
      <td>Id</td>
      <td>:</td>
      <td>&nbsp;
        <label>
        <input name="id" type="text" id="id" value="<?php echo "$id"; ?>" readonly="1" />
        </label></td>
    </tr>
    <tr>
      <td width="85">Nama</td>
      <td width="3">:</td>
      <td width="207"><label>&nbsp;
        <input name="nama" type="text" id="nama" value="<?php echo "$nama"; ?>" />
      </label></td>
    </tr>
    <tr>
      <td>TTL</td>
      <td>:</td>
      <td><label>&nbsp;
        <input name="tgl_lahir" type="text" id="tgl_lahir" value="<?php echo "$tgl_lahir"; ?>" />
      </label></td>
    </tr>
    <tr>
      <td>Jenis Kelamin </td>
      <td>:</td>
      <td><label>
        <input name="jk" type="radio" value="Perempuan" />
      Perempuan
      <input name="jk" type="radio" value="Laki-Laki" />
      Laki-laki</label></td>
    </tr>
    <tr>
      <td>Agama</td>
      <td>:</td>
      <td> &nbsp;
        <label>
        <select name="agama" id="agama">
          <option>Pilih Agama</option>
          <option value="Islam">Islam</option>
          <option value="Non Islam">Non Islam</option>
        </select>
      </label></td>
    </tr>
    <tr>
      <td>Hobi</td>
      <td>:</td>
      <td> <label>&nbsp;
        <input name="hobi" type="text" id="hobi" value="<?php echo "$hobi"; ?>" />
      </label></td>
    </tr>
    <tr>
      <td>Pesan</td>
      <td>:</td>
      <td>&nbsp;
        <label>
        <textarea name="pesan" id="pesan"><?php echo "$pesan"; ?></textarea>
      </label></td>
    </tr>
    <tr>
      <td colspan="3"><label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="submit" name="Submit" value="Submit" />
      </label>
        <label>
        <input type="reset" name="Submit2" value="Cancel" />
      </label></td>
    </tr>
  </table>
</form>
<p><?php include "link.php";?>&nbsp;</p>
</body>
</html>


8.      proses_edit_guestbook.php
<?php
include"koneksi.php";
$id=$_POST["id"];
$nama=$_POST["nama"];
$tgl_lahir=$_POST["tgl_lahir"];
$jk=$_POST["jk"];
$agama=$_POST["agama"];
$hobi=$_POST["hobi"];
$pesan=$_POST["pesan"];

$update="update tb_guestbook set nama='$nama',tgl_lahir='$tgl_lahir',jk='$jk',agama='$agama',hobi='$hobi',pesan='$pesan'
where id='$id'";
$hasil=mysql_query($update);
if ($hasil)
{
echo "<script>alert('data berhasil update');document.location.href='tampil2.php';</script>";
}
else
{
echo "<script>alert('gagal');document.location.href='tampil2.php';</script>";
}

?>


9.      proses_hapus_guestbook.php
<?php
include"koneksi.php";
$id=$_GET["id"];
$hapus="delete from tb_guestbook where id='$id'";

$hasil=mysql_query ($hapus);
if ($hasil)
{
echo "<script>alert('data berhasil dihapus');document.location.href='tampil2.php';</script>";
}
else
{
echo "<script>alert('gagal');document.location.href='tampil2.php';</script>";
}

?>


10.  hal_cari_guestbook.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<p>&nbsp;</p>
<form id="form1" name="form1" method="post" action="hasil_cari_guestbook.php">
  Cari Nama  
  <label>
  <input name="nama" type="text" id="nama" maxlength="30" />
  </label>
  <label>
  <input type="submit" name="Submit" value="Cari"&nbsp; />
  </label>
 
</form>

<p><?php include "link.php";?></p>

</body>
</html>


11.  hasil_cari_guestbook.php
<title>Cari Nama "<? echo $nama; ?>"</title>
<?php
echo "<H3 ALIGN=CENTER>HASIL PENCARIAN</H3>";

include "koneksi.php";
$nama = $_POST['nama'];
$cari="select * from tb_guestbook where nama LIKE '%$nama%'";
$hasil=mysql_query($cari);
$jumlah=mysql_num_rows($hasil);

if ($jumlah > 0)
{
echo "Pesan yang bernama \"<font color=red><B>$nama</B></font>\" ada : $jumlah orang<BR><BR>";
echo "<TABLE BORDER=1 align=center>
<TR><TH>ID</TH><TH>Nama</TH>
<TH>Tanggal lahir</TH><TH>Jenis Kelamin</TH>
<TH>Agama</TH><TH>Hobi</TH><TH>Pesan</TH></TR>";

while ($data=mysql_fetch_array($hasil))
{
echo "<TR><TD align=center>$data[id]</TD>";
echo "<TD align=center>$data[nama]</TD>";
echo "<TD align=center>$data[tgl_lahir]</TD>";
echo "<TD align=center>$data[jk]</TD>";
echo "<TD align=center>$data[agama]</TD>";
echo "<TD align=center>$data[hobi]</TD>";
echo "<TD align=center>$data[pesan]</TD></TR>";
}
echo "</TABLE><hr>";
echo "<div align=center>";
include "link.php";
echo "</div>";
}
else
{
echo "Data dengan nama <B>$nama</B> tidak ditemukan<hr>";
echo "<div align=center>";
include "link.php";
echo "</div>";
/*echo "<script>alert('Data tidak ditemukan, Klik OK untuk melanjutkan'); document.location.href='tampil2.php';</script>\n";*/
}
?>




A Supernatural Flute