Kali ini kita akan belajar bagaimana mengimplementasikan rumus dari teori pelajaran Data Mining dengan menggunakan bahasa pemrograman PHP dan database MySQL / MariaDB. Yang kita butuhkan adalah Web Server (XAMPP/LAMP), Text Editor (Atom/Sublime Text / Notepad++ / etc), & Web Browser (Google Chrome / Mozilla Firefox).

Bagi yang belum tau apa itu data miningforecasting, dan regresi linear, silakan googling dulu ya, atau coba tanyakan pada dosen.

Disini saya mempunyai studi kasus “Peramalan Pendaftaran Mahasiswa Baru (PMB)”. Program ini saya buat se-simple mungkin agar pemula sekalipun bisa mengikuti dengan baik 

Langkah pertama kalian harus membuat database, disini saya menggunakan mysql phpmyadmin, kurang lebih seperti gambar dibawah:

Setelah itu langsung coding saja, buat folder baru di htdocs Anda dan buat beberapa file seperti punya saya, kemudian tulis script seperti dibawah :

index.php index adalah tampilan halaman utama pada website.

<!doctype html>
<html lang=''>
<head>
   <meta charset='utf-8'>
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="assets/styles.css">
   <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
   <script src="script.js"></script>
   <title>Aplikasi Forecasting</title>
   <link rel="apple-touch-icon" href="images/favicon.png">
   <link rel="shortcut icon" href="images/favicon.png">

   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.0/normalize.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lykmapipo/themify-icons@0.1.2/css/themify-icons.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pixeden-stroke-7-icon@1.2.3/pe-icon-7-stroke/dist/pe-icon-7-stroke.min.css">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.2.0/css/flag-icon.min.css">
   <link rel="stylesheet" href="assets/css/cs-skin-elastic.css">
   <link rel="stylesheet" href="assets/css/style.css">

   <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800' rel='stylesheet' type='text/css'>
</head>
<body>

<div id='cssmenu'>
<ul>
   <li class='active'><a href='index.php'>Home</a></li>
   <li><a href='input.php'>Input PMB</a></li>
   <li><a href='tampil.php'>Tampil PMB</a></li>
   <li><a href='prediksi.php'>Prediksi</a></li>
</ul>
</div>
<div class="content">
          <div class="animated fadeIn">
              <div class="row">
                  <div class="col-lg-12">
                      <div class="card">
                          <div class="card-body">
                              <!-- Credit Card -->
                              <div id="hai">
                                  <div class="card-body">
                                      <div>
                                          <img src="images/umk.png" width="90px" height="90px" />
                                          <a href="https://www.kuycoding.com">
                                          <img src="images/favicon.png" width="90px" height="90px" align="right"/>
                                        </a>
                                        <div class="col-sm-12">
                                            <div class="page-header float-center">
                                                <div class="page-title">
                                                    <h1 class="text-sm-center">APLIKASI FORECASTING PERAMALAN PMB</h1>
                                                </div>
                                            </div>
                                        </div>
                                      </div>
                                      <hr>
                        <div class="clearfix"></div>
                          <center>
                          <div class="col-md-6">
                                            <div class="card">
                                                <div class="card-header">
                                                    <strong class="card-title mb-3">Profil Mahasiswa</strong>
                                                </div>
                                                <div class="card-body">
                                                    <div class="mx-auto d-block">
                                                        <img class="rounded-circle rounded-circle mr-3" src="images/afif.jpg" alt="Card image cap">
                                                        <h4 class="text-sm-center mt-2 mb-1">Afif Nor Yusuf</h4>
                                                        <h5 class="text-sm-center"></h5>
                                                        <div class="location text-sm-center">Teknik Informatika</div>
                                                        <p></p>
                                                        <a href="https://www.kuycoding.com">
                                                          <!--halaman web jangan dihapus hargai pembuat-->
                                                        <h5 class="text-sm-center mt-2 mb-1">WWW.KUYCODING.COM</h5>
                                                      </a>
                                                        <div class="location text-sm-center"><i class="fa fa-map-marker"></i> Mayong Lor, RT03/RW05, Mayong, Jepara</div>
                                                    </div>
                                                    <hr>
                                                    <div class="card-text text-sm-center">
                                                        <a href="http://facebook.com/afiftwrp"><i class="fa fa-facebook pr-1"></i></a>
                                                        <a href="https://kuycoding.com"><i class="fa fa-globe pr-1"></i></a>
                                                        <a href="http://instagram.com/afif_ny"><i class="fa fa-instagram pr-1"></i></a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        </center>
                                      </hr>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="clearfix"></div>
                      <?php include "assets/footer.php" ?>
