Membangun Web Service PDDIKTI Sederhana (Lanjutan), Dictionary Tabel Feeder

Untuk memudahkan kita membuat webservice yang tujuannya memasukkan data melalui template excel ataupun dari database, alangkah baiknya kita mengetahui nama tabel serta struktur tabel yang dimaksud.
Untuk hal ini kita perlu membuat script sederhana bagaimana menampilkan seluruh tabel-tabel yang ada di feeder serta struktur tabelnya. Karna tanpa kita mengetahui nama tabel dan struktur tabelnya, sangat mustahil kita bisa membuat script untuk insert data dari luar ke dalam database feeder. Karena dalam struktur tabel feeder, ada kewajiban-kewajiban yang harus di penuhi dalam hal insert data. Dengan mengetahui struktur tabel dan syarat-syarat yang diminta itulah selanjutnya kita akan mulai membuat script untuk insert data dengan mudah.
Tanpa panjang lebar, marilah kita mulai bagaimana membuat Script-nya.

Pertama yang harus dilakukan adalah kita akan membuka dan mengedit file depan.php, dan editlah file tersebut untuk menambahkan tombol Dictionary di atas, dimana tombol tersebut dimaksudkan  setelah pilih tombol tersebut maka jendela browser akan mengarah ke dokumen dictionary.php. Edit file depan.php juga bermaksud mengatur susunan fieldset beserta legend, dan script depan.php akan di edit menjadi seperti beriklut ini :

depan.php


<?php
include "session.php";
//echo "Id Perguruan Tinggi : <strong>".$id_sp."</strong><br />";
echo "Nama Perguruan Tinggi : <strong>".$nm_lemb."</strong><br />";

?>
<br />
<form method="post" enctype="multipart/form-data" action="dictionary.php">
<input name="dictiopnary" type="submit" value="Dictionary">
</form>

<fieldset>
  <legend>Form excell</legend>
  <p>Untuk <strong><font color="#990000">insert mahasiswa dari file excell</font></strong> silahkan download template excel <a href="temp_excell/inst_mhs.xls">disini</a></p>
<form method="post" enctype="multipart/form-data" action="insert_mhs.php">
Pilih File Excel :
<input name="fileexcel" type="file"> <input name="upload" type="submit" value="Import">
</form>
<br />
  <p>Untuk <strong><font color="#990000">insert KRS dari file excell</font></strong> silahkan download template excel <a href="temp_excell/krs_mhs.xls">disini</a></p>
<form method="post" enctype="multipart/form-data" action="insert_krs_mhs.php">
Pilih File Excel :
<input name="fileexcel" type="file"> <input name="upload" type="submit" value="Import">
</form>
<br />
  <p>Untuk <strong><font color="#990000">insert nilai dari file excell</font></strong> silahkan download template excel <a href="temp_excell/nilai_mhs.xls">disini</a></p>
<form method="post" enctype="multipart/form-data" action="insert_nilai_mhs.php">
Pilih File Excel :
<input name="fileexcel" type="file"> <input name="upload" type="submit" value="Import">
</form>
</fieldset>
<br />
<fieldset>
  <legend>Form MySql2Feeder</legend>
<p>Untuk <strong><font color="#990000">insert mahasiswa</font></strong> dari database : <font color="#000066" size="+2">MySql to Feeder</font></p>
<form method="post" enctype="multipart/form-data" action="mhs_mysql2feeder.php">
<input name="mhs" type="submit" value="MySql2Feeder">
</form>
</fieldset>
<br />
<form method="post" action="logout.php">
<input name="kembali" type="button" value="Logout" />
</form>

Berikut tampilan halaman depan.php setelah dilakukan perubahan-perubahan.


Kemudian kita akan membuat script untuk menampilkan seluruh tabel database feeder  seperti script di bawah ini :

dictionary.php


