FORM
<code=php]
<?php
session_start();
include "config/koneksi.php";
//misal kita mau ambil mata kuliah di semester ganjil
/*saya rasa hanya perlu perbaikan pada $semester = "3";
variabel $semester bisa saja diperoleh dari variabel global $_GET; misal url dibentuk seperti ini:
file.php?smt=1 maka $semester = $_GET;*/
//$semester = "4";
$semester=$_GET;
echo "MATA KULIAH SEMESTER : $semester<p>";
//melakukan query ke database
$kuliah = mysql_query("select * from makul where SEMES='$semester'");
while($k = mysql_fetch_array($kuliah)){
$kode[] = $k;
$nama[] = $k;
$sks[] = $k;
}
?>
<html>
<head>
<title>Check Box</title>
<script>
<?php
echo "var jumlah = ".count($kode).";\n";
echo "var sks = new Array();\n";
//mengambil sks matakuliah dan memasukkan ke array javascript
for($j=0;$j<count($kode);$j++){
echo "sks."'] = ".$sks[$j].";\n";
}
?>
function hitungtotal(){
jum = 0;
for(i=0;i<jumlah;i++){
id = "mk"+i;
td1 = "k1"+i;
td2 = "k2"+i;
td3 = "k3"+i;
td4 = "k4"+i;
if(document.getElementById(id).checked){
kode = document.getElementById(id).value
jum = jum + sks[kode];
//untuk mengubah warna latar tabel apabila diceklist
document.getElementById(td1).style.backgroundColor = "orange";
document.getElementById(td2).style.backgroundColor = "orange";
document.getElementById(td3).style.backgroundColor = "orange";
document.getElementById(td4).style.backgroundColor = "orange";
}else {
document.getElementById(td1).style.backgroundColor = "white";
document.getElementById(td2).style.backgroundColor = "white";
document.getElementById(td3).style.backgroundColor = "white";
document.getElementById(td4).style.backgroundColor = "white";
}
}
//menampilkan total jumlah SKS yang diambil
document.getElementById("jsks").innerHTML = jum;
}
</script>
</head>
<body>
<form name="formkrs" method="POST" action="index1.php?hal=inputkrs">
<table border=1 width=400 style="border-collapse:collapse" bordercolor="#999999">
<tr><td align="center" colspan="4"><b>DAFTAR MATAKULIAH YANG DITAWARKAN</b></td></tr>
<tr><td><b>Kode</b></td>
<td><b>Nama Mata Kuliah</b></td>
<td><b>SKS</b></td>
<td><b>Ambil</b></td></tr>
<?php
//menampilkan matakuliah ke dalam tabel
for($i=0;$i<count($kode);$i++){
echo "<tr><td id=k1$i>".$kode[$i]."</td>";
echo "<td id=k2$i>".$nama[$i]."</td>";
echo "<td id=k3$i>".$sks[$i]."</td>";
echo "<td id=k4$i><input type=checkbox name=mk[] onclick=hitungtotal()
value=".$kode[$i]." id=mk".$i."></tr>";
}
?>
<tr><td colspan=3>JUMLAH YANG DIAMBIL</td><td><span id=jsks></span></td></tr>
</table>
<br/>
<input name="kirim" type="submit" value="Kirim KRS">
<!--<input name="reset">-->
</form>
</body>
</html>
[/code]
SUBMIT
<?php
include "config/koneksi.php";
//misal saat ini adalah semester ganjil tahun 2010
$nim =$_POST['NIMHS']; //misal ini adalah NIM mahasiswa yang mengambil
$semester = $_POST['SEMES'];
$mk=$_POST['KODEMKM'];
//$tahun= $_POST[TAHUN];
//$tahun = 2010;
//cek apakah sudah pernah input;
$cek = mysql_query("SELECT * FROM krs WHERE NIMHS='$nim'
AND SEMES='$semester'");
if(mysql_num_rows($cek) > 0 ){
die("sudah pernah input
<br>coba ubah nilai variabel \$nim pada file inputkrs.php");
}
foreach($_POST['mk'] as $value){
//input ke database krs
mysql_query("INSERT INTO krs VALUES('$nim',
'$mk','$semester')");
}
//Menampilkan kembali yang sudah diambil
?>
Berikut KRS yang anda ambil :
<table border=1 width=400 style="border-collapse:collapse" bordercolor="#999999">
<tr><td><b>No.</td><td><b>Kode</b></td><td><b>Nama Mata Kuliah</b></td>
<td><b>SKS</b></td></tr>
<?php
$krs = mysql_query("SELECT * FROM krs, makul WHERE NIMHS='$nim' AND krs.KDMKM = makul.KDMKM
AND krs.SEMES='$semester'");
$jum = 0;
$i = 1;
while($k = mysql_fetch_array($krs)){
echo "<tr><td>$i</td>";
echo "<td>".$k['kode']."</td>";
echo "<td>".$k['nama']."</td>";
echo "<td>".$k['sks']."</td></tr>";
$jum = $jum + $k['sks'];
$i++;
}
echo "<tr><td colspan=3>JUMLAH</td><td>$jum</td></tr>";
?>
</table>
gak masuk ke database mas, tolong kasih tau salahnya mas...
CREATE TABLE `krs` (
`idkrs` int(10) NOT NULL auto_increment,
`NIMHS` varchar(10) NOT NULL,
`KDMKM` varchar(12) NOT NULL,
`SEMES` varchar(3) NOT NULL,
`TAHUN` int(10) NOT NULL,
PRIMARY KEY (`idkrs`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE `makul` (
`KDMKM` varchar(10) NOT NULL,
`NAKMK` varchar(25) NOT NULL,
`SKSMK` int(2) NOT NULL,
`SKSTM` int(2) NOT NULL,
`SKSPR` int(2) NOT NULL,
`SKSLP` int(2) NOT NULL,
`SEMES` varchar(2) NOT NULL,
`KDWP` varchar(1) NOT NULL,
`KDPS` int(2) NOT NULL,
`KDJEN` varchar(1) NOT NULL,
`KDPT` varchar(7) NOT NULL,
`NIDN` int(10) NOT NULL,
PRIMARY KEY (`KDMKM`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;