<?php
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
 $query = mysqli_query($koneksi, "select nis,kelas,mapel,smt,tahun,
							AVG (IF(ki='KI3',nilai,NULL)) AS rata,
                            AVG (IF(ki='KI4',nilai,NULL)) AS rerata
							from nilai_rapor GROUP BY nis,mapel,smt,tahun");
 while ($nilai = mysqli_fetch_array($query)) {
	  $cek = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM binduk_nilai WHERE  tahun='$nilai[tahun]' AND smt='$nilai[smt]' AND mapel='$nilai[mapel]' AND kelas='$nilai[kelas]' "));
	  $cek2 = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM nilai_rapor WHERE   tahun='$nilai[tahun]' AND smt='$nilai[smt]' AND mapel='$nilai[mapel]' AND kelas='$nilai[kelas]' GROUP BY nis,mapel,smt,tahun"));
      $kkm = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM jadwal_mapel  WHERE kode='$nilai[mapel]' AND kelas='$nilai[kelas]'"));
	  $rapor=fetch($koneksi,'pengaturan',['id_setting' => 1]);
	  if($cek<>$cek2){
	
	 $exec = mysqli_query($koneksi,"INSERT INTO binduk_nilai(nis,mapel,nilai3,nilai4,kelas,smt,tahun,tapel,kkm,kelompok,nama_mapel,urut) VALUES('$nilai[nis]','$nilai[mapel]','$nilai[rata]','$nilai[rerata]','$nilai[kelas]','$nilai[smt]','$nilai[tahun]','$rapor[tp]','$kkm[kkm]','$kkm[kelompok]','$kkm[mapel]','$kkm[urut]')");
}else{
 
}  
 }
 ?>