<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
	<head>
		<title>Welcome to my site</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<!-- Font Awesome -->
		<link rel="shortcut icon" href="logo-propinsi-gorontalo.png">
		<link rel="stylesheet" href="css/font-awesome.min.css">
		<link href="style.css" rel="stylesheet" media="screen">	
		<link href="responsive.css" rel="stylesheet" media="screen">	
		<!-- Modern slider -->
		<link rel="stylesheet" href="modern-slide-in/css/sequencejs-theme.modern-slide-in.css" />
		
		<script src="grafik pie/js/jquery-min.js"></script>
			<script type="text/javascript" src="grafik pie/js/jquery.min.js"></script>
	
		
		<style>
		
		.wadah-mengetik
		{
            width: 100%;
            white-space:nowrap;
            overflow:hidden;
            -webkit-animation: ketik 5s steps(50, end);
            animation: ketik 5s steps(50, end);
		}
		@keyframes ketik{
				from { width: 0; }
		}
		@-webkit-keyframes ketik{
				from { width: 0; }
		}
	</style>
		
		<script>
		 if (typeof jQuery == 'undefined'){
		    document.write(unescape('%3Cscript src="modern-slide-in/scripts/jquery-min.js" %3E%3C/script%3E'));
		 }
		</script>
		<script src="modern-slide-in/scripts/jquery.sequence-min.js"></script>
		<script src="modern-slide-in/sequencejs-options.modern-slide-in.js"></script>
	</head>

	<body>
	
	
		<div class="header_area">
			<div class="fix header">
				<div class="logo fix floatleft">
					<a href="#"><h1>Sistem<span> Cerdas</span></h1></a>
				</div>
				<div class="mainmenu floatright">
					<ul id="nav">
						<li><a href="index.php" class="active">Home</a></li>
						<li><a href="tabel_user.php" class="active">Pengguna</a></li>
						<li><a href="tabel_gejala.php">Gejala</a></li>
						<li><a href="tabel_penyakit.php.">Penyakit</a></li>
						<li><a href="tabel_aturan.php.">Aturan</a></li>
							<li><a href="Diagnosa.php">Diagnosa</a></li>
						
						<li><a href="../logout.php">Log Out</a></li>
					</ul>
				</div>
			</div>
		</div>
		<div class="header_bottom_area"></div>
		<div class="main_content_area fix">
			<div class="content_area structure">
				<div class="single_header_title">
					<h1>About us</h1>
				</div>
				<div class="single_content_area">
					<div class="single_main_content floatleft">
						
						
						
					
						
						
						
						
						
					<H3>
	  <P align="center"><b> KONSULTASI GEJALA </b></P></H3>


	  <?php 
include_once "library/inc.connection.php";
include_once "library/inc.library.php";
$sql9 = mysql_query("TRUNCATE TABLE hasil_diagnosa");

# ID = no IP komputer, atau No HP, User ID
$ID		= str_replace(".","",$_SERVER['REMOTE_ADDR']);
$HiHasil = array();

 
 
// echo $jum;
 //echo $HiHasil;
 //exit();

