Read 563 times | Created 2014-04-12 20:04:57 | Updated 2014-05-22 22:05:28 | | |

 

[FILE :config.php]

<?php
/*********************************
FILENAME    : config.php
CREATE BY   : cahya dsn
PURPOSE     : database configuration
CREATE DATE : 2013-01-20
**********************************/
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='expert';
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);

[CONTOH SCHEMA DATABASE]
/*
--
-- Database: `expert`
--

-- --------------------------------------------------------

--
-- Table structure for table `tbl_symptoms`
--

DROP TABLE IF EXISTS `tbl_symptom`;
CREATE TABLE IF NOT EXISTS `tbl_symptoms` (
  `code` char(4) NOT NULL,
  `symptom` varchar(100) NOT NULL,
  PRIMARY KEY (`code`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Dumping data for table `tbl_symptoms`
--

INSERT INTO `tbl_symptoms` (`code`, `symptom`) VALUES
('G001', 'Demam mendadak tinggi (38-40 C)'),
('G002', 'Demam tiba-tiba turun'),
('G003', 'Kebocoran pembuluh darah'),
('G004', 'Sakit kepala berat'),
('G005', 'Nyeri otot dan ruam'),
('G006', 'Sakit perut (mual dan muntah)'),
('G007', 'Terjadi syok'),
('G008', 'Nafas terengah-engah'),
('G009', 'Detak jantung cepat tapi lemah'),
('G010', 'Kulit dingin dan kebiruan'),
('G011', 'Perubahan mental seperti tampak linglung'),
('G012', 'Menggigil'),
('G013', 'Sakit tulang'),
('G014', 'Batuk'),
('G015', 'Telapak tangan yang terasa dingin dan lembab'),
('G016', 'Organ hati sering membesar'),
('G017', 'Pembengkakan Kelenjar Getah Bening'),
('G018', 'Bintik merah pada tangan dan kaki'),
('G019', 'Nyeri pada persendian lutut, pergelangan tangan, dan kaki'),
('G020', 'Nyeri tenggorokan'),
('G021', 'Buang air besar berwarna hitam dan keras'),
('G022', 'Lelah'),
('G023', 'Kurang darah'),
('G024', 'Warna kuning di kulit/selaput lendir mata'),
('G025', 'Mata merah'),
('G026', 'Radang saluran kelenjar getah bening'),
('G027', 'Pembesaran tungkai, lengan, bbuah dada'),
('G028', 'Buah zakar terlihat agak merah dan terasa panas'),
('G029', 'Hilang nafsu makan');

-- --------------------------------------------------------

--
-- Table structure for table `tbl_diseases`
--

DROP TABLE IF EXISTS `tbl_diseases`;
CREATE TABLE IF NOT EXISTS `tbl_diseases` (
  `code` char(4) NOT NULL,
  `disease` varchar(60) NOT NULL,
  `scientific_name` varchar(60) NOT NULL,
  `definition` text NOT NULL,
  `solution` text NOT NULL,
  PRIMARY KEY (`code`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Dumping data for table `tbl_diseases`
--

INSERT INTO `tbl_diseases` (`code`, `disease`, `scientific_name`, `definition`, `solution`) VALUES
('P001', 'Demam Berdarah', 'Dengue Fever', 'Penyakit yang disebabkan oleh virus dengue ini disebarkan oleh gigitan nyamuk Aedes aegypti betina.', 'Minumlah air putih min.20 gelas berukuran sedang setiap hari (lebih banyak lebih baik); Cobalah menurunkan panas dengan minum obat penurun panas (paracetamol misalnya); Beberapa teman dan dokter menyarankan untuk minum minuman ion tambahan seperti pocari sweat; Minuman lain yang disarankan: Jus jambu merah untuk meningkatkan trombosit (ada juga yang menyarankan: daun angkak, daun jambu, dsb); Makanlah makanan yang bergizi dan usahakan makan dalam kuantitas yang banyak (meskipun biasanya minat makan akan menurun drastis).'),
('P002', 'Malaria', 'Vivax', 'Penyebabnya adalah parasit dari keluarga Plasmodium falciparum, vivax, atau malariae. Penyebarnya nyamuk Anopheles betina.', 'Saat ini ada obat-obatan atau kombinasi obat-obatan baru yang diberikan untuk mengobati malaria di berbagai daerah. Salah satunya adalah artemisinin (sudah digunakan bertahun-tahun di Cina), yang sering diberikan bersama dengan obat antimalaria lainnya atau dengan antibiotik. Di beberapa daerah klorokin (obat yang paling banyak digunakan selama bertahun-tahun) masih ampuh.'),
('P003', 'Chikungunya', 'Arthralgia', 'Demam Chikungunya disebabkan oleh virus Chikungunya (CHIKV). CHIKV termasuk keluarga Togaviridae, Genus alphavirus, dan ditularkan oleh nyamuk Aedes Aegypti.', 'Belum ada obat atau vaksin untuk penyakit ini. Perawatan hanya untuk mengurangi gejala.'),
('P004', 'Kaki Gajah', 'Filariasis/Elephantiasis', 'Filariasis (penyakit kaki gajah) adalah penyakit menular yang disebabkan cacing yang ditularkan melalui gigitan berbagai jenis nyamuk. Cacing jenis Wuchereria bancrofti yang paling sering ditemukan di negeri tropis seperti Indonesia. Kemudian ada jenis lain bernama Brugia malayi merupakan jenis endemis di daerah pedesaan di India, Asia Tenggara, daerah pantai utara China dan Korea Selatan. Dan yang terakhir jenis Brugia timori yang banyak ditemukan di daerah pedesaan di Kepulauan Timor, Flores, Alor dan Roti di Tenggara Indonesia.', 'Pengobatan filariasis secara massal dilakukan di daerah endemis dengan menggunakan obat diethyl carbamazine citrate (DEC) dikombinasikan dengan Albenzol sekali setahun selama 5-10 tahun. Untuk mencegah reaksi samping seperti demam, diberikan parasetamol. Dosis obat untuk sekali minum adalah DEC 6 mg/kg/berat badan, Albenzol 400 mg (1 tablet).'),
('P005', 'Demam Penyakit Kuning', 'Icterus/Jaundice', 'Demam penyakit kuning perkotaan disebarkan oleh nyamuk hitam yang sama dengan yang menyebarkan demam berdarah. Nyamuknya mempunyai bintik-bintik putih di sepanjang punggung dan kaki-kakinya. Nyamuk ini hidup dan berkembangbiak dalam genangan air di kota-kota dan desa-desa.', 'Pengobatan terbaik untuk demam penyakit kuning adalah istirahat dan minum banyak cairan. Kebanyakan orang sembuh total setelah beberapa waktu dan badan mereka membentuk antibodi terhadap demam penyakit kuning.');

-- --------------------------------------------------------

--
-- Table structure for table `tbl_relations`
--

DROP TABLE IF EXISTS `tbl_relations`;
CREATE TABLE IF NOT EXISTS `tbl_relations` (
  `disease_code` char(4) NOT NULL,
  `symptom_code` char(4) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Dumping data for table `tbl_relations`
--

INSERT INTO `tbl_relations` (`disease_code`, `symptom_code`) VALUES
('P001', 'G001'),
('P001', 'G002'),
('P001', 'G003'),
('P001', 'G004'),
('P001', 'G005'),
('P001', 'G006'),
('P001', 'G007'),
('P001', 'G008'),
('P001', 'G009'),
('P001', 'G010'),
('P001', 'G011'),
('P001', 'G012'),
('P001', 'G018'),
('P001', 'G019'),
('P001', 'G020'),
('P001', 'G021'),
('P002', 'G001'),
('P002', 'G004'),
('P002', 'G005'),
('P002', 'G006'),
('P002', 'G012'),
('P002', 'G013'),
('P002', 'G014'),
('P002', 'G015'),
('P002', 'G016'),
('P002', 'G020'),
('P002', 'G022'),
('P002', 'G023'),
('P002', 'G024'),
('P003', 'G001'),
('P003', 'G004'),
('P003', 'G006'),
('P003', 'G017'),
('P003', 'G018'),
('P003', 'G019'),
('P003', 'G025'),
('P004', 'G001'),
('P004', 'G017'),
('P004', 'G026'),
('P004', 'G027'),
('P004', 'G028'),
('P005', 'G001'),
('P005', 'G002'),
('P005', 'G004'),
('P005', 'G005'),
('P005', 'G006'),
('P005', 'G009'),
('P005', 'G029');

-- --------------------------------------------------------

--
-- Table structure for table `tbl_users`
--

DROP TABLE IF EXISTS `tbl_users`;
CREATE TABLE IF NOT EXISTS `tbl_users` (
  `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(30) NOT NULL,
  `password` varchar(40) NOT NULL,
  `salt` varchar(32) NOT NULL,
  `surename` varchar(30) NOT NULL,
  `sex` enum('M','F') NOT NULL,
  `address` varchar(100) NOT NULL,
  `occupation` varchar(60) NOT NULL,
  `level` tinyint(3) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

--
-- Dumping data for table `tbl_user`
--

INSERT INTO `tbl_user` (`id`, `username`, `password`, `salt`, `surename`, `sex`, `address`, `occupation`, `level`) VALUES
(1, 'admin', 'df5ea16694cf699082605a75cc6235ee427676cd', '0b05984787416f8a82911f1ac3c45c5a', 'administrator', 'M', 'depok', 'freelance', 10);

-- --------------------------------------------------------

--
-- Table structure for table `tmp_analyzes`
--

DROP TABLE IF EXISTS `tmp_analyzes`;
CREATE TABLE IF NOT EXISTS `tmp_analyzes` (
  `user_id` smallint(4) unsigned NOT NULL,
  `disease_code` char(4) NOT NULL,
  `symptom_code` char(4) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `tmp_symptoms`
--

DROP TABLE IF EXISTS `tmp_symptoms`;
CREATE TABLE IF NOT EXISTS `tmp_symptoms` (
  `user_id` smallint(4) unsigned NOT NULL,
  `symptom_code` char(4) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- --------------------------------------------------------

--
-- Table structure for table `tmp_diseases`
--

DROP TABLE IF EXISTS `tmp_diseases`;
CREATE TABLE IF NOT EXISTS `tmp_diseases` (
  `user_id` smallint(4) NOT NULL,
  `disease_code` char(4) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

*/

