Wednesday, July 4, 2007

PHP to PDF with Oracle Database

I never think that PHP can do this. It's still about my database labwork. Yesterday, I've got a problem with creating a report from PHP. And then I asked my friend, Wima because I had to finish web engine for my application. We have the same project so we divided it into two parts. He made the report and I handled the PHP web engine.

First, a trying to create an excel document from PHP failed. We couldn't debug where is the error although didn't sleep for a long long night. In the next day, Wima tried to open sample application in XAMMP at localhost. We used XAMMP bundle where Apace, PHP, and MySQL integrated here. Wanna know the code? We still used Oracle as database. Here's the code:


//connection to Oracle
include ('koneksi.php');

//query
$query = "select no_kamar_inv , kamar_jenis_inv,kamar_kategori,
kamar_harga from kel65_inventaris_kamar order by no_kamar_inv";
$stmt = ociparse($c,$query);
ociexecute($stmt,OCI_DEFAULT);

//php class to create pdf
include ('class.ezpdf.php');
$pdf = new Cezpdf();
$pdf->selectFont('../../php/extras/pdf-related/Helvetica.afm');

$pdf->ezText('Laporan Data Kamar',14);
$pdf->ezText('Sistem Informasi E-Inap',10);
$pdf->ezText('',12);

$i=0;
while ($row=oci_fetch_array($stmt))
{
$data[$i]=array('NO KAMAR'=>$row[0],'JENIS KAMAR'=>$row[1],
'KATEGORI KAMAR'=>$row[2], 'HARGA KAMAR'=>$row[2]);
$i++;
}

$pdf->ezTable($data,"","",array('width'=>500));

$pdf->ezStream();
exit;



How to connect to Oracle is described in article PHP Connection to Oracle.
You will find some different things here if you use another version of XAMMP. But don't worry, it's just a little bit different. Have a nice try with PHP PDF report...

Special thanks for Wima

No comments: