FPDF fail now - Has worked? - php

I have FPDF to generate a member list from a Mysql database to a list in PDF. But after my website-host has update the php version, has mine member list not worked !
Now it show :
Warning: Invalid argument supplied for foreach() in
/customers/8/f/b/xxx.com/httpd.www/test/printmedlemsliste.php on line
46 FPDF error: Some data has already been output, can't send PDF file
(output started at
/customers/8/f/b/xxx.com/httpd.www/test/printmedlemsliste.php:46)
My code :
<?php
require('fpdf.php');
class People {
public function all() {
try {
$db = new PDO('mysql:host=localhost;dbname=xxx_com;charset=UTF-8', 'xxx_com', 'xxx');
$query = $db->prepare("SELECT o.user_post, o.user_name1, o.user_name2, o.user_address1, o.user_address2, o.user_phone1, o.user_phone2, c.user_email, o.user_type FROM e107_user c, e107_user_extended o WHERE c.user_id = o.user_extended_id Order By user_name1");
$query->execute();
$people = $query->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
//echo "Exeption: " .$e->getMessage();
$result = false;
}
$query = null;
$db = null;
return $people;
}
}
class PeoplePDF extends FPDF{
// Create basic table
public function BasicTable($header, $data)
{
// Header
$this->SetFont('','B', 12);
$this->Cell(0,6,'Medlemsliste'.'',1,1,'C');
$this->SetFont('','B', 9);
$dateinvoice = substr( $invoice[ 'dateinvoice' ], 6 ).substr( $invoice[ 'dateinvoice' ], 3, 2 ).substr( $invoice[ 'dateinvoice' ], 0, 2 );
$dateinvoice = date_create( $dateinvoice );
$dateinvoice = false === $dateinvoice ? '' : $dateinvoice->format( 'd-M-Y' );
$this->Cell( 10 ); $this->Cell( 17, 5, 'Print Dato:' ); $this->Cell( 17, 5, $dateinvoice, 0, 1 );
$this->SetFillColor(255);
$this->SetTextColor(0);
$this->SetFont('','B');
foreach ($header as $col) {
//Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
$this->Cell($col[1], 5, $col[0], 1, 0, 'L', true); }
$this->Ln();
// Data
$this->SetFillColor(255);
$this->SetTextColor(0);
$this->SetFont('');
foreach ($data as $row)
{
$i = 0;
foreach ($row as $field) {
$this->Cell($header[$i][1], 6, $field, 1, 0, 'L', true);
$i++;
}
$this->Ln();
}
}
}
// Column headings
$header = array(
array('Post', 20),
array('Fornavn', 18),
array('Efternavn', 46),
array('Adresse', 40),
array('Postnummer & By', 40),
array('Telefon', 22),
array('Mobil', 22),
array('E-Mail', 57),
array('Type', 12)
);
// Get data
$people = new People();
$data = $people->all();
$pdf = new PeoplePDF();
$pdf->SetFont('Arial', '', 9);
$pdf->AddPage('L');
$pdf->Ln(0);
$pdf->SetTitle("Medlemsliste", boolean);
$pdf->SetAuthor("-");
$pdf->BasicTable($header,$data);
$pdf->Output('yourfilename.pdf','D');
?>
Line 46 is foreach ($data as $row).
If I set // front of foreach ($data as $row), it a new error on line 49. Line 49 is foreach ($row as $field) {
Also if I set // front of Line 49, it generate the PDF file, but with no data in !
Can anyone help ?

That's because $data contains nothing , so the loop fails.
Confirm that you've got the correct data in $data when you've performed the query with $data = $people->all();
Try var_dump($data); to see that your return in the all() function actually returns a result.
I would also change your try / catch to this.
try {
$db = new PDO('mysql:host=localhost;dbname=xxx_com;charset=UTF-8', 'xxx_com', 'xxx');
$query = $db->prepare("SELECT o.user_post, o.user_name1, o.user_name2, o.user_address1, o.user_address2, o.user_phone1, o.user_phone2, c.user_email, o.user_type FROM e107_user c, e107_user_extended o WHERE c.user_id = o.user_extended_id Order By user_name1");
$query->execute();
$people = $query->fetchAll(PDO::FETCH_ASSOC);
return $people;
} catch (PDOException $e) {
//echo "Exeption: " .$e->getMessage();
return false;
}

Related

How to do linebreaks in a pdf table with fpdf generated from mysql db

