Postingan lainnya
Menyederhanakan script php?
Permisi, saya mau bertanya cara menyederhanakan script php.
Saya punya script seperti ini
<?php
$tbkegiatancdr = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='CENDERAWASIH'";
if(!$hasilkegiatancdr = $db->query($tbkegiatancdr)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatancdr=$hasilkegiatancdr->num_rows;
$tbkegiatangrd4 = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='GARUDA 4'";
if(!$hasilkegiatangrd4 = $db->query($tbkegiatangrd4)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatangrd4=$hasilkegiatangrd4->num_rows;
$tbkegiatangrd7 = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='GARUDA 7'";
if(!$hasilkegiatangrd7 = $db->query($tbkegiatangrd7)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatangrd7=$hasilkegiatangrd7->num_rows;
$tbkegiatanhm = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='HEMODIALISA'";
if(!$hasilkegiatanhm = $db->query($tbkegiatanhm)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatanhm=$hasilkegiatanhm->num_rows;
$tbkegiatanigd = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='IGD'";
if(!$hasilkegiatanigd = $db->query($tbkegiatanigd)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatanigd=$hasilkegiatanigd->num_rows;
$tbkegiatanibs = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='INSTALASI BEDAH SENTRAL'";
if(!$hasilkegiatanibs = $db->query($tbkegiatanibs)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatanibs=$hasilkegiatanibs->num_rows;
$tbkegiatanicu = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='INTENSIVE CARE UNIT'";
if(!$hasilkegiatanicu = $db->query($tbkegiatanicu)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatanicu=$hasilkegiatanicu->num_rows;
$tbkegiatankby = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='KAMAR BAYI'";
if(!$hasilkegiatankby = $db->query($tbkegiatankby)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatankby=$hasilkegiatankby->num_rows;
$tbkegiatanmrt = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='MERPATI'";
if(!$hasilkegiatanmrt = $db->query($tbkegiatanmrt)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatanmrt=$hasilkegiatanmrt->num_rows;
$tbkegiatannr = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='NURI'";
if(!$hasilkegiatannr = $db->query($tbkegiatannr)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatannr=$hasilkegiatannr->num_rows;
?>
apakah bisa disederhanyakan supaya tidak banyak pengulangan coding seperti diatas? saya sudah mencoba seperti ini
<?php
$tb_perawat1 = "SELECT DISTINCT tb_perawat.ruangan FROM tb_perawat WHERE ruangan!='KANIT KEPERAWATAN'";
if(!$hasilperawat1 = $db->query($tb_perawat1)){
die('There was an error running the query [' . $db->error . ']');
}
while ($perawat1=$hasilperawat1->fetch_assoc())
{
$tbkegiatan = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan='$perawat1[ruangan]'";
if(!$hasilkegiatan = $db->query($tbkegiatan)){
die('There was an error running the query [' . $db->error . ']');
}
$jumlahkegiatan[]=$hasilkegiatan->num_rows;
}
?>
tetapi masih ada yang error, Terima kasih.
Tanggapan
error apa gan?
1 Jawaban:
coba dijadikan function aja gan, dan nama ruanganya masukan ke variable, biar klo mau ngambil data sesuai ruangannya cuma ganti variable nya aja,
misalnya :
<pre> function data($namaRuangan){ $tbkegiatancdr = "SELECT * FROM tb_kegiatan JOIN tb_perawat ON tb_kegiatan.nip=tb_perawat.nip WHERE tb_perawat.ruangan=$namaRuangan"; if(!$hasilkegiatancdr = $db->query($tbkegiatancdr)){ die('There was an error running the query [' . $db->error . ']'); } }
data('CENDRAWASIH'); </pre>