[FILE :login.php]
<?php
/*********************************
FILENAME    : login.php
CREATE BY   : cahya dsn
PURPOSE     : login form
CREATE DATE : 2014-04-08
**********************************/
if(isset($_POST['login'])){
  include 'config.php';
  $sql="SELECT id 
        FROM tbl_users
        WHERE 
          username='{$_POST['lname']}'
          AND password=SHA1(CONCAT('{$_POST['lpass']}',salt))";
  if($result=$db->query($sql)){
    if($result->num_rows>0){
      session_start();
      $data=$result->fetch_object();
      $_SESSION['id']=$data->id;
      header('location:consult.php');
    }else{
      $msg='fail to login';
    }
  }
}
?>
<!DOCTYPE html>
<html>
  <head>
    <title>ProtoExpert</title>
  </head>
  <body>
    <div id='container'>
      <form id='login' method='post'>
        <table>
          <tr>
            <th colspan='2'>LOGIN</th>
          </tr>
          <?php
          if(isset($msg)){
            echo "<tr><td>{$msg}</td></tr>";
          }
          ?>
          <tr>
            <td>Username</td>
            <td><input type='text' name='lname' id='lname' /></td>
          </tr>
          <tr>
            <td>Password</td>
            <td><input type='password' name='lpass' id='lpass' /></td>
          </tr>
          <tr>
            <td colspan='2'><input type='submit' name='login' id='login' /></td> 
          </tr>
        </table>
      </form>
    </div>
  </body>
