Read 138 times | Created 2013-06-21 04:36:41 | Updated 2013-06-21 05:38:28 | | |

 

<?php 
/*********************************   
FILENAME  : siswa.php   
CREATE BY  : cahya dsn   
PURPOSE   : listed master data and show detail data
CREATE DATE : 2013-06-21 
********************************** 

use test; 
-- -------------------------------------------------------- 
-- Table structure for table `tbl_siswa` 
-- 
DROP TABLE IF EXISTS `tbl_siswa`; 
CREATE TABLE IF NOT EXISTS `tbl_siswa` ( 
  `id_siswa` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key for table tbl_siswa', 
  `nis` varchar(4) NOT NULL COMMENT 'identification number', 
  `nama` varchar(30) NOT NULL COMMENT 'name', 
  `alamat` varchar(150) NOT NULL COMMENT 'address', 
  `hobi` varchar(50) NOT NULL COMMENT 'hobby', 
  `cita` varchar(50) NOT NULL COMMENT 'aim', 
  PRIMARY KEY (`id_siswa`) 
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='table to store siswa datas'; 
-- 
-- Dumping data for table `tbl_siswa` 
-- 
INSERT INTO `tbl_siswa` (`id_siswa`, `nis`,`nama`,`alamat`,`hobi`,`cita`) VALUES 
(1, '1201','ardila dw','tegal','menyanyi','penyanyi'), 
(2, '1202','budi susanto','slawi','game','dokter'),
(3, '1203','citra st','pemalang','memasak','chef');

*/
//database configuration   
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='test';
//database connection
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname); 
//get input data
$act=isset($_POST['act'])?$_POST['act']:(isset($_GET['act'])?$_GET['act']:''); 
$id=isset($_POST['id'])?$_POST['id']:(isset($_GET['id'])?$_GET['id']:'');

$header=<<<HEADER
<!DOCTYPE html>
<html>
  <head>
    <title>Data Siswa</title>
    <style type="text/css"> 
      *{font-family:calibri,arial,san-serif;font-size:12px;} 
      table {padding:2px;} 
      td,th{padding:2px;border:solid 1px #999;}
    </style>
  </head>
  <body> 
HEADER;

$footer=<<<FOOTER
    </body>
</html>
FOOTER;

if(!empty($id))
{
  $sql="SELECT * "
      ."FROM tbl_siswa "
      ."WHERE id_siswa='$id'";
  $result=$db->query($sql);
  $row=$result->fetch_object();
  $data=array(
    array('NIS',$row->nis),
    array('Nama',$row->nama),
    array('Alamat',$row->alamat),
    array('Hobi',$row->hobi),
    array('Cita-cita',$row->cita)
  );
  //================ DETAIL VIEW DATA ==================
  if($act=='view'){
    echo $header
        ."<h3>Detail</h3>n"
        ."<table>n";
    foreach($data as $dt){
      echo "<tr><th>".$dt[0]."</th><td>".$dt[1]."</td></tr>n";
    }
    echo "</table>n"
        ."<a href='siswa.php'>semua</a>n"
        .$footer;
  }
  //================ DETAIL VIEW DATA REPORT ===========
  elseif($act=='report')
  {
    require('fpdf.php');
    class PDF extends FPDF 
    { 
      function detailTable($data)
      {
          // Color and font restoration
          $this->SetFillColor(224,235,255);
          $this->SetTextColor(0);
          $this->SetFont('');
          // Data
          $fill = false;
          $w = array(40, 35);
          $this->Cell(array_sum($w),0,'','T',2);
          foreach($data as $row)
          {
              $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
              $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
              $this->Ln();
              $fill = !$fill;
          }
          // Closing line
          $this->Cell(array_sum($w),0,'','T');
      }
    }
    
    $pdf = new PDF(); 
    $pdf->SetFont('Arial','',14);
    $pdf->AddPage();
    $pdf->detailTable($data);
    $pdf->Output();
  }
//=============== LIST GRID VIEW DATA =================
}
else
{
    $sql="SELECT * "
        ."FROM tbl_siswa ";
    $result=$db->query($sql);
    $i=0;
    echo $header
        ."<h3>Daftar</h3>n"
        ."<table>n"
        ."<tr>"
        ."<th>No</th>"
        ."<th>NIS</th>"
        ."<th>Nama</th>"
        ."<th>Alamat</th>"
        ."<th>Hobi</th>"
        ."<th>Cita-cita</th>"
        ."<th>Aksi</th>"
        ."</tr>n";
    while($row=$result->fetch_object()){ 
      echo "<tr>"
          ."<td>".++$i."</td>"
          ."<td>".$row->nis."</td>"
          ."<td>".$row->nama."</td>"
          ."<td>".$row->alamat."</td>"
          ."<td>".$row->hobi."</td>"
          ."<td>".$row->cita."</td>"
          ."<td>"
          ."<a href='?act=view&id=".$row->id_siswa."'>view</a> | "
          ."<a href='?act=report&id=".$row->id_siswa."'>report</a>"
          ."</td>"
          ."</tr>n";
    }
    echo "<table>n"
        .$footer;
}
$db->close();