Hai para programmer :)Kali ini saya akan berbagi cara membuat laporan berbentuk text di php. Pada postingan yang telah lalu, saya kita sudah membahas bagaimana cara membuat report PDF di php. Bagi yang belum tahu, bisa di lihat di :Ok..untuk membuat report text nya kita perlu sediakan 3 file.langkah2nya sebagai berikut:Sediakan file ctk.txtFile ctk.txt biarkan isinya kosong. File ini berfungsi sebagai file copy yang nantinya digunakan untuk membuat file text baru.Buat file php dengan nama file class.grid.phpScriptnya sebagai berikut.<?php
class stringGrid{
function GetCollumned($tabArray, $textRows, $colSep, $newLine){
$jml_row = count($textRows);
$jml_col = count($tabArray['colWidth']);
for($r=0; $r<$jml_row; $r++){
for($c=0; $c<$jml_col; $c++){
$str.=$this->DrawString($textRows[$r][$c], $tabArray['colWidth'][$c], $tabArray['hAlign'][$c]);
$str.=$colSep;
}
$str.=$newLine;
}
return $str;
}
function DrawString($str, $width, $align){
// LEFT ALIGN
if($align==0){
$str=str_pad($str, $width, ' ', STR_PAD_RIGHT);
}
// CENTER ALIGN
elseif($align==1){
$str=str_pad($str, $width, ' ', STR_PAD_BOTH);
}
// RIGHT ALIGN
else{
if(is_numeric(str_replace('.','',$str))){
$pl=' ';
}else{
$pl='';
}
$str=str_pad($str.$pl, $width, ' ', STR_PAD_LEFT);
}
return $str;
}
}
function line($width,$line){
return str_pad('', $width, $line, STR_PAD_BOTH);
}
?>Buat file report dengan nama report.php<?php
//============================================
//@author : Anggun Patriana
//============================================
header("Content-type: text/plain");
require_once('class.grid.php');
$path = "../out/"; //lokasi menyimpan output
$Pcode = "hasil-text"; //nama file report
#################################################
$colSep = "";
$newLine = "
";
//mengatur ukuran kolom
$tabArray['colWidth'] = array(21, 5, 20, 5);
//mengatur posisi align 1=kiri 0=tengah 2=kanan
$tabArray['hAlign'] = array(1, 0, 1, 0);
$row=0; $textRows = '';
$textRows[$row][0] ="BELAJAR";
$textRows[$row][1] ="";
$textRows[$row][2] ="";
$textRows[$row][3] ="";
$row++; $hh++;
$textRows[$row][0] ="MEMBUAT";
$textRows[$row][1] ="";
$textRows[$row][2] ="";
$textRows[$row][3] ="";
$row++; $hh++;
$textRows[$row][0] ="REPORT DALAM BENTUK";
$textRows[$row][1] ="";
$textRows[$row][2] ="";
$textRows[$row][3] ="";
$row++; $hh++;
$textRows[$row][0] ="TEXT";
$textRows[$row][1] ="";
$textRows[$row][2] ="";
$textRows[$row][3] ="";
$row++; $hh++;
$textRows[$row][0] ="1";
$textRows[$row][1] ="";
$textRows[$row][2] ="";
$textRows[$row][3] ="";
$row++; $hh++;
$textRows[$row][0] ="";
$textRows[$row][1] ="2";
$textRows[$row][2] ="";
$textRows[$row][3] ="";
$row++; $hh++;
$textRows[$row][0] ="";
$textRows[$row][1] ="";
$textRows[$row][2] ="3";
$textRows[$row][3] ="";
$row++; $hh++;
$textRows[$row][0] ="";
$textRows[$row][1] ="";
$textRows[$row][2] ="";
$textRows[$row][3] ="4";
$row++; $hh++;
$textRows[$row][0] =line(50, '-');
$textRows[$row][1] ="";
$textRows[$row][2] ="";
$textRows[$row][3] ="";
$cs = new stringGrid();
$print.=$cs->GetCollumned($tabArray, $textRows, $colSep, $newLine);
echo $print;
########### GENERATE FILE ############
if( !file_exists( $path )){
mkdir( $path, 0777, true);
}
if (copy('ctk.txt', $path.$Pcode.'.txt')) {
$file = fopen( $path.$Pcode.'.txt', 'w' );
fwrite( $file, $print );
fclose( $file );
$print='';
}
#######################################
?>Lalu panggil file tersebut misal localhost/belajar/report.php (jika anda menyimpan file report.php di folder belajar).Maka akan muncul hasil outputnya di browser anda, lalu kita lihat file textnya di folder tempat anda menyimpan (pathnya).Jadi deh, tampilannya tinggal di modif sesuai kebutuhan.Selamat mencoba, semoga bermanfaat :)
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :