Assalamualaikum Semua
Percabangan (decision-making) memungkinkan aplikasi untuk memeriksa isi suatu variabel atau hasil perhitungan ekspresi dan mengambil tindakan yang sesuai.
viewnilai.php
Bentuk IF-Else If
if(condition1){
// statement 1
}elseif(condition2){
// statement 2
}else{
// statement 3
}
// statement 4
Switch
switch(a){
case 1; // statement 1 goes here break;
case 2; // statement 2 goes here break;
case 3; // statement 3 goes here break;
default;// statement 4 goes here break;
}
// statement 5 goes here
viewhari.php
Bentuk Ternary (?:)
Coding correction : Contoh penggunaan ternary
pakaiternary.php
tanpaternary.php
Coding correction Tanpa ternary if & if else
if.php
pada if.php tidak menghasilkan uotput jika tanggal hari saat menjalankan program lebih dari tanggal 10
ifelse.php
Pakai ternary
1 aksi
$t = date ('d');
echo 'Hari ini adalah ',
($t <= 10 ? 'Awal Bulan');
viewrapot.php
Studi Kasus 5 :
Buatlah Halaman Web Seperti Dibawah Ini, Lakukan Pemrograman Untuk Menentukan Nilai Akhir Menggunakan Pemrograman Php, rumus : Nilai harian 20%, Nilai tugas 10%, Nilai UTS 30%, Nilai UAS 40%
codingan sama dengan yang diatas namun ditambah css
cssrapot
Hallo semua selamat datang kembali. Saya Muhammad Aslam atau panggil saja Mas Lam akan berbagi pengetahuan tentang PHP langsung saja tanpa banyak basa basi lagi yu urang belajar PHP
percabangan
Macam-macam Percabangan
Bentuk If (If-else, If- else if – else, Nested If)
Bentuk Switch-case
Ternary( ?: )
Bentuk If-Else
if(condition){
// statement 1 goes here
}else{
// statemant 2 goes here
}
// statement 3 goes here
Studi Kasus 1 :Buatlah Halaman Web Seperti Dibawah Ini, Lakukan Pemrograman Untuk MENGHITUNG NILAI AKHIR Menggunakan Pemrograman Php
daftarnilai.php
<!DOCTYPE html>
<html>
<head>
<title>M. Aslam S R</title>
</head>
<body>
<form id="daftarnilai" name="daftarnilai" method="post" action="viewnilai.php">
<table align="center">
<tr>
<td colspan="2" align="center"><h1>Daftar Nilai UN Siswa</h1></td>
</tr>
<tr>
<td colspan="2" align="center"><h2>SMKN 4 Bandung</h2></td>
</tr>
<tr>
<td>Nama : </td>
<td><input type="text" name="nama" id="nama" size="20"></td>
</tr>
<tr>
<td>Nilai B. Indonesia : </td>
<td><input type="text" name="bind" id="bind" size="20"></td>
</tr>
<tr>
<td>Nilai Matematika : </td>
<td><input type="text" name="mtk" id="mtk" size="20"></td>
</tr>
<tr>
<td>Nilai B. Inggris : </td>
<td><input type="text" name="bing" id="bing" size="20"></td>
</tr>
<tr>
<td>Nilai Teori Kejuruan : </td>
<td><input type="text" name="ntk" id="ntk" size="20"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="button" id="button" value="Hitung"></td>
</tr>
</table>
</form>
</body>
</html>
<html>
<head>
<title>M. Aslam S R</title>
</head>
<body>
<form id="daftarnilai" name="daftarnilai" method="post" action="viewnilai.php">
<table align="center">
<tr>
<td colspan="2" align="center"><h1>Daftar Nilai UN Siswa</h1></td>
</tr>
<tr>
<td colspan="2" align="center"><h2>SMKN 4 Bandung</h2></td>
</tr>
<tr>
<td>Nama : </td>
<td><input type="text" name="nama" id="nama" size="20"></td>
</tr>
<tr>
<td>Nilai B. Indonesia : </td>
<td><input type="text" name="bind" id="bind" size="20"></td>
</tr>
<tr>
<td>Nilai Matematika : </td>
<td><input type="text" name="mtk" id="mtk" size="20"></td>
</tr>
<tr>
<td>Nilai B. Inggris : </td>
<td><input type="text" name="bing" id="bing" size="20"></td>
</tr>
<tr>
<td>Nilai Teori Kejuruan : </td>
<td><input type="text" name="ntk" id="ntk" size="20"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="button" id="button" value="Hitung"></td>
</tr>
</table>
</form>
</body>
</html>
viewnilai.php
<!DOCTYPE html>
<html>
<head>
<title>M. Aslam S R</title>
</head>
<body>
<?php
$nama = $_POST['nama'];
$bind = (float)$_POST['bind'];
$mtk = (float)$_POST['mtk'];
$bing = (float)$_POST['bing'];
$ntk = (float)$_POST['ntk'];
$nilai = ($bind+$bing+$mtk+$ntk)/4;
echo "$nama".'<br/>';
echo "Nilai Rata Rata anda $nilai".'<br/>';
?>
</body>
</html>
<html>
<head>
<title>M. Aslam S R</title>
</head>
<body>
<?php
$nama = $_POST['nama'];
$bind = (float)$_POST['bind'];
$mtk = (float)$_POST['mtk'];
$bing = (float)$_POST['bing'];
$ntk = (float)$_POST['ntk'];
$nilai = ($bind+$bing+$mtk+$ntk)/4;
echo "$nama".'<br/>';
echo "Nilai Rata Rata anda $nilai".'<br/>';
?>
</body>
</html>
Bentuk IF-Else If
if(condition1){
// statement 1
}elseif(condition2){
// statement 2
}else{
// statement 3
}
// statement 4
Ada 3 kemungkinan aliran program :
Apabila condition 1 bernilai true :
1. Statement 1 dijalankan
2. Statement 4 dijalankan
Apabila condition 1 bernilai false, dan condition 2 bernilai true :
1. Statement 2 dijalankan
2. Statement 4 dijalankan
Apabila condition 1 dan condition 2 bernilai false :
1. Statement 3 dijalankan
2. Statement 4 dijalankan
Nested IF( If Bersarang)
if(condition){
if(condition1){
//statement 1 goes here
}else{
// statemant 2 goes here
}
}else{
// statemant 3 goes here
}
contoh
menentukanhari.php
<?php
$hari ="minggu";
$tanggal = 2;
if ($hari == "minggu") {
if ($tanggal<10) {
echo "selamat berakhir pekan pada tanggal muda :d";
}
else{
echo "selamat berakhirpekan pada tanggal tua dan tetap semangat :d";
}
}
else{
echo "semoga hari anda menyenagkan";
echo "sampai jumpa di akhir pekan";
}
?>
$hari ="minggu";
$tanggal = 2;
if ($hari == "minggu") {
if ($tanggal<10) {
echo "selamat berakhir pekan pada tanggal muda :d";
}
else{
echo "selamat berakhirpekan pada tanggal tua dan tetap semangat :d";
}
}
else{
echo "semoga hari anda menyenagkan";
echo "sampai jumpa di akhir pekan";
}
?>
Switch
switch(a){
case 1; // statement 1 goes here break;
case 2; // statement 2 goes here break;
case 3; // statement 3 goes here break;
default;// statement 4 goes here break;
}
// statement 5 goes here
Studi Kasus 2 :Buatlah Halaman Web Seperti Dibawah Ini, Lakukan Pemrograman Untuk Menentukan Jumlah Hari Pada Setiap Bulan Menggunakan Pemrograman Php
menghitunghari.php
<!DOCTYPE html>
<html>
<head>
<title>M.Aslam S R</title>
</head>
<body>
<form id="hitunghari" name="hitunghari" method="post" action="viewhari.php">
<table>
<tr>
<td colspan="3"><H1>Menghitung jumlah hari</H1></td>
</tr>
<tr>
<td colspan="3">Isi dengan angka saja</td>
</tr>
<tr>
<td>Bulan <input type="text" name="bulan"></td>
<td>Tahun <input type="text" name="tahun"></td>
<td><input type="submit" name="button" id="button" value="Hitung"></td>
</tr>
</table>
</form>
</body>
</html>
<html>
<head>
<title>M.Aslam S R</title>
</head>
<body>
<form id="hitunghari" name="hitunghari" method="post" action="viewhari.php">
<table>
<tr>
<td colspan="3"><H1>Menghitung jumlah hari</H1></td>
</tr>
<tr>
<td colspan="3">Isi dengan angka saja</td>
</tr>
<tr>
<td>Bulan <input type="text" name="bulan"></td>
<td>Tahun <input type="text" name="tahun"></td>
<td><input type="submit" name="button" id="button" value="Hitung"></td>
</tr>
</table>
</form>
</body>
</html>
viewhari.php
<?php
$bulan = (int)$_POST['bulan'];
$tahun = (int)$_POST['tahun'];
switch ($bulan) {
case 1:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 2:
echo " banyak hari pada bulan ini adalah : 29";
break;
case 3:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 4:
echo " banyak hari pada bulan ini adalah : 30";
break;
case 5:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 6:
echo " banyak hari pada bulan ini adalah : 30";
break;
case 7:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 8:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 9:
echo " banyak hari pada bulan ini adalah : 30";
break;
case 10:
echo " banyak hari pada bulan ini adalah k :31 ";
break;
case 11:
echo " banyak hari pada bulan ini adalah k :30 ";
break;
case 12:
echo " banyak hari pada bulan ini adalah k :31 ";
break;
default:
echo "Harap input bulan dengan angka";
break;
}
?>
$bulan = (int)$_POST['bulan'];
$tahun = (int)$_POST['tahun'];
switch ($bulan) {
case 1:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 2:
echo " banyak hari pada bulan ini adalah : 29";
break;
case 3:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 4:
echo " banyak hari pada bulan ini adalah : 30";
break;
case 5:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 6:
echo " banyak hari pada bulan ini adalah : 30";
break;
case 7:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 8:
echo " banyak hari pada bulan ini adalah : 31";
break;
case 9:
echo " banyak hari pada bulan ini adalah : 30";
break;
case 10:
echo " banyak hari pada bulan ini adalah k :31 ";
break;
case 11:
echo " banyak hari pada bulan ini adalah k :30 ";
break;
case 12:
echo " banyak hari pada bulan ini adalah k :31 ";
break;
default:
echo "Harap input bulan dengan angka";
break;
}
?>
Bentuk Ternary (?:)
<?php
$var = 5;
$hasil = ($var%2 == 0 ? "genap" : "ganjil");
echo "angka $var adalah bilangan $hasil";
?>
$var = 5;
$hasil = ($var%2 == 0 ? "genap" : "ganjil");
echo "angka $var adalah bilangan $hasil";
?>
Coding correction : Contoh penggunaan ternary
pakaiternary.php
<?php
$nilai = 85;
$hasil = ($nilai > 70 ? "baik" : "buruk");
echo "hasilnya adalah $hasil dengan bilangan $nilai";
//hasil baik
?>
$nilai = 85;
$hasil = ($nilai > 70 ? "baik" : "buruk");
echo "hasilnya adalah $hasil dengan bilangan $nilai";
//hasil baik
?>
tanpaternary.php
<?php
$nilai = 85;
if ($nilai > 70) {
echo 'baik';
}
else
{
echo 'buruk';
}
?>
$nilai = 85;
if ($nilai > 70) {
echo 'baik';
}
else
{
echo 'buruk';
}
?>
Coding correction Tanpa ternary if & if else
if.php
<?php
$t=date("d");
if ($t<="10") {
echo "awal bulan";
}
?>
$t=date("d");
if ($t<="10") {
echo "awal bulan";
}
?>
ifelse.php
<?php
$d= date("d");
if ($d<="10") {
echo "awal bulan";
}
else
{
echo "sudah lewat awal bulan";
}
?>
$d= date("d");
if ($d<="10") {
echo "awal bulan";
}
else
{
echo "sudah lewat awal bulan";
}
?>
Pakai ternary
1 aksi
$t = date ('d');
echo 'Hari ini adalah ',
($t <= 10 ? 'Awal Bulan');
2 aksi
$t = date ('d');
echo 'Hari ini adalah ',
($t <= 10 ? 'Awal Bulan' :
'Sudah Lewat Awal Bulan');
3 aksi
$t = date ('d');
echo 'Hari ini adalah ',
($t <= 10 ? 'Awal Bulan' :
($t <= 20 ? 'Pertengahan bulan' :‘
Akhir Bulan'));
Studi Kasus 4
Buatlah halaman web untuk menentukan Nilai Rapot dan Index.
Komponen Penilaian Tugas 20%, Ulangan Harian 10%, UTS 30%, UAS 40%.
Input : Nama, Nilai Tugas, Nilai Ulangan Harian, UTS, UAS
Output : Nama : Jon
Nilai Tugas : 75
Nilai Ulangan Harian : 90
Nilai Uts : 80
Nilai Uas : 80
Nilai Akhir : 80
Index : A
A : 80-100 :Hijau
B : 70-79 :Biru
C : 60-69 :Kuning
D : 50-59 :Orange
E : < 50 :Merah
nilairapot.php
<!DOCTYPE html>
<html>
<head>
<title>M. Aslam S R</title>
<link rel="stylesheet" href="cssrapot.css" type="text/css">
</head>
<body>
<form id="daftarnilai" name="daftarnilai" method="post" action="viewrapot.php">
<table align="center">
<tr>
<td colspan="2" align="center"><h1>Daftar Nilai Siswa</h1></td>
</tr>
<tr>
<td colspan="2" align="center"><h2>SMKN 4 Bandung</h2></td>
</tr>
<tr>
<td>Nama : </td>
<td><input type="text" name="nama" id="nama" size="20"></td>
</tr>
<tr>
<td>Nilai Tugas : </td>
<td><input type="text" name="tugas" id="tugas" size="20"></td>
</tr>
<tr>
<td>Nilai Ulangan Harian : </td>
<td><input type="text" name="ul" id="ul" size="20"></td>
</tr>
<tr>
<td>Nilai UTS : </td>
<td><input type="text" name="uts" id="uts" size="20"></td>
</tr>
<tr>
<td>Nilai UAS : </td>
<td><input type="text" name="uas" id="uas" size="20"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="button" id="button" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>
<html>
<head>
<title>M. Aslam S R</title>
<link rel="stylesheet" href="cssrapot.css" type="text/css">
</head>
<body>
<form id="daftarnilai" name="daftarnilai" method="post" action="viewrapot.php">
<table align="center">
<tr>
<td colspan="2" align="center"><h1>Daftar Nilai Siswa</h1></td>
</tr>
<tr>
<td colspan="2" align="center"><h2>SMKN 4 Bandung</h2></td>
</tr>
<tr>
<td>Nama : </td>
<td><input type="text" name="nama" id="nama" size="20"></td>
</tr>
<tr>
<td>Nilai Tugas : </td>
<td><input type="text" name="tugas" id="tugas" size="20"></td>
</tr>
<tr>
<td>Nilai Ulangan Harian : </td>
<td><input type="text" name="ul" id="ul" size="20"></td>
</tr>
<tr>
<td>Nilai UTS : </td>
<td><input type="text" name="uts" id="uts" size="20"></td>
</tr>
<tr>
<td>Nilai UAS : </td>
<td><input type="text" name="uas" id="uas" size="20"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="button" id="button" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>
viewrapot.php
<?php
$nama = $_POST['nama'];
$tugas = (float)$_POST['tugas'];
$ul = (float)$_POST['ul'];
$uts = (float)$_POST['uts'];
$uas = (float)$_POST['uas'];
$nilaiakhir = ($tugas*0.2)+($ul*0.1)+($uts*0.3)+($uas*0.4);
$hasil = ($nilaiakhir >= 80 ? 'A' : ($nilaiakhir >= 70 ? 'B' : ($nilaiakhir >= 60 ? 'C' : ($nilaiakhir >= 50 ? 'D' : 'E')) ));
echo "Nama : $nama".'<br/>';
echo "Nilai Tugas : $tugas".'<br/>';
echo "Nilai ulangan harian : $ul".'<br/>';
echo "Nilai UTS : $uts".'<br/>';
echo "Nilai UAS : $uas".'<br/>';
echo "Nilai Akhir : $nilaiakhir".'<br/>';
if ($nilaiakhir >= 80) {
echo "Index : <font color='green'>$hasil</font>";
}
else if($nilaiakhir >= 70 || $hasil <= 79){
echo "Index : <font color='blue'>$hasil</font>";
}
else if ($nilaiakhir >= 60 || $hasil <= 69) {
echo "Index : <font color='yellow'>$hasil</font>";
}
else if ($nilaiakhir >= 50 || $hasil <= 59) {
echo "Index : <font color='orange'>$hasil</font>";
}
else if ($nilaiakhir < 50) {
echo "Index : <font color='red'>$hasil</font>";
}
else{
echo "Error";
}
?>
$nama = $_POST['nama'];
$tugas = (float)$_POST['tugas'];
$ul = (float)$_POST['ul'];
$uts = (float)$_POST['uts'];
$uas = (float)$_POST['uas'];
$nilaiakhir = ($tugas*0.2)+($ul*0.1)+($uts*0.3)+($uas*0.4);
$hasil = ($nilaiakhir >= 80 ? 'A' : ($nilaiakhir >= 70 ? 'B' : ($nilaiakhir >= 60 ? 'C' : ($nilaiakhir >= 50 ? 'D' : 'E')) ));
echo "Nama : $nama".'<br/>';
echo "Nilai Tugas : $tugas".'<br/>';
echo "Nilai ulangan harian : $ul".'<br/>';
echo "Nilai UTS : $uts".'<br/>';
echo "Nilai UAS : $uas".'<br/>';
echo "Nilai Akhir : $nilaiakhir".'<br/>';
if ($nilaiakhir >= 80) {
echo "Index : <font color='green'>$hasil</font>";
}
else if($nilaiakhir >= 70 || $hasil <= 79){
echo "Index : <font color='blue'>$hasil</font>";
}
else if ($nilaiakhir >= 60 || $hasil <= 69) {
echo "Index : <font color='yellow'>$hasil</font>";
}
else if ($nilaiakhir >= 50 || $hasil <= 59) {
echo "Index : <font color='orange'>$hasil</font>";
}
else if ($nilaiakhir < 50) {
echo "Index : <font color='red'>$hasil</font>";
}
else{
echo "Error";
}
?>
Studi Kasus 5 :
Buatlah Halaman Web Seperti Dibawah Ini, Lakukan Pemrograman Untuk Menentukan Nilai Akhir Menggunakan Pemrograman Php, rumus : Nilai harian 20%, Nilai tugas 10%, Nilai UTS 30%, Nilai UAS 40%
codingan sama dengan yang diatas namun ditambah css
cssrapot
body{
background-color: gray;
}
table{
border: dotted green;
background-color: white;
}
background-color: gray;
}
table{
border: dotted green;
background-color: white;
}
Comments
Post a Comment