</body>
<html>

koneksi.php script untuk mengkoneksikan database :

<?php

   ob_start();
   $koneksi;
   $namaserver  = "localhost";
   $username	= "root";
   $password	= "";
   $namadb	= "forecasting";

   $koneksi	=	mysqli_connect($namaserver,$username,$password,$namadb);
   if(!$koneksi) {
      die("Maaf, Koneksi Gagal:".mysqli_connect_error());
   }

?>

form input.php adalah form untuk menginputkan data kedalam database


<!doctype html>
<html lang=''>
<head>
   <meta charset='utf-8'>
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="assets/styles.css">
   <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
   <script src="script.js"></script>
   <title>Aplikasi Forecasting</title>
   <link rel="apple-touch-icon" href="images/favicon.png">
   <link rel="shortcut icon" href="images/favicon.png">

   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.0/normalize.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lykmapipo/themify-icons@0.1.2/css/themify-icons.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pixeden-stroke-7-icon@1.2.3/pe-icon-7-stroke/dist/pe-icon-7-stroke.min.css">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.2.0/css/flag-icon.min.css">
   <link rel="stylesheet" href="assets/css/cs-skin-elastic.css">
   <link rel="stylesheet" href="assets/css/style.css">

   <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800' rel='stylesheet' type='text/css'>
</head>
<body>

<div id='cssmenu'>
<ul>
   <li><a href='index.php'>Home</a></li>
   <li class='active'><a href='input.php'>Input PMB</a></li>
   <li><a href='tampil.php'>Tampil PMB</a></li>
   <li><a href='prediksi.php'>Prediksi</a></li>
</ul>
</div>
<div class="content">
          <div class="animated fadeIn">
            <div class="row">
                  <div class="col-lg-12">
                      <div class="card">
                          <div class="card-header">
                              <strong class="card-title">Input Data</strong>
                          </div>
                          <div class="card-body">
                              <!-- Credit Card -->
                              <div id="pay-invoice">
                                  <div class="card-body">
                                      <div class="card-title">
                                          <h3 class="text-center">Input Prediksi Pendaftaran Mahasiswa Baru</h3>
                                      </div>
                                      <hr>
                                      <form action="simpan.php" method="post">
                                          <div class="form-group text-center">
                                          </div>
                                          <div class="form-group">
                                            <label for="periode_pmb" class="control-label mb-1">Periode PMB</label>
                                            <select name="periode_pmb" id="periode_pmb" class="form-control">
                                              <?php
                                              $now = date('Y');
                                              for ($i = $now; $i >= 2000; $i--) {
                                                echo "<option value='".$i."/".($i+1)."'>".$i."/".($i+1)."</option>"
                                                ;
                                              }
                                              ?>
                                            </select>
                                          </div>
                                          <div class="form-group">
                                            <label>Jumlah PMB</label>
                                            <input id="jumlah_pmb" name="jumlah_pmb" type="number" class="form-control" value="">
                                          </div>
                                          <div class="form-actions form-group">
                                            <button type="submit" name="simpan" class="btn btn-primary btn-sm">Simpan</button>
                                          </div>
                                        </form>
                                      </hr>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="clearfix"></div>
                      <?php include "assets/footer.php" ?>
</body>
<html>

Proses simpan.php untuk mengirimkan data ke dalam database

<?php
include "koneksi.php";
if(isset($_POST['simpan'])) {
  $periode_pmb = $_POST['periode_pmb'];
  $jumlah_pmb = $_POST['jumlah_pmb'];
  $sql = mysqli_query($koneksi, "INSERT INTO pmb VALUES ('$kode_pmb','$periode_pmb','$jumlah_pmb')") or
    die(mysqli_error($koneksi));
  if($koneksi) {
    echo "<script>alert('Berhasil menambahkan data'); window.location='/pmb_forecasting/input.php';</script>";
  }
}
?>

