1

Here's the code for generating a PDF Table but it seems my code is not right regarding in foreach, the tutorial use file.txt. Guys its my first time using FPDF in php so i'm confused right now what i need to do with my error. Tutorials > Tutorial 4: Multi-columns FPDF Link

<?php
require('fpdf.php');
$hostname = "localhost";
$database = "brm_dbs";
$username = "root";
$password = "";
$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($database, $conn);
class PDF extends FPDF
{
// Load data
function LoadData($query)
{
    // Read file lines
    //$lines = file($file);
    //$lines = file($query);
    $result = mysql_query($query);
    $data = array();
    foreach($result as $line)
        $data[] = explode(',',trim($line));
    return $data;
}

// Simple table
function BasicTable($header, $data)
{
    // Header
    foreach($header as $col)
        $this->Cell(40,7,$col,1);
    $this->Ln();
    // Data
    foreach($data as $row)
    {
        foreach($row as $col)
            $this->Cell(40,6,$row,1);
        $this->Ln();
    }
}

// Better table
function ImprovedTable($header, $data)
{
    // Column widths
    $w = array(40, 35, 40, 45);
    // Header
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C');
    $this->Ln();
    // Data
    foreach($data as $row)
    {
        $this->Cell($w[0],6,$row[0],'LR');
        $this->Cell($w[1],6,$row[1],'LR');
        $this->Cell($w[2],6,$row[2],'LR',0,'R');
        $this->Cell($w[3],6,$row[3],'LR',0,'R');
        $this->Ln();
    }
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}

// Colored table
function FancyTable($header, $data)
{
    // Colors, line width and bold font
    $this->SetFillColor(255,0,0);
    $this->SetTextColor(255);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
    // Header
    $w = array(40, 35, 40, 45);
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C',true);
    $this->Ln();
    // Color and font restoration
    $this->SetFillColor(224,235,255);
    $this->SetTextColor(0);
    $this->SetFont('');
    // Data
    $fill = false;
    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->Cell($w[2],6,$row[2],'LR',0,'R',$fill);
        $this->Cell($w[3],6,$row[3],'LR',0,'R',$fill);
        $this->Ln();
        $fill = !$fill;
    }
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}
}


//Create new pdf file

//Select the Products you want to show in your PDF file
$query=("SELECT name,service,type,status FROM permits");


//$name = $row['name'];
//$service = $row['service'];
//$type = $row['type'];
//$status = $row['status'];
$pdf = new PDF();
// Column headings
$header = array('Resident', 'Frontline Service', 'Type', 'Status');
// Data loading
$data = $pdf->LoadData($query);
$pdf->SetFont('Arial','',14);
$pdf->AddPage();
$pdf->BasicTable($header,$data);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);
$pdf->AddPage();
$pdf->FancyTable($header,$data);


$pdf->Output();
?>

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\project\heldeskback\sample_pdf3.php on line 19 FPDF error: Some data has already been output, can't send PDF file

Meyka Jograt
  • 314
  • 2
  • 4
  • 19
  • 1
    There appears to be several issues here. First, `mysql_query()` expects 2 parameters. The second should be the resource to the database. http://php.net/manual/en/function.mysql-query.php – Chris Sep 14 '15 at 15:03
  • possible duplicate of [Invalid argument supplied for foreach()](http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – Halayem Anis Sep 14 '15 at 15:07
  • 1
    @Chris: The 2nd parameter to `mysql_query` is actually optional. If it's not provided, it uses the last call to `mysql_connect` as its connection. – gen_Eric Sep 14 '15 at 15:08
  • Which `foreach` is giving this error? Are you making sure your query succeeded before looping over the result? – gen_Eric Sep 14 '15 at 15:09
  • i think my error is in this `function LoadData($query){}` the correct codes are in comment the `$lines$` cu'z the data is in .txt file not in query. – Meyka Jograt Sep 17 '15 at 07:49

0 Answers0