if(! $_POST) {
	echo "<meta http-equiv='refresh' content='0; url=konsultasi_pilih_gejala.php'>";
}
else if(isset($_POST['btnKonsultasi'])){
   $jum  = count($dataGejala);
  // echo $jum;
   //exit();
   if ($jum==0) {
		//echo "BELUM ADA GEJALA YANG DIPILIH";
		include_once "gagal.php";
   }
   else {
		echo "<b>Gejala yang dihadapi : </b><br><br>";
		foreach ($dataGejala as $nilai) {
			$gejalaSQL = "SELECT * FROM gejala WHERE id_gejala='$nilai'";
			$gejalaQry = mysql_query($gejalaSQL) or die ("Error gejala".mysql_error());
			$gejalaHsl = mysql_fetch_array($gejalaQry);
			$kodegejala1=$gejalaHsl['id_gejala'];
			echo $gejalaHsl['id_gejala'].": ".$gejalaHsl['nama_gejala']."<br>";
			
		}
		
		
		# TAMPILKAN SEMUA DAFTAR PENYAKIT, Sekaligus membuat Rumus berulang sejumlah Penyakit
		//echo "<b> Hasil Diagnosa : </b><hr>";
		$Hi	= 1; $h = 0; $PH	= 0;
		$penyakitSql = "SELECT * FROM penyakit";
		$penyakitQry = mysql_query($penyakitSql) or die ("Error penyakit".mysql_error());
		while ($penyakitData = mysql_fetch_array($penyakitQry)){
			
			#Buat info aja
			//echo "<br> <b> $penyakitData[id_penyakit] $penyakitData[nama_penyakit]</b><br>";
		
			$np_populasi = $penyakitData['np_populasi'];
			
			// Kode untuk mengurai Gejala yang dipilih
			//$Em = 1;
			$bayes2 = 0;
			foreach ($dataGejala as $id_gejala) {
				$aturanSql  = "SELECT * FROM aturan WHERE id_penyakit='$penyakitData[id_penyakit]' AND id_gejala='$id_gejala'";
				$aturanQry  = mysql_query($aturanSql) or die ("Error program".mysql_error());
				$aturanData = mysql_fetch_array($aturanQry);
				
				
				
				$id_gejala = $aturanData['id_gejala'];
				$nilai_probabilitas = $aturanData['nilai_probabilitas'];
				
				//echo $id_gejala,$nilai_probabilitas;
				
				$bayes1=$np_populasi*$nilai_probabilitas;
				$bayes2=$bayes2+$bayes1;
				
				//echo $bayes2;
				//echo "<br>";
				}
				
				
				
				$aturanok  = "SELECT * FROM aturan WHERE id_penyakit='$penyakitData[id_penyakit]'";
				$aturanoke  = mysql_query($aturanok) or die ("Error program".mysql_error());
				$bayes4=0;
				while($aturanokee = mysql_fetch_array($aturanoke)){
				$id_gejalaok = $aturanokee['id_gejala'];
				$nilai_probabilitasok = $aturanokee['nilai_probabilitas'];
				
				//echo $nilai_probabilitasok;
				//echo "<br>";
				//echo $np_populasi;
				$bayes3=$np_populasi*$nilai_probabilitasok;
				//echo "<br>";
				//echo $bayes3;
				//echo "<br>";
				//echo $bayes4;
				$bayes4=$bayes4+$bayes3;
				//$bayes5=$bayes4-$np_populasi-$nilai_probabilitasok;
				}
				
				//echo "<br>";
				$hasilakhir=($bayes2/$bayes4);
				$hasilok=$hasilakhir*100;
				
				
				//echo "$bayes2 / $bayes5";
				//echo "<br>";
				//echo floor($hasilok)."%";
				
				$HiHasil[$penyakitData['id_penyakit']] = $hasilakhir;
				//$hasilsip[$bayes2]=$mantapok;
				
				//echo "<br>";
				
			//exit();
			
		}
		//echo "<br>";
		
		mysql_query("DELETE FROM tmp_diagnosa WHERE ID='$ID'") or die ("Error delete tmp".mysql_error());
		
		
	//$PHitung = array();
		//var_dump($HiHasil);
		foreach ($HiHasil as $kode => $hasilakhir){
			//$PHitung= calcBagi($bayes2, $bayes4);
			// Simpan konsultasi ke Temporary
			//echo $hasilakhir;	
			
			$inTmpSql = "INSERT INTO tmp_diagnosa (ID,id_penyakit,hasil_hitung)Values ('$ID','$kode','$hasilakhir')";
						 //echo $inTmpSql ;
			
			mysql_query($inTmpSql) or die ("Error inTmpSql".mysql_error());
			//echo $inTmpSql;
			}
		
	
echo "<hr>";
echo "Nilai Probabilitas Dari Gejala Yang dipilih: <br> ";

$maxSql2 = "SELECT * FROM tmp_diagnosa order by hasil_hitung = 'asc'";
		$maxQry2 = mysql_query($maxSql2) or die ("Error maxSql".mysql_error());
		
		while($maxHsl2 = mysql_fetch_array($maxQry2))
		{
		$id_penyakit2 = $maxHsl2['id_penyakit'];
		$maxHasil2 = $maxHsl2['hasil_hitung'];
		echo $id_penyakit2," = ",$maxHasil2 ,"<br>" ; 
		}


echo "<hr>";

		$maxSql = "SELECT * FROM tmp_diagnosa WHERE hasil_hitung=(SELECT MAX(hasil_hitung) FROM tmp_diagnosa WHERE ID='$ID') AND 			ID='$ID'";
		$maxQry = mysql_query($maxSql) or die ("Error maxSql".mysql_error());
		$maxHsl = mysql_fetch_array($maxQry);
		$maxHasil = $maxHsl['hasil_hitung'];
		$maxHasilok = $maxHasil*100;
		
		$penyakit2Sql = "SELECT * FROM penyakit WHERE id_penyakit='$maxHsl[id_penyakit]'";
		$penyakit2Qry = mysql_query($penyakit2Sql) or die ("Error penyakit2Sql".mysql_error());
		$penyakit2Hsl = mysql_fetch_array($penyakit2Qry);
		$nama_penyakit = $penyakit2Hsl['nama_penyakit'];
		$penyebab = $penyakit2Hsl['penyebab'];
		$pengobatan = $penyakit2Hsl['pengobatan'];
		
		echo "<br>";
		# HITUNG : STEP 7 : INTERPRETASI NILAI PROBABILITAS
		# ========================================================
		//echo "<br><b> Step 3 : </b><hr>";	
		if($maxHasil < 0) {
			$hasil = "Tidak diketahui";
		}
		elseif($maxHasil == -1) {
			$hasil = "Pasti Tidak";
		}
		elseif($maxHasil >  -1 && $maxHasil <= -0.8) {
			$hasil = "Hampir Pasti Tidak";
		}
		elseif($maxHasil >  -0.8 && $maxHasil <= -0.6) {
			$hasil = "Kemungkinan Besar Tidak";
		}
		elseif($maxHasil >  -0.6 && $maxHasil <= -0.4) {
			$hasil = "Kemungkinan Besar Tidak";
		}
		elseif($maxHasil >  -0.4 && $maxHasil <= -0.2) {
			$hasil = "Mungkin Tidak";
		}
		elseif($maxHasil >  -0.2 && $maxHasil <= -0.2) {
			$hasil = "Tidak Tahu";
		}
		elseif($maxHasil >  0.2 && $maxHasil <= 0.4) {
			$hasil = "Mungkin";
		}
		elseif($maxHasil >  0.4 && $maxHasil <= 0.7) {
			$hasil = "Kemungkinan Besar";
		}
		else if($maxHasil >  0.6 && $maxHasil <= 0.8) {
			$hasil = "Hampir Pasti";
		}
		else if($maxHasil >  0.8 && $maxHasil <= 1) {
			$hasil = "pasti";
		}
		else if($maxHasil > 1) {
			$hasil = "Tidak diketahui";
		}
		
		//echo round($zxc,2);
		//echo $cv;
		echo "Hasil Diagnosa Penyakit anda berdasarkan gejala yang dipilih adalah <b>$hasil</b>	menderita penyakit <b> $nama_penyakit </b>dengan nilai probabilitas $maxHasilok %";
		echo "<br><br>Penyebab :  $penyebab  ";
		echo "<br><br>Solusi : <br> $pengobatan  ";
		
		$query = "INSERT INTO hasil_diagnosa (nama_penyakit,maxHasilok) VALUES('$nama_penyakit','$maxHasilok')";
		$hasil = mysql_query($query);
   
		
mysql_close();
	}
}
else {
	echo "<meta http-equiv='refresh' content='0; url=?'>";
}
?>

	
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						
						</div>
					<div class="single_sidebar_area floatright">
						<div class="single_sidebar">
							<h2><span>Sistem Cerdas</span></h2>
							<img src='../images/img-1.jpg' width="350">
					<p class="wadah-mengetik" align='justify'>
                      <script language="JavaScript">