form prediksi.php untuk memprediksi sesuai tahun yang diinputkan


<!doctype html>
<html lang=''>
<head>
   <meta charset='utf-8'>
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="assets/styles.css">
   <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
   <script src="script.js"></script>
   <title>Aplikasi Forecasting</title>
   <link rel="apple-touch-icon" href="images/favicon.png">
   <link rel="shortcut icon" href="images/favicon.png">

   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.0/normalize.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lykmapipo/themify-icons@0.1.2/css/themify-icons.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pixeden-stroke-7-icon@1.2.3/pe-icon-7-stroke/dist/pe-icon-7-stroke.min.css">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.2.0/css/flag-icon.min.css">
   <link rel="stylesheet" href="assets/css/cs-skin-elastic.css">
   <link rel="stylesheet" href="assets/css/style.css">

   <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800' rel='stylesheet' type='text/css'>
</head>
<body>

<div id='cssmenu'>
<ul>
   <li><a href='index.php'>Home</a></li>
   <li><a href='input.php'>Input PMB</a></li>
   <li><a href='tampil.php'>Tampil PMB</a></li>
   <li class="active"><a href='prediksi.php'>Prediksi</a></li>
</ul>
</div>

<div class="content">
        <div class="animated fadeIn">


            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-header">
                            <strong class="card-title">Prediksi</strong>
                        </div>
                        <div class="card-body">
                            <!-- Credit Card -->
                            <div id="prediksi">
                                <div class="card-body">
                                    <!--<div class="card-title">
                                        <h3 class="text-center">Prediksi</h3>
                                    </div>-->

                                      <form id="form1" name="form1" action="tampil.php" method="post">
                                        Peramalan PMB untuk
                                        <select name="periode_pmb" required>
                                          <?php
                                          for ($i=1; $i <=10 ; $i++) {
                                            // code...
                                            echo "<option value='$i'>$i</option>";
                                          }
                                           ?>
                                         </select>
                                         Tahun akademik berikutnya <input type="submit" name="prediksi" id="prediksi"class="btn btn-primary btn-sm" value="Prediksi"/>
                                      </form>

                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="clearfix"></div>
                    <?php include "assets/footer.php" ?>
</body>
<html>

tampil.php untuk menampilkan data yang telah di inputkan kedalam database dan dipanggil lagi untuk ditampilkan


<!doctype html>
<html lang=''>
<head>
   <meta charset='utf-8'>
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="assets/styles.css">
   <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
   <script src="script.js"></script>
   <title>Aplikasi Forecasting</title>
   <link rel="apple-touch-icon" href="images/favicon.png">
   <link rel="shortcut icon" href="images/favicon.png">

   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.0/normalize.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lykmapipo/themify-icons@0.1.2/css/themify-icons.css">
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pixeden-stroke-7-icon@1.2.3/pe-icon-7-stroke/dist/pe-icon-7-stroke.min.css">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.2.0/css/flag-icon.min.css">
   <link rel="stylesheet" href="assets/css/cs-skin-elastic.css">
   <link rel="stylesheet" href="assets/css/style.css">

   <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800' rel='stylesheet' type='text/css'>
</head>
<body>

<div id='cssmenu'>
<ul>
   <li><a href='index.php'>Home</a></li>
   <li><a href='input.php'>Input PMB</a></li>
   <li class='active'><a href='tampil.php'>Tampil PMB</a></li>
   <li><a href='prediksi.php'>Prediksi</a></li>
</ul>
</div>

