Dasar Pemrograman PHP HTML
Ceklist Membuat halaman hello world Membuat tabel Membuat link / hyperlink Membuat formulir
Struktur direktori xampp File pada web dengan alamat localhost disimpan pada direktory c:/xampp/htdocs/ Buat direktori dibawah nya sehingga terdapat direktori c:/xampp/htdocs/latphp Direktori tersebut menghasilkan alamat url http://localhost/latphp
Hello world (hello1.php) http://localhost/latphp/hello1.php <html> <head> <title>hello world</title> </head> <body> Hello world </body> </html>
Tabel 1 (tabel1.php) <body> <table border=1> <tr><td>no<td>nama<td>nilai <tr><td>1<td>mardi<td>70 <tr><td>2<td>siswo<td>80 <tr><td>3<td>utomo<td>90 </table> </body>
Tabel 2 <body> <table border=1> <tr><td>no<td colspan=2>data <tr><td><td>nama<td>nilai <tr><td>1<td>mardi<td>70 <tr><td>2<td>siswo<td>80 <tr><td>3<td>utomo<td>90 </table> </body>
Tabel 3 <table border=1> <tr><td>no<td>nama<td>MK<td>nilai <tr><td rowspan=2>1<td rowspan=2>mardi<td>jarkom<td>70 <tr><td>PTI<td>90 <tr><td rowspan=2>2<td rowspan=2>utomo<td>jarkom<td>60 <tr><td>PTI<td>80 </table>
Tabel Latihan Buat tabel isian BKD hanya dengan menggunakan notepad
Sintaks PHP <html> <head> <title>hello world</title> </head> <body> <?php echo “Hello world”; ?> </body> </html>
Variabel PHP Nama variabel diawali dengan tanda $ <?php $x=5; $y=7; $z=$x * $y; echo “$z”; ?>
Jawaban Latihan <table border=1> <tr><td>x<td>y<td>z <?php $x=2; $y=3; $z=$x+$y; echo “<tr><td>$x<td>$y<td>$z”; ?> </table>
Perulangan <?php echo “<table border=1>”; for($x=0;$x<10;$x++){ echo “<tr><td>$x”; } echo “</table>”; ?>
Latihan Buat tabel perkalian 10x10
Pencabangan <?php $loop=1; while($loop<10){ echo “$loop – “; if(($loop % 2) == 0) { echo “<br>”; } $loop=$loop+1; ?>
Membuat Link <a href=‘[url]’ >[teks]</a> <a href=‘http://localhost/latphp/tabel1.php’ >Tabel1</a> <a href=‘http://localhost/latphp/tabel2.php’ >Tabel2</a>
Membuat Link Tugas: Buat link untuk masing-masing latihan yang telah dibuat. Hello1 Tabel1 Tabel2 Tabel3
Membuat Formulir (cobafrm.php) <form method=‘post’ action=‘cobaform.php’> Masukan nama : <input type=‘text’ name=‘nama’><br> Jumlah MK : <select name=‘MK’> <option value=‘1’>1</option> <option value=‘2’>2</option> <option value=‘3’>3</option> </select> <br><input type=‘submit’ value=‘simpan’> </form>
formulir Tambahkan sintak php di bagian atas formulir tadi <?php echo “Nama : $_REQUEST[nama] <br>”; echo “MK : $_REQUEST[MK]”; echo “<br><br><br>”; ?>
Membuat Formulir (cobafrm.php) <form method=‘post’ action=‘cobaform.php’> Masukan nama : <input type=‘text’ name=‘nama’ value=‘mardi siswo’><br> Jumlah MK : <select name=‘MK’> <option value=‘1’>1</option> <option value=‘2’>2</option> <option value=‘3’>3</option> </select> <br><input type=‘submit’ value=‘simpan’> </form>
Latihan buat formulir untuk penjumlahan Masukan angka 1 : [ ] Masukan angka 2 : [ ] [Hitung] Hasil penjumlahan angka1 dan angka2= ????
Jawaban <form method=‘post’ action=‘tambah.php’> Angka 1 : <input type=‘text’ name=‘angka1’><br> Angka 2 : <input type=‘text’ name=‘angka2’><br> <input type=‘submit’ value=‘hitung’> </form> <?php $hasil=$_REQUEST[angka1]+$_REQUEST[angka2]; echo “<br> hasil penjumlahan adalah $hasil”; ?>
Latihan Buat formulir isian data dosen <form method=‘post’ action=‘inpdosen.php’> <table> <tr><td>Nama<td>:<td><input type=‘text’ name=‘nama’ size=100> <tr><td>NIY<td>:<td><input type=‘text’ name=‘NIY’ size=20> ……dst </table></form>
Koneksi ke mysql <?php $db_username = "root"; $db_hostname = "localhost"; $db_password = "root"; $db_name = “unisbank"; //-------------------------------- $con = mysql_connect($db_hostname, $db_username, $db_password); $db = mysql_select_db($db_name, $con); //---------------------------------- $s="select * from dosen"; $r=mysql_query($s); While($w=mysql_fetch_array($r)){ echo "$w[NIY] - $w[Nama] <br>"; } ?>
http://latphp/sql2.php?niy=
http://latphp/sql2.php?nip=YS.2.. <?php $db_username = "root"; $db_hostname = "localhost"; $db_password = "root"; $db_name = “unisbank"; //-------------------------------- $con = mysql_connect($db_hostname, $db_username, $db_password); $db = mysql_select_db($db_name, $con); //---------------------------------- If($_REQUEST[btsimpan]==‘simpan’) { $s=“update dosen set nama=‘$_REQUEST[nama]’, alamat=‘$_REQUEST[alamat] where nip= ‘$_REQUEST[nip]’ "; $r=mysql_query($s); } $s="select Nama from dosen where nip= ‘$_REQUEST[nip]’ "; If ($w=mysql_fetch_array($r)){ echo “<form method=‘post’ action=‘sql2.php’> Masukan nip : <input type=‘text’ name=‘nip’ value=‘$w[nip]’><br> Masukan nama : <input type=‘text’ name=‘nama’ value=‘$w[Nama]’><br> <input type=‘submit’ name=‘btsimpan’ value=‘simpan’></form>”; ?>
If ($w=mysql_fetch_array($r)){ echo “<a href=‘latphp/sql2.php?nip=$w[NIP]’>$w[NIP]</a>”; }
Update [nama_tabel] set [nama_kolom1]=… , [nama_kolom2]=… where [kondisi] Contoh ; Update dosen set nama=‘mardi siswo utomo’ where niy=‘0001’