Hello

Please can any one help me? I want to export from mysql to pdf, using PHP and FPDF. Please can some one tell me how to export, using the following format

Firstname Lastname
Address
Town
Post Code

The code below will only do it in this format

Firstname Lastname Address Town Post Code

<?php
require('mysql_table.php');



require('../db_config.php');
require('../global.php');
// connect to the database
db_connect($mysql['username'], $mysql['password'], $mysql['database'],$mysql['host']);
$config = get_config($mysql['prefix']);

// start the session
admin_sessions($config['admin_session_expire']);
if(!isset($_SESSION['logged_in'])){
	redirect('./login.php');
}
class PDF extends PDF_MySQL_Table
{
function Header()
{
	//Title
	$this->SetFont('Arial','',18);
	$this->Cell(0,6,'Members who serve in '. $_GET['name'] .'',0,1,'C');
	$this->Ln(10);
	//Ensure table header is output
	parent::Header();
}
}



$pdf=new PDF();
$pdf->AddPage();
$pdf->AddCol('Firstname',30,'Firstname');
$pdf->AddCol('Surname',30,'Surname');
$pdf->AddCol('address1',30,'Address');
$pdf->AddCol('address2',30,'Address');
$pdf->AddCol('postCode',30,'Post Code');
$pdf->AddCol('towns',30,'Town');
$prop=array('HeaderColor'=>array(255,150,100),
			'color1'=>array(210,245,255),
			'color2'=>array(255,255,210),
			'padding'=>2);
$pdf->Table("select `members_HouseGroup`.`description` AS `description`,`members_HouseGroup`.`id` AS `housegroupID`,`members_users`.`id` AS `id`,`members_users`.`memberTypeID` AS `memberTypeID`,`members_users`.`firstname` AS `firstname`,`members_users`.`lastname` AS `lastname`,`members_users`.`marital_status` AS `marital_status`,`members_users`.`email` AS `email`,`members_users`.`homephone` AS `homephone`,`members_users`.`businessphone` AS `businessphone`,`members_users`.`mobilephone` AS `mobilephone`,`members_users`.`address1` AS `address1`,`members_users`.`address2` AS `address2`,`members_users`.`towns` AS `towns`,`members_users`.`postCode` AS `postCode`,`members_users`.`relationship` AS `relationship`,`members_users`.`username` AS `username`,`members_users`.`status` AS `status`,`members_users`.`marital_status` AS `marital`, members_memberType.description as MemberType from ((`members_users` left join`members_memberType` on((`members_memberType`.`id` = `members_users`.`memberTypeID`))) left join `members_HouseGroup` on((`members_HouseGroup`.`id` = `members_users`.`housegroupID`))  ) where ((`members_users`.`memberTypeID` in (select id from members_memberType where housegroupind = 1)) ) and username not in (select username from members_spouseLink) order by members_users.lastname",$prop);
$pdf->Output();

?>

Recommended Answers

All 10 Replies

i took this from my invoice pdf code
you might have to change some sizes and locations
but it should show you what you need

<?php
require('fpdf.php');
$pdf=new FPDF();

//==========================================================
//SET DATABASE 
//==========================================================
		$h = "localhost";
    	$u = "USERNAME";
    	$p = "PASSWORD";
    	$d = "DATABASE";
      $link = mysql_connect ($h, $u, $p) or die ("Could not connect to database, try again later");
mysql_select_db($d,$link);

//==========================================================
//GRAB DATA FROM DATABASE
//==========================================================
$query = "SELECT * FROM SOMEDATABASE WHERE `SOMETHING` = 'SOMETHINGELSE'"; 
 $result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_BOTH)){
$FIRSTNAME[] = $row['Firstname'];
$LASTNAME[] = $row['Lastname'];
$ADDRESS[] = $row['Address'];
$TOWN[] = $row['Town'];
$ZIPCODE = $row['Post Code'];


}

$county = count($item);
//==========================================================
//SET PDF INFO
//==========================================================

$pdf->AddPage();
$pdf->SetAuthor("AUTHOR");
$pdf->SetCreator("CREATOR", boolean);
$pdf->SetSubject("FILE ", boolean);
$pdf->SetTitle("TITLE", boolean);
$pdf->SetKeywords("SOME WORDS HERE", boolean);


//==========================================================
//SET COLOUMS
//==========================================================