<div class="content">
  <div class="animated fadeIn">
    <div class="rows">
      <div class="col-lg-12">
        <div class="card">
          <div class="card-header">
            <strong class="card-title">Tampil Data</strong>
          </div>
          <div class="card-body">
            <div id="tampil">
              <table class="table table-striped">
                <thead>
                  <tr>
                    <th scope="col">No.</th>
                    <th scope="col">Tahun Akademik</th>
                    <th scope="col">Jumlah PMB</th>
                    <th scope="col">X</th>
                    <th scope="col">Y</th>
                    <th scope="col">XX</th>
                    <th scope="col">XY</th>
                  </tr>
                </thead>
                  <tbody>
                  <?php
                    $con=mysqli_connect("localhost","root","","forecasting");
                        if (mysqli_connect_errno()) {
                            throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
                        };
                      $sql = mysqli_query($con, "SELECT * FROM pmb") or die (mysqli_error($con));
                    //  if (mysqli_num_error($sql) > 0) {
                        // code...
                        {
                        $x = 0;
                        $jumlah_x = 0;
                        $jumlah_y = 0;
                        $jumlah_xx = 0;
                        $jumlah_xy = 0;
                        while ($data = mysqli_fetch_array($sql)) {
                              $jumlah_x += $x;
                              $jumlah_y += $data['jumlah_pmb'];
                              $jumlah_xx += ($x * $x);
                              $jumlah_xy += ($x * $data['jumlah_pmb']);
                            ?>
                            <tr>
                              <td><?=$x+1;?>.</td>
                              <td><?=$data['periode_pmb'];?></td>
                              <td align="left"><?=$data['jumlah_pmb'];?></td>
                              <td align="left"><?=$x;?></td>
                              <td align="left"><?=$data['jumlah_pmb'];?></td>
                              <td align="left"><?=$x * $x;?></td>
                              <td align="left"><?=$x * $data['jumlah_pmb'];?></td>
                            </tr>
                            <?php
                            $x++;
                          }
                          ?>
                          <tr>
                            <td colspan="2">Jumlah</td>
                            <td></td>
                            <td><?=$jumlah_x;?></td>
                            <td><?=$jumlah_y;?></td>
                            <td><?=$jumlah_xx;?></td>
                            <td><?=$jumlah_xy;?></td>
                          </tr>
                          <tr>
                            <td colspan="2">Rata2</td>
                            <td></td>
                            <td>
                              <?php
                              $rata2_x = $jumlah_x / $x;
                              echo $rata2_x;
                              ?>
                            </td>
                            <td>
                              <?php
                              $rata2_y = $jumlah_y / $x;
                              echo $rata2_y;
                              ?>
                            </td>
                            <td colspan="2"></td>
                          </tr>
                          <tr>
                            <td colspan="2">B1</td>
                            <td colspan="6">
                              <?php
                              $b1 = ($jumlah_xy - (($jumlah_x * $jumlah_y) / $x)) / ($jumlah_xx - ($jumlah_x * $jumlah_x) / $x);
                              echo $b1;
                              ?>
                            </td>
                          </tr>
                          <tr>
                            <td colspan="2">B0</td>
                              <td colspan="6">
                                <?php
                                $b0 = $rata2_y - $b1 * $rata2_x;
                                echo $b0;
                                ?>
                              </td>
                            </tr>
                            <?php
                          }
                          ?>
                        </tbody>
              </table>
              <div>
                Rumus Regresi Linier :<br>
                <?php
                $y = $b0." + ".$b1." x";
                echo $y;
                ?>
              </div>
              <?php
              if (isset($_POST['prediksi'])) {
                $periode_pmb = $_POST['periode_pmb'];
                $thn = ($x - 1) + $periode_pmb;
                $prediksi = $b0 + ($b1 * $thn);
                ?>
                <div>
                  Prediksi PMB untuk <?=$periode_pmb;?> tahun berikunya adalah <?=$prediksi;?>
                </div>
                <?php
              }
              ?>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
 <div class="clearfix"></div>
 <?php include "assets/footer.php" ?>
</body>
<html>

Untuk file tambahanan silahkan download dibawah ini

Download Assets

Copy folder assets dan images kedalam folder aplikasi yang akan dibuat. File ini bertujuan untuk mempercantik tampilan web teman-teman.

Hasil perhitungan dengan excel

Ini tampilannya

Home
Input

Script saya diatas sudah menggunakan mysqli extension, bukan mysql extension yang lama. Jadi support di PHP versi 5 ataupun 7.

Jika masih ada error coba sesuaikan sama persis dengan script diatas dulu, diteliti lagi. Kalo udah paham silakan diedit sesuka hati sesuai kebutuhan.

Semoga bermanfaat. Happy coding 

Wassalamu’alaikum wr. wb.

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here