First define pdf.php:
<?php
require_once 'fpdf.php';
define('FPDF_FONTPATH','/var/www/vhosts/mydomain/httpdocs/fonts/');
class PDF extends FPDF
{
private $db;
private $data;
function __construct($db)
{
$this->db = $db;
}
function LoadData($ecnid, $member_id)
{
$query = sprintf("SELECT c.code, c.begin_date, c.expire_date,
c.discount, c.member_id, c.product_id,
c.used_count, c.saleprice, c.coupon_id,
c.paid, p.payment_id, p.member_id,
p.coupon_id, p.completed, p.time,p.amount,
p.product_id, p.p_discount, p.comm_paid,
p.d_paid, m.member_id, m.agentrate
FROM coupon c, payments p, members m
WHERE c.coupon_id = %d
AND c.coupon_id = p.coupon_id
AND m.member_id = %d
AND p.completed = 1
AND p.comm_paid = 0
ORDER BY p.payment_id",
$ecnid,
$member_id);
$result = mysql_query($query, $this->db) or die(mysql_error());
$this->data = array();
while ($row = mysql_fetch_assoc($result))
{
$this->data[] = $row;
}
}
//Simple table
function PrintBasicTable()
{
foreach($this->data as $row)
{
foreach($row as $col)
{
$this->Cell(40,6,$col,1);
}
$this->Ln();
}
}
}
?>
Then use your PDF class:
<?php
require 'pdf.php';
// ...
$ecnid = 1;
mysql_select_db($database_pin, $pin);
$pdf = new PDF($pin);
$pdf->AliasNbPages();
$data = $pdf->LoadData($ecnid, $member_id);
$pdf->AddPage();
//Set font
$pdf->SetFont('Arial','B',36);
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(5,130);
$pdf->PrintBasicTable();
$pdf->Output();
?>
One important change that I made was to turn:
$row_comm = mysql_fetch_assoc($comm);
into:
$this->data = array();
while ($row = mysql_fetch_assoc($result))
{
$this->data[] = $row;
}
The reason is that mysql_fetch_assoc() only returns one row of the result set, but you want all the rows. Note also my correction to the sprintf(); but I made up $member_id as it was not given.
Originally, LoadData() was intended to load from a file, but since you changed your mind and wanted it to load from a database, it should be changed accordingly, as per my example.