$pdf->Rect(5, 70, 200, 190);
$pdf->Rect(5, 70, 10, 190);
$pdf->Rect(165, 70, 10, 190); //qty
$pdf->Rect(15, 70, 20, 190); // products
$pdf->Rect(160, 265, 40, 20);
$number = 75;


//==========================================================
//PRINT PRODUCTS  products
//==========================================================\
$county--;
		  for ($counter = 1 ; $counter <= $county; $counter += 1) { 
$pdf->Text(7, "$number", "$counter");
$pdf->Text(18, "$number", $FIRSNAME[$COUNTER]." ".$LASTNAME[$COUNTER]);
$pdf->Text(36, "$number", $ADDRESS[$COUNTER]);
$pdf->Text(170, "$number", $TOWN[$COUNTER];
$pdf->Text(181, "$number", $ZIPCODE[$COUNTER]);

$number = $number + 5; // SET SPACE BETTWEEN LINES

}

$pdf->Output();
?>

HITMANOF44th

Thanks for your help. I have converted all the relevant values to my database, but when I run it I get error

FPDF error: Some data has already been output, can't send PDF file (output started at /home/w/e/weststreet/web/public_html/MembersDBLive/reports/test.php:1)

I have checked google, but there seems to be many reasons. Please can you help me with this?

Below is the new code

<?php
      require('fpdf17/fpdf.php');
      $pdf=new FPDF();
      //==========================================================
      //SET DATABASE
      //==========================================================
      $h = "localhost";
      $u = "";
      $p = "";
      $d = "";
      $link = mysql_connect ($h, $u, $p) or die ("Could not connect to database, try again later");
      mysql_select_db($d,$link);
      //==========================================================
      //GRAB DATA FROM DATABASE
      //==========================================================
      $query = "SELECT * FROM members_users";
      $result = mysql_query($query) or die(mysql_error());
      while($row = mysql_fetch_array($result, MYSQL_BOTH)){
      $FIRSTNAME[] = $row['Firstname'];
      $LASTNAME[] = $row['Lastname'];
      $ADDRESS[] = $row['Address'];
      $TOWN[] = $row['Town'];
      $ZIPCODE = $row['Post Code'];
      }
      $county = count($item);
      //==========================================================
      //SET PDF INFO
      //==========================================================
      $pdf->AddPage();
      $pdf->SetAuthor("AUTHOR");
      $pdf->SetCreator("CREATOR", boolean);
      $pdf->SetSubject("FILE ", boolean);
      $pdf->SetTitle("TITLE", boolean);
      $pdf->SetKeywords("SOME WORDS HERE", boolean);
      //==========================================================
      //SET COLOUMS
      //==========================================================
      $pdf->Rect(5, 70, 200, 190);
      $pdf->Rect(5, 70, 10, 190);
      $pdf->Rect(165, 70, 10, 190); //qty
      $pdf->Rect(15, 70, 20, 190); // products
      $pdf->Rect(160, 265, 40, 20);
      $number = 75;
      //==========================================================
      //PRINT PRODUCTS products
      //==========================================================\
      $county--;
      for ($counter = 1 ; $counter <= $county; $counter += 1) {
      $pdf->Text(7, "$number", "$counter");
      $pdf->Text(18, "$number", $FIRSNAME[$COUNTER]." ".$LASTNAME[$COUNTER]);
      $pdf->Text(36, "$number", $ADDRESS[$COUNTER]);
      $pdf->Text(170, "$number", $TOWN[$COUNTER]);
      $pdf->Text(181, "$number", $ZIPCODE[$COUNTER]);
      $number = $number + 5; // SET SPACE BETTWEEN LINES
      }
      $pdf->Output();
      ?>

Hi HITMANOF44th

Found the problem, spaces before <?


Just working the data, will let you know the results soon

haha that dam space i had something like that happen and it was only once space
and it took me awhile to figure out where the error was now i know if i get the header error its because of spaces or some texted got echo"ed

Hi

I am only getting a blank PDF file without any of the output. Is there anywhere I am going wrong? Below is the code

<?php
      require('fpdf17/fpdf.php');
      $pdf=new FPDF();
      //==========================================================
      //SET DATABASE
      //==========================================================
      $h = "localhost";
      $u = "";
      $p = "";
      $d = "";
      $link = mysql_connect ($h, $u, $p) or die ("Could not connect to database, try again later");
      mysql_select_db($d,$link);
      //==========================================================
      //GRAB DATA FROM DATABASE
      //==========================================================
	  
	  //$memberRecord = mysql_query("select `members_HouseGroup`.`description` AS `description`,`members_HouseGroup`.`id` AS `housegroupID`,`members_users`.`id` AS `id`,`members_users`.`memberTypeID` AS `memberTypeID`,`members_users`.`firstname` AS `firstname`,`members_users`.`lastname` AS `lastname`,`members_users`.`marital_status` AS `marital_status`,`members_users`.`email` AS `email`,`members_users`.`homephone` AS `homephone`,`members_users`.`businessphone` AS `businessphone`,`members_users`.`mobilephone` AS `mobilephone`,`members_users`.`address1` AS `address1`,`members_users`.`address2` AS `address2`,`members_users`.`towns` AS `towns`,`members_users`.`postCode` AS `postCode`,`members_users`.`relationship` AS `relationship`,`members_users`.`username` AS `username`,`members_users`.`status` AS `status`,`members_users`.`marital_status` AS `marital`, members_memberType.description as MemberType from ((`members_users` left join`members_memberType` on((`members_memberType`.`id` = `members_users`.`memberTypeID`))) left join `members_HouseGroup` on((`members_HouseGroup`.`id` = `members_users`.`housegroupID`))  ) where ((`members_users`.`memberTypeID` in (select id from members_memberType where housegroupind = 1)) ) and username not in (select username from members_spouseLink) order by members_users.lastname"); 
	  //while (($rowMember = mysql_fetch_array($memberRecord)) != false) {
	 
      //$query = "SELECT * FROM members_users";
      $result = mysql_query("SELECT * FROM members_users");
      while(($row = mysql_fetch_array($result)) != false) {
      $FIRSTNAME[] = $row['firstname'];
      $LASTNAME[] = $row['lastname'];
      $ADDRESS[] = $row['address1'];
      $TOWN[] = $row['towns'];
      $ZIPCODE[] = $row['postCode'];

      }
	  
     // $county = count($item);
      //==========================================================
      //SET PDF INFO
      //==========================================================
      $pdf->AddPage();
      $pdf->SetAuthor("AUTHOR");
      $pdf->SetCreator("CREATOR", boolean);
      $pdf->SetSubject("FILE ", boolean);
      $pdf->SetTitle("TITLE", boolean);
      $pdf->SetKeywords("SOME WORDS HERE", boolean);
      //==========================================================
      //SET COLOUMS
      //==========================================================
      $pdf->Rect(5, 70, 200, 190);
      $pdf->Rect(5, 70, 10, 190);
      $pdf->Rect(165, 70, 10, 190); //qty
      $pdf->Rect(15, 70, 20, 190); // products
      $pdf->Rect(160, 265, 40, 20);
      $number = 75;
      //==========================================================
      //PRINT PRODUCTS products
      //==========================================================\
      $county--;
      for ($counter = 1 ; $counter <= $county; $counter += 1) {
      $pdf->Text(7, "$number", "$counter");
      $pdf->Text(18, "$number", $FIRSNAME[$COUNTER]." ".$LASTNAME[$COUNTER]);
      $pdf->Text(36, "$number", $ADDRESS[$COUNTER]);
      $pdf->Text(170, "$number", $TOWN[$COUNTER]);
      $pdf->Text(181, "$number", $ZIPCODE[$COUNTER]);
      $number = $number + 5; // SET SPACE BETTWEEN LINES
	  echo "this is $FIRSTNAME[$COUNTER]";
      }
      $pdf->Output();
?>

you should not be echoing anything

you should not be echoing anything

Sorry I have removed the echo statement. I was just using it to check if anything was being passed through.

also are you sure the fpdf file is there try moving it into the same director and use require('fpdf.php'); just to see

Thanks yes I have just done so, but I am still getting the same result.

Change
1) LINE #31; $county = count($item); to $county = count($FIRSTNAME); (or similar) to count how many rows of elements are returned from your sql query

2) LINE #56 has typo change FIRSNAME to FIRSTNAME ("T" missing") and

3) LINE #56-#61: change $COUNTER to $counter everywhere. Uppercase and lowercase vairable names are seen as different in php.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.