I found this code in an older post. It works absolutely fine for my project except one problem. If the string from the mysql db is too long it will overflow the cells. I want it to automatically break and start a new line in the particular cell.
Original post I got the code from: Basic table creation fpdf
<?php
require('fpdf.php');
class People {
public function all() {
try {
$db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password');
$query = $db->prepare("SELECT first_name, middle_name, last_name, age, email FROM people ");
$query->execute();
$people = $query->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
//echo "Exeption: " .$e->getMessage();
$result = false;
}
$query = null;
$db = null;
return $people;
}
}
class PeoplePDF extends FPDF {
// Create basic table
public function CreateTable($header, $data)
{
// Header
$this->SetFillColor(0);
$this->SetTextColor(255);
$this->SetFont('','B');
foreach ($header as $col) {
//Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
$this->Cell($col[1], 10, $col[0], 1, 0, 'L', true);
}
$this->Ln();
// Data
$this->SetFillColor(255);
$this->SetTextColor(0);
$this->SetFont('');
foreach ($data as $row)
{
$i = 0;
foreach ($row as $field) {
$this->Cell($header[$i][1], 6, $field, 1, 0, 'L', true);
$i++;
}
$this->Ln();
}
}
}
// Column headings
$header = array(
array('First Name', 30),
array('Middle Name', 30),
array('Last Name', 30),
array('Age', 12),
array('Email', 47)
);
// Get data
$people = new People();
$data = $people->all();
$pdf = new PeoplePDF();
$pdf->SetFont('Arial', '', 12);
$pdf->AddPage();
$pdf->CreateTable($header,$data);
$pdf->Output();

PHP MultiDimensional Array with ints

at the moment I pass one of my functions an array like so
$inputs = array("FOOD" => "Pancake");
And then in my function I do something like the following
foreach ($inputs as $label => $data)
{
echo ($label . $data);
}
The above is only an example I have just written so I hope it is correct. Anyways, the function which has that foreach loop also has the following within it
$this->SetFillColor(190,205,44);
Now for each loop, I also want to set the fill colour of the output. So I was thinking about doing something like the following instead
$inputs = array("FOOD" => array("Pancake", '190,205,44'));
If I var_dump the array I am passing my function, I see
array:1 [▼
"FOOD" => array:2 [▼
0 => "PANCAKE"
1 => "190,205,44"
]
]
So this all seems correct. Now in my function I do
foreach($inputs as $label => $data){
foreach($data as $content => $colour) {
$this->SetFillColor($colour);
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
$this->Cell(100, 7, $content, 0, 0, 'L', 1, 0);
}
}
Now I have a couple of problems. Firstly, it seems to output two rows. The first cell prints out FOOD like it should do but the second cell prints out 0 (should be PANCAKE). The second row once again prints out FOOD, but the second cell prints out 1.
So how can I get it printing out just the first row? The other problem is $colour, because this is a string when it needs to be an int I think.
Any advice appreciated.
Thanks
Try this:
<?php
foreach($inputs as $label => $data){
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
// content is on index 0
$this->Cell(100, 7, $data[0], 0, 0, 'L', 1, 0);
// color on index 1
$this->SetFillColor($data[1]);
}
Or maybe even better create an object of \stdClass like this:
<?php
$foo[0]['food'] = new \stdClass;
$foo[0]['food']->content= 'Pancake';
$foo[0]['food']->color = '190,205,44';
$foo[1]['food'] = new \stdClass;
$foo[1]['food']->content= 'Apple';
$foo[1]['food']->color = '255,0,0';
for($i=0,$cnt=count($foo);$i<$cnt;$i++) {
foreach($foo[$i] as $label => $data) {
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
$this->Cell(100, 7, $data->content, 0, 0, 'L', 1, 0);
$this->SetFillColor($data->color);
}
}
Edit:
You can do this too:
<?php
$foo[0]['food']->color = new \stdClass;
$foo[0]['food']->content= 'Pancake';
$foo[0]['food']->color->r = 190;
$foo[0]['food']->color->g = 205;
$foo[0]['food']->color->b = 44;
... [snip]
$c = $data->color;
$this->SetFillColor($c->r,$c->g,$c->b);
Or even do it more OOP style
<?php
class color {
private $r;
private $g;
private $b;
public function get_r() {
return (float) $this->r;
}
public function get_g() {
return (float) $this->g;
}
public function get_b() {
return (float) $this->b;
}
public function set_r($r) {
$this->r = $r;
return $this;
}
public function set_g($g) {
$this->g = $g;
return $this;
}
public function set_b($b) {
$this->b = $b;
return $this;
}
static public function factory() {
return new color;
}
public function __construct() {
}
}
class food {
private $content;
private $color;
public function get_content() {
return $this->content;
}
public function get_color() {
return $this->color;
}
public function set_content($content) {
$this->content = $content;
return $this;
}
public function set_color(color $color) {
$this->color = $color;
return $this;
}
static public function factory() {
return new food;
}
public function __construct() {
}
}
$color = color::factory()->set_r(255)->set_g(0)->set_b(0);
$dataarray[] = food::factory()->set_content('Apple')->set_color($color);
foreach($dataarray as $food) {
echo $food->get_content();
echo $food->get_color()->get_r();
echo $food->get_color()->get_g();
echo $food->get_color()->get_b();
}
I think you wanted to write
$inputs = array("FOOD" => array("Pancake" => '190,205,44'));
so that 'Pancake' is a key that has a color string as its value.
Otherwise you should adjust your code to read like this:
foreach($inputs as $label => $data){
$content = $data[0];
$color = $data[1];
$this->SetFillColor($colour);
$this->Cell(65, 7, $label, 0, 0, 'L', 1, 0);
$this->Cell(100, 7, $content, 0, 0, 'L', 1, 0);
}
Either way should do the trick.
Currently $inputs = array("FOOD" => array("Pancake", '190,205,44')); just sets the 'FOOD' entry of $inputs to contain an array that has keys of 0,1 and so your $content variable will iterate 0,1 while $color iterates the values 'PANCAKE','190,205,44'.