<html>
<head>
</head>
<body>
<input name="kembali" type="submit" value="Kembali" alt="Kembali" onClick="history.go(-1)" />
<h1> List Table Feeder </h1>
    <?php
      include 'session.php';
      $result=$proxy->ListTable($gettoken);
      $tabel=$result['result'];
      //print_r($tabel);
 ?>
      <!--<table class="table table-striped table-hover dataTable" id="table-editable">-->
      <table border="0" >
             <thead>
             <tr>
             <th bgcolor="#FFA6D2">No</th>
             <th bgcolor="#FFA6D2">Nama Tabel</th>
             <th bgcolor="#FFA6D2">Jenis</th>
             <th bgcolor="#FFA6D2">Record Data</th>
             <th class="text-center" bgcolor="#FFA6D2">Aksi</th>
             </tr>
             </thead>
                                        
             <tbody>
  <?php
  $n=0;
  for($i=0;$i<count($tabel);$i++){
  $n=$n+1;
  $natabel = $tabel[$i]['table'];
  $jenis=$tabel[$i]['jenis'];
  @$result1=$proxy->GetCountRecordset($token,$natabel);
  @$record=$result1['result'];
  //echo $token."-".$natabel."-".$record."<br>";
           
           
  //Print_r($record);
           
  if ($n%2==1)
     $warna="#EBE4E4";
  else
     $warna="#FADDDD";  
  ?>
          
               <tr <?php echo $warna;?>>
                   <td bgcolor=<?php echo $warna;?>><?php echo $n;?></td>
                   <td bgcolor=<?php echo $warna;?>><?php echo $natabel;?></td>
                   <td bgcolor=<?php echo $warna;?>><?php echo $jenis;?></td>
                   <td bgcolor=<?php echo $warna;?>><?php echo $record;?></td>
                   <td bgcolor=<?php echo $warna;?>><a href="<?php echo "struktur.php?act=data&tabel=$natabel&jenis=$jenis";?>">DataTabel</a>
                       <a href="<?php echo "struktur.php?act=lihat&tabel=$natabel&jenis=$jenis";?>">StrukturData</a></td>
               </tr>
               <?php
  }
  ?>
              </tbody>
        </table>
</body>
</html>

Hasil dari script dictionary tersebut adalah seperti gambar berikut ini :


Pada tabel tersebut, pada kolom aksi ada link yang bertujuan untuk melihat isi data tabel, dan juga link untuk melihat struktur tabel pada baris tersebut.
Link itu akan mengarah pada file struktur.php seperti script berikut :

struktur.php

<html>
<head>


<?php
include "session.php";
@$tabel=$_GET['tabel'];
@$jenis=$_GET['jenis'];

