Anda di halaman 1dari 4

1. <?php 2. 3. function faktorial($n=1) { 4. $hasil = 1; 5. for($i=$n; $i >= 1; $i--) { 6. $hasil = $hasil * $i; 7. } 8. return $hasil; 9. } 10. 11.

echo "10! = ".faktorial(10); 12. 13. ?>

Factorial.php Factorial-2.php
1. <?php 2. 3. function faktorial($n=1) { 4. if ($n==1) { 5. return 1; 6. } else { 7. return $n * faktorial ($n-1); 8. } 9. } 10. 11. echo "10! = ".faktorial(10); 12. 13. ?>

Code block

<?php #koneksi mysql mysql_connect("localhost","root",""); mysql_select_db("demo"); #end koneksi function html_menu(&$strmenu="", $parent=0) { $query = "SELECT kd_menu, kd_parent, nm_menu, link FROM menu WHERE kd_parent='$parent' ORDER BY kd_menu"; //die ($query); $sql = mysql_query($query); if (mysql_num_rows($sql) > 0) { $strmenu .= '<ul>'; } // tampilkan anaknya while ($row = mysql_fetch_assoc($sql)) { $strmenu .= "<li>"; $strmenu .= sprintf("<a href='%s' title='%s'>%s</a>", $row['link'], $row['nm_menu'], $row['nm_menu']);

//panggil diri sendiri html_menu($strmenu, $row['kd_menu']); $strmenu .= "</li>"; } if (mysql_num_rows($sql) > 0) $strmenu .= '</ul>'; } $strmenu = ""; html_menu($strmenu, 0); echo $strmenu; ?>

Perintah SQL untuk membuat table dan mengisi contoh datanya.


DROP TABLE IF EXISTS `menu`; CREATE TABLE `menu` ( `kd_menu` int(5) NOT NULL, `nm_menu` varchar(50) NOT NULL, `link` varchar(50) NOT NULL, `kd_parent` int(5) NOT NULL, PRIMARY KEY (`kd_menu`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT (1, (2, (3, (4, (5, (21, (22, (23, (231, (232, (31, (32, (33, (34, (41, (42, (43, (431, (432, INTO `menu` (`kd_menu`, 'Home', '#', 0), 'Master', '#', 'Transaksi', '#', 'Laporan', '#', 'About', '#', 'Pelanggan', '#', 'Barang', '#', 'Pegawai', '#', 'Tetap', '#', 'Tidak Tetap', '#', 'Penjualan Tunai', 'Penjualan Kredit', 'Faktur', '#', 'Surat Jalan', '#', 'Stok Barang', '#', 'Pelanggan', '#', 'Penjualan', '#', 'Tunai', '#', 'Kredit', '#',

`nm_menu`, `link`, `kd_parent`) VALUES 0), 0), 0), 0), 2), 2), 2), 23), 23), '#', '#', 3), 3), 4), 4), 4), 43), 43);

3), 3),

Membuat koneksi ke database MySQL Script config.php <?php$host="localhost";$username="root";$password="";$database="mahasiswa"; $con=mysql_connect("$host","$username","$password")or die ("Tidak dapatterkoneksi");mysql_select_db("$database") or die ("Tidak dapata terkoneksi ke database");?>

Halaman Input dataFungsi dari script ini adalah untuk menyimpan data melalui form interface ke dalam database MySQLScript input.php<html><head><title>Halaman Input Data</title></head><body><form action="proses_input.php" method="post"><table align="center"width="534" border="0" cellspacing="2" cellpadding="2"><tr><td width="89"><div align="right">Nim</div></td><td width="451"><input name="nim" type="text" id="nim" size="10" /></td></tr><tr><td><div align="right">Nama</div></td><td><input name="nama" type="text" id="nama" size="25" /></td></tr> <tr><td><div align="right">Jenis Kelamin </div></td><td><select name="jkl" id="jkl"><option selected="selected">Pilih</option><option value="L">Laki-laki</option><option value="P">Perempuan</option></select></td></tr><tr><td><div align="right">Jurusan</div></td><td><select name="jurusan" id="jurusan"><option selected="selected">Jurusan</option><option value="Teknik Mesin">Teknik Mesin</option><option value="Teknik Informatika">Teknik Informatika</option></select> </td></tr><tr><td><div align="right">TTL</div></td><td><input name="tmpt_lahir" type="text" id="tmpt_lahir" /><select name="tgl_lahir" id="tgl_lahir"><option>Tanggal</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select> <select name="bln_lahir" id="bln_lahir"><option selected="selected">Bulan</option><option value="Januari">Januari</option><option value="Februari">Februari</option><option value="Maret">Maret</option><option value="April">April</option><option value="Mei">Mei</option><option value="Juni">Juni</option><option value="Juli">Juli</option><option value="Agustus">Agustus</option><option value="September">September</option><option value="Oktober">Oktober</option><option value="November">November</option><option value="Desember">Desember</option></select><select name="thn_lahir" id="thn_lahir"><option>Tahun</option><option value="1990">1990</option><option value="1991">1991</option><option value="1992">1992</option></select> </td></tr><tr><td><div align="right">Gambar</div></td><td><input name="file_gambar" type="file" id="foto" /></td></tr><tr><td></td><td><input type="submit" name="Submit" value="Simpan" /><input type="reset" name="reset" value="Batal" /></td></tr></table></form></body></html> Pada script diatas hanya sebatas tampilan saja, agar data yang di inputkan dapat tersimpan maka harusmembuat kan script untuk memproses nya.Script proses_input.php <? include("config.php"); //mengincludekan scrip koneksi ke database$sql=("insert into mhs (nama, nim, jkl, jurusan, tmpt_lahir,tgl_lahir,bln_lahir,thn_lahir)values('$_POST[nama]','$_POST[nim]','$_POST[jkl]','$_POST[jurus an]','$_POST[tmpt_lahir]','$_POST[tgl_lahir]','$_POST[bln_lahir]','$_POST[thn_lahir]')"); $result=mysql_query($sql);if($result){echo "<div align='center'>Silahkan tunggu beberapa detik ....</div>";echo "<meta http-equiv='refresh' content='5;url=http://localhost/nilai/view.php'>";}else {echo "Tidak dapat menyimpan kedalam database";}mysql_close();?>

Gambar Tampilan halaman input.php Halaman Penampilan Data (view.php) <?phpinclude ("config.php");$sql = "SELECT * FROM mhs order by id desc";$hasil = mysql_query($sql);?><!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>Halaman Penampilan Data</title></head><body><table width="772" border="1" align="center" cellpadding="1" cellspacing="0"><tr><td width="31">No</td><td width="109">Nim</td><td width="142">Nama</td><td width="89">Jenis Kelamin</td><td width="91">Jurusan</td><td width="172">Tempat Lahir </td><td width="122">Tgl Lahir </td></tr><? php$no=1;$counter=1;while ($row =mysql_fetch_array($hasil)){ ?><tr><td><?php echo $no ? ></td><td><?php echo $row ['nim']?></td><td><?php echo $row ['nama']?></td><td><?php echo $row ['jkl']?> </td><td><?php echo $row ['jurusan']?></td><td><?php echo $row ['tmpt_lahir']?></td><td><? php echo $row ['tgl_lahir']?> <?php echo $row ['bln_lahir']?> <?phpecho $row ['thn_lahir']?></td></tr><? php$no++;$counter++;}?></table>