Invalid argument supplied for foreach() FPDF PHP

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

Why doesn't my FPDF class function SetMargins() effect the first row?

I am using the FPDF class to create a pdf based on the results from a mysql query. The information was outputted in a table to pdf as expected but my problem occurred when I used SetMargins() to set the page margins. Everything except the first row is effected. The first line seems to be hardcoded to a certain position or margin definition.
Here is my code:
class Table extends FPDF
{
public function CreateTable($header, $data)
{
//Header
$this->SetFillColor(255);
$this->SetTextColor(0);
$this->SetFont('Arial','B', 12);
foreach ($header as $col) {
$this->Cell($col[1], 10, $col[0], 1, 0, 'C');
//Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
}
$this->Ln();
//Data
$this->SetFillColor(255);
$this->SetTextColor(0);
$this->SetFont('Arial', '', 8);
foreach ($data as $row) {
$i = 0;
foreach ($row as $field) {
$this->Cell($header[$i][1], 6, $field, 1, 0, 'C');
$i++;
}
$this->Ln();
}
}
}
//column headings for the department table
$dept_header = array(array('Name', 75), array('Phone', 40), array('Fax', 40));
//column headings for the team tables
$team_header = array(array('Name', 35), array('Role', 30), array('Office', 25), array('Cell', 25), array('Email', 45), array('Pager', 25));
//get data
$query = new ConnectQuery();
$dept_data = $query->all('SELECT * FROM Table');
$team_data = $query->all('SELECT CONCAT_WS(" ", FIRST_NAME, LAST_NAME), JOB_ROLE, OFFICE_PHONE, MOBILE_PHONE, EMAIL, PAGER_NUM FROM Table2');
$pdf = new Table('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->SetMargins(5, 5);
$pdf->CreateTable($team_header, $team_data);
$pdf->CreateTable($dept_header, $dept_data);
$pdf->Output();
?>
Just define the page margin BEFORE you add the first page. The position is not reset by setMargins() call, which results in the "hardcoded postition" which was set in AddPage():
$pdf = new Table('P', 'mm', 'Letter');
$pdf->SetMargins(5, 5);
$pdf->AddPage();
There's a property in the FPDF class called $cMargin, which is used to calculate the x-offset of the text before it gets printed within the cell, but there doesn't appear to be a setter for it. It's a public property, so after you've instantiated your FPDF class, just call:
$pdf = new fpdf('P','mm','A4');
$pdf->cMargin = 0;
Or you can workaround your 1st line like
$pdf->Ln(); //workaround for 1st line
$pdf->Cell(..);

custom images using Yahoo Weather feed

I'm retrieving data from Yahoo Weather currently and able to display the elements I need so far. I'm stumped on how to use the Condition Codes (i think i need to turn the number into an integer) to have a string such that I can display my own custom icons. As the weather in the region I am tracking is fairly mild I want to assign only a small number of icons but have each cover a string of Condition Codes. So far this is my code that is working–
function bm_getWeather ($code = '', $temp = 'c') {
$file = 'http://weather.yahooapis.com/forecastrss?w=' . $code . '&u=' . $temp;
$request = new WP_Http;
$result = $request->request($file);
if (isset($result->errors)) {
return FALSE;
}
$data = $result['body'];
$output = array (
'temperature' => bm_getWeatherProperties('temp', $data),
'weather_code' => bm_getWeatherProperties('code', $data),
'class' => bm_getWeatherProperties('code', $data),
'weather' => bm_getWeatherProperties('text', $data),
);
return $output;
}
function weather_icon() {
$data = bm_getWeather($code = '', $temp = 'c');
// Error is here
$nums = (int)$data['weather_code']; // Needs to be cast as an integer
$severe = array(12, 13, 14, 16, 19);
$rain = array(3200);
// Therefore the maths is wrong
switch($nums) {
case (in_array($nums, $severe)):
$cat = 'severe';
break;
case (in_array($nums,$rain)):
$cat = 'snow';
break;
default:
$cat = 'happy';
break;
}
return $cat;
}
function bm_getWeatherProperties ($needle, $data) {
$regex = '<yweather:condition.*' . $needle . '="(.*?)".*/>';
preg_match($regex, $data, $matches);
return (string)$matches[1];
}
Any help would be much appreciated as my php skills are less than adequate right now.

Categories