if(isset($_GET['act'])){
 switch($_GET['act']){
  case "lihat" :
  ?>
     <form method="post" enctype="multipart/form-data" action="dictionary.php">
            <input name="dictiopnary" type="submit" value="Dictionary">
            </form>
     <?php

  echo "<h1> Struktur Tabel ".$tabel."</h1>";
  //echo "ini case lihat tabel ".$tabel;
  ?>
  <!--<table class="table table-bordered" border="1" cellspacing="1" cellpadding="1" class="box_cccccc">-->
   <table width="80%" border="0" >
   <!-- Membuat Judul Kolom dan Warna Sesuai Tabelnya-->
    <thead>
    <tr>

    <?php
     @$result=$proxy->GetDictionary($token,$tabel);
     @$coba=$result['result'];
     @$coba1=array_unique($coba);

     //print_r($coba1);

     foreach($coba1 as $key=>$value){
      ?>
         <th bgcolor="#FFA6D2">No</th>
      <?php
        foreach($value as $item=>$final_item)
         {
          ?>
        <th height="20" align="center" bgcolor="#FFA6D2" >&nbsp;<?php echo $item; ?>&nbsp;</th>
        <?php
         }
     }
        ?>
        
     </tr>
     </thead>
         
     <!-- end judul Kolom -->

     <tbody>
     <?php

     /* mengatur warna pada isi tabel berdasarkan ganjil/genap */

     $a=0;
     foreach(@$coba as $key=>$value){
      for($i=0;$i<1;$i++){
       $a++;

      if ($a%2==1)
       $warna="#EBE4E4";
      else
       $warna="#FADDDD";
     ?>
     <!-- menerapkan background isi tabel -->
     <tr bgcolor="<?php echo $warna; ?>">
     <!-- loop mencari isi tabel */-->
     <td bgcolor=<?php echo $warna;?>><?php echo $a;?></td>
      <?php
      foreach(@$value as $item=>$final_item){
      ?>
       <!-- isi tabel -->
       <td height="20" align="center" bgcolor=<?php echo $warna;?>>&nbsp;<?php echo $final_item; ?>&nbsp;</td>
       <?php
      }
      }
       /* end pengaturan warna isi tabel */
     }
     ?>
     </tr>
     </tbody>
     </table>
     <?php  

    break;
    /* end case "lihat" */
  
    case "data" :

  ?>
   <form method="post" enctype="multipart/form-data" action="dictionary.php">
            <input name="dictiopnary" type="submit" value="Dictionary">
            </form>
   <?php
   
  echo "<h1> Data Tabel ".$tabel."</h1>";

     if ($jenis=='Data'){
    echo "Program Studi :";
    $nama_pt="id_sp='".$_SESSION['id_sp']."'";
    //echo $nama_pt;
    $result=$proxy->GetRecordset($gettoken,'sms',$nama_pt,'id_jenj_didik,nm_lemb asc','','');
    $coba=$result['result'];
    @$coba1=array_unique($coba);
    ?>
     <form method="post" name="prodi" action="<?php echo $_SERVER['PHP_SELF']."?act=records&tabel1=".$tabel."&id_pt=".$nama_pt; ?>">
     
     <select name="keyfilter" class="inputbox" id="keyfilter">
     <?php
                        for($i=0;$i<count($coba);$i++){
                            $prodi = $coba[$i]['nm_lemb'];
                            $id_sms = $coba[$i]['id_sms'];
                            $jenjang=$coba[$i]['id_jenj_didik'];
                                    
                            // untuk menggabungkan jenjang dengan prodi
                            $result=$proxy->GetRecord($gettoken,'jenjang_pendidikan',"id_jenj_didik='$jenjang'");
                                $jenj=$result['result'];
                                $j=$jenj['nm_jenj_didik'];
                                //echo "$j"."-"."$prodi<br>";
        
                            if (isset($_POST['keyfilter']))
                               {
                                             $selected = (isset($_POST['keyfilter']) and $_POST['keyfilter'] == $test) ? 'selected' : '';
                                 echo "<option value='$id_sms' $selected>$j"." ("."$prodi".")</option>\n";
                               }
                            else
                               {
                                 $selected = (isset($_GET['keyfilter']) and $_GET['keyfilter'] == $test) ? 'selected' : '';
                                 echo "<option value='$id_sms' $selected>$j"." ("."$prodi".")</option>\n";
                               }
                        }
                    ?>
                    </select>
   <button class="btn btn-primary" name="Submit" type="image" value="Submit" data-toggle="submit">OK</button>
   </form>
   <?php
   break;
   
     }else{
    $result=$proxy->GetRecordset($gettoken,$tabel,'','','','');
    $coba=$result['result'];
    @$coba1=array_unique($coba); 
    ?>
    
    <table width="80%" border="0" >
    <thead>  
    <tr> 
    <?php
    // judul kolom
    foreach(@$coba1 as $key=>$value){
       ?>
       <th bgcolor="#FFA6D2">No</th>
         <?php
       foreach(@$value as $item=>$final_item){
        ?>
        <th height="20" align="center" bgcolor="#FFA6D2">&nbsp;<?php echo $item; ?>&nbsp;</th>
        <?php
       }
    }
    // end judul kolom
    ?> 
    </tr>  
    </thead>  
    <tbody>
    <?php
    //  tampil data dalam kolom
    $a=0;
    foreach(@$coba as $key=>$value){
     for($i=0;$i<1;$i++){
      $a++;
      if ($a%2==1)
        $warna="#EBE4E4";
      else
        $warna="#FADDDD";
      ?>
      <tr bgcolor=<?php echo $warna; ?>>
      <td bgcolor=<?php echo $warna;?>><?php echo $a;?></td>
        <?php
      foreach(@$value as $item=>$final_item){
       ?>
       <td height="20" align="center" bgcolor=<?php echo $warna;?>>&nbsp;<?php echo $final_item; ?>&nbsp;</td> 
       <?php
      }
      ?>
      </tr>
      <?php
     }
    }
   ?>  
    </tbody>  
    </table>
   <?php      
   break;
   }
 case "records":
   
   ?>
     <form method="post" enctype="multipart/form-data" action="dictionary.php">
            <input name="dictiopnary" type="submit" value="Dictionary">
            </form>
     <?php
      
   //if(isset($_GET['op']))
   $tabel1=$_GET['tabel1'];
   $nama_pt=$_GET['id_pt'];
   $filter=$_POST['keyfilter'];
   $filter1="id_sms='$filter'";
   //$filter1="id_sms='d1871911-d62d-4c60-8c0f-ef6b5f0b6f77'"; 
   $result=$proxy->GetRecord($gettoken,'sms',$filter1);
   $prod=$result['result'];
   $prodi_data=$prod['nm_lemb'];
   
   //echo "filter ".$filter1;
   //echo "nama pt :".$nama_pt;
   //{
   //   if($_GET['op']=='records'){
     //echo "ini record ".$filter."<br>";
     //echo "ini record ".$filter1."<br>";
     //echo "nama prodi ".$prodi;
     ?>
     <h1>Prodi : <?php echo $prodi_data;?></h1>
     <h3><?php //echo "Tabel :".$tabel1."<br>";?></h3>
     <?php
     //if ($tabel1=='mahasiswa' || $tabel1=='dosen_pt'){
     if ($tabel1=='mahasiswa'){  //|| $tabel1=='dosen_pt'){
      $tbl='mahasiswa_pt';
      
     }elseif($tabel1=='dosen'){
      $tbl='dosen_pt';
      
     }
     
     if($tabel1=='mahasiswa' || $tabel1=='dosen'){
      $filter1="p.id_sms='$filter'";
      $filter2=$nama_pt;
      $result=$proxy->GetRecordset($gettoken,$tbl,$filter1,'','','');
      //echo "tabel yang dipilih mahasiswa di rubah ke mahasiswa_pt";        
     }else{
      $result=$proxy->GetRecordset($gettoken,$tabel1,$filter1,'','','');
     }
     $coba=$result['result'];
     @$coba1=array_unique($coba);
     //echo $gettoken."<br>";
     //print_r($coba1); 
    //}
   
    ?>
    <div class="table-responsive">
    <table id="myTable" class="display table" width="100%" border="0" >
    
    <thead>  
      <tr> 
      <?php
      // untuh membuat judul kolom
      foreach(@$coba1 as $key=>$value){
      ?>
     <th bgcolor="#FFA6D2">No</th>
     <?php
      foreach(@$value as $item=>$final_item){
      ?>
      <th height="20" align="center" bgcolor="#FFA6D2">&nbsp;<?php echo $item; ?>&nbsp;</th>
      <?php
      }
      }
      // akhir judul kolom
      ?> 
      </tr>  
    </thead>  
    <tbody>
    <?php
     // untuk menampilkan data dalam tabel
     $a=0;
     //$NO=0;
     foreach(@$coba as $key=>$value){
      for($i=0;$i<1;$i++){
      $a++;
      //$NO++;
      if ($a%2==1)
        $warna="#EBE4E4";
      else
        $warna="#FADDDD";  
      ?>
      
      <tr class="text" bgcolor=<?php echo $warna; ?>>
      
         <td class=<?php echo $warna;?>><?php echo $a;?></td>
         <?php
         foreach(@$value as $item=>$final_item){
        ?>
        
        <td height="20" align="center" class=<?php echo $warna;?>>&nbsp <?php echo $final_item; ?>&nbsp;</td> 
        
        <?php
         }
      ?>
      </tr>
      <?php
      } 
     }
     // end tampil data 
 break;       //}
 }
}

?>

</body>
</html>

<script>
$(document).ready(function(){

    $('#myTable').dataTable();

});
</script>


Dari script tersebut kita terapkan fungsi Get untuk melihat aksi yang di identikasi dengan act, yang selanjutnya dibuat suatu pilhan aksi tersebut menggunakan event switch, dimana jika pada kolom aksi dipilih link DataTabel maka pada file struktur.php akan di eksekusi switch case "data": yang akan mengeksekusi baris dibawahnya sampa ketemu break, sedang jika dipilih aksi StrukturData, maka switch aksi yang di eksekusi adalah switch case "lihat": sampai baris dibawahnya sampai ketemu break.

Demikian mebuat Webservice untuk melihat dictionary masing-masing tabel feeder.

Selamat belajar, selamat berkarya...dan salam koding.
Chayooooo..............
Previous
Next Post »