</html>

[FILE : logout.php]
<?php
/*********************************
FILENAME    : logout.php
CREATE BY   : cahya dsn
PURPOSE     : logout apps
CREATE DATE : 2014-04-08
**********************************/
session_start();
unset($_SESSION['id']);
header('location:login.php');

[FILE:consult.php]
<?php
/*********************************
FILENAME    : consult.php
CREATE BY   : cahya dsn
PURPOSE     : expert system engine
CREATE DATE : 2014-04-08
**********************************/
session_start();
if(!isset($_SESSION['id'])){
  header('location:login.php');
}else{
  include 'config.php';
  if(!isset($_GET['process'])){
    $sql="SELECT disease_code 
          FROM tmp_diseases 
          WHERE user_id='{$_SESSION['id']}' 
          GROUP BY disease_code";
    $result=$db->query($sql);
    if($result->num_rows==1){
      $sql="SELECT a.* 
            FROM tbl_diseases a
            JOIN tmp_analyzes b ON b.disease_code=a.code
            WHERE b.user_id='{$_SESSION['id']}'";
      $result=$db->query($sql);
      $solution=$result->fetch_assoc();
      foreach($solution as $k=>$v){
        echo "<b>$k</b> : $v<br>";
      }
      die();
    }else{
      $result->free();
      $sql="SELECT COUNT(1) AS jml
            FROM tmp_analyzes 
            WHERE user_id='{$_SESSION['id']}'";
      $result=$db->query($sql);
      $data=$result->fetch_object();
      $result->free();
      if($data->jml>1){
        $sql="SELECT a.code,a.symptom
              FROM tbl_symptoms a
                JOIN tmp_analyzes b ON b.symptom_code=a.code
              WHERE b.user_id='{$_SESSION['id']}'
                AND NOT b.symptom_code IN (
                  SELECT symptom_code 
                  FROM tmp_symptoms
                  WHERE user_id='{$_SESSION['id']}')
              ORDER BY a.code LIMIT 1";
      }else{
        $sql="SELECT code,symptom
              FROM tbl_symptoms
              ORDER BY code
              LIMIT 1";
      }
      $result=$db->query($sql);
      $data=$result->fetch_object();
    }
  }else{
    $sql="SELECT COUNT(1) AS jml
          FROM tmp_analyzes 
          WHERE user_id='{$_SESSION['id']}'";
    $result=$db->query($sql);
    $data=$result->fetch_object();
    if($data->jml>0){
      if($_POST['answer']=='Y'){
        $sql="DELETE FROM tmp_diseases
              WHERE user_id='{$_SESSION['id']}';"
              //- --
            ."INSERT INTO tmp_diseases (user_id,disease_code)
              SELECT '{$_SESSION['id']}',a.disease_code
              FROM tbl_relations a
              JOIN tmp_analyzes b ON b.disease_code=a.disease_code
              WHERE b.symptom_code='{$_POST['symptom']}'
              GROUP BY a.disease_code
              ORDER BY a.disease_code;"
              //- --
            ."DELETE FROM tmp_analyzes 
              WHERE user_id='{$_SESSION['id']}';"
              //- --
            ."INSERT INTO tmp_analyzes (user_id, disease_code,symptom_code)
              SELECT b.user_id,a.disease_code,a.symptom_code
              FROM tbl_relations a
              JOIN tmp_diseases b ON b.disease_code=a.disease_code
              WHERE b.user_id='{$_SESSION['id']}'
              ORDER BY a.disease_code,a.symptom_code;"
              //- --
            ."INSERT INTO tmp_symptoms (user_id,symptom_code) 
              VALUES ('{$_SESSION['id']}','{$_POST['symptom']}')";
      }else{
        $sql="DELETE FROM tmp_analyzes
              WHERE disease_code IN(
                SELECT disease_code
                FROM tbl_relations
                WHERE symptom_code='{$_POST['symptom']}')
              AND user_id='{$_SESSION['id']}';"
              //- --
            ."DELETE FROM tmp_diseases
              WHERE disease_code IN(
                SELECT disease_code
                FROM tbl_relations
                WHERE symptom_code='{$_POST['symptom']}')
              AND user_id='{$_SESSION['id']}';";
      }
    }else{
      if($_POST['answer']=='Y'){
        $sql="INSERT INTO tmp_diseases(user_id,disease_code)
              SELECT '{$_SESSION['id']}',disease_code
              FROM tbl_relations 
              WHERE symptom_code='{$_POST['symptom']}';"
              //- --
            ."INSERT INTO tmp_analyzes (user_id, disease_code,symptom_code)
              SELECT b.user_id,a.disease_code,a.symptom_code
              FROM tbl_relations a
              JOIN tmp_diseases b ON b.disease_code=a.disease_code
              WHERE b.user_id='{$_SESSION['id']}'
              ORDER BY a.disease_code,a.symptom_code;"
              //- --
            ."INSERT INTO tmp_symptoms (user_id,symptom_code) 
              VALUES ('{$_SESSION['id']}','{$_POST['symptom']}')";
      }else{
        $sql="INSERT INTO tmp_analyzes(user_id,disease_code,symptom_code)
              SELECT '{$_SESSION['id']}',disease_code,symptom_code
              FROM tbl_relations
              WHERE disease_code IN(
                SELECT disease_code
                FROM tbl_relations
                WHERE symptom_code = '{$_POST['symptom']}')
              ORDER BY disease_code,symptom_code;"
              //- --
            ."INSERT INTO tmp_diseases(user_id,disease_code)
              SELECT '{$_SESSION['id']}',disease_code
              FROM tbl_relations
              WHERE disease_code IN(
                SELECT disease_code
                FROM tbl_relations
                WHERE symptom_code = '{$_POST['symptom']}')
              GROUP BY disease_code;"
              //- --
             ."DELETE FROM tmp_analyzes
              WHERE disease_code IN(
                SELECT disease_code
                FROM tbl_relations
                WHERE symptom_code='{$_POST['symptom']}')
              AND user_id='{$_SESSION['id']}';"
              //- --
            ."DELETE FROM tmp_diseases
              WHERE disease_code IN(
                SELECT disease_code
                FROM tbl_relations
                WHERE symptom_code='{$_POST['symptom']}')
              AND user_id='{$_SESSION['id']}';";
      }
    }
    $db->multi_query($sql);
    while ($db->more_results() && $db->next_result());
    header('location:consult.php');
  }
}
if(isset($data)){
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Consultations</title>
  </head>
  <body>
    <form action="consult.php?process=1" method="post" name="form1">
      <table width="100%" border="0" cellpadding="2" cellspacing="1" >
      <tr>
        <td>Answer this question</td>
      </tr>
      <tr>
        <td width="312">
        Do you <?php echo "[{$data->code}] $data->symptom";?> ?
        <input name="symptom" type="hidden" value="<?php echo $data->code; ?>"></td>
      </tr>
      <tr>
        <td>
          <input type='radio' name='answer' value='Y' checked='checked' />
          True (YES)
          <input type='radio' name='answer' value="N" />
          False (NO)
        </td>
      </tr>
      <tr>
        <td>
          <input type="submit" name="Submit" value="Process" />
        </td>
      </tr>
      </table>
    </form>
  </body>
</html>
<?php
}