var text="Hasil Diagnosa Penyakit Berdasarkan gejala Yang dipilih Adalah <br> <h2><font color='red'><?php echo $nama_penyakit ?></h2></font><b>Penyebab :</b><?php echo $penyakit2Hsl['penyebab'] ?><br><b>Pengobatan :</b></font><?php echo $penyakit2Hsl['pengobatan'] ?>";
var delay=20;
var currentChar=1;
var destination="[none]";
function type()
{
//if (document.all)
{
var dest=document.getElementById(destination);
if (dest)// && dest.innerHTML)
{
dest.innerHTML=text.substr(0, currentChar)+"<blink>_</blink>";
currentChar++;
if (currentChar>text.length)
{
currentChar=1;
setTimeout("type()", 5000);
}
else
{
setTimeout("type()", delay);
}
}
}
}
function startTyping(textParam, delayParam, destinationParam)
{
text=textParam;
delay=delayParam;
currentChar=1;
destination=destinationParam;
type();
}
</script> <b><div  id="textDestination" margin:="" style="background-color: none;"></div></b> <script language="JavaScript">
javascript:startTyping(text, 50, "textDestination");
</script>
					</div>
				</div>
			</div>
		</div>
		<div class="fix footer_area">
			<div class="fix footer">
				<div class="fix single_footer_container">
					
					</div>
				</div>
			</div>
		</div>
	
		<script type="text/javascript" src="js/selectnav.min.js"></script>
		<script type="text/javascript">
			selectnav('nav', {
			  label: '-Navigation-',
			  nested: true,
			  indent: '-'
			});
		</script>		
	</body>
</html>
