Getting mysql tbl data in pdf

Hi Guys,

This is very frustrating, the steam is comming out of my ears and i’m about to jump off table mountain.

I want to use the popular TCPDF module to convert a simple table that extract data from a mysql ‘users’ and ‘basket’ table and store it on my server as I will attach this dynamic pdf with a e-mail as a Invoice.

My code is below and is incorrect, i can access the mysql db tables and the data, but i can’t get it to display in the table(view the code).

When I echo the query values before the ‘html script’ it displays.
It looks like the html script cant run php tags inside it.


<?php
//============================================================+
// File name   : example_006.php
// Begin       : 2008-03-04
// Last Update : 2009-09-30
//
// Description : Example 006 for TCPDF class
//               WriteHTML and RTL support
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com s.r.l.
//               Via Della Pace, 11
//               09044 Quartucciu (CA)
//               ITALY
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: WriteHTML and RTL support
 * @author Nicola Asuni
 * @copyright 2004-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
 * @link http://tcpdf.org
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 * @since 2008-03-04
 */

require_once('../config/lang/eng.php');
require_once('../tcpdf.php');

// mysql connection
$con = mysql_connect("localhost","root");
  if(!$con)
	  {
	  die(mysql_error());
	  }
	
// get data from users table
mysql_select_db("kurt_shop");
$result = mysql_query("SELECT * FROM users WHERE username = 'testing'");

while($row = mysql_fetch_array($result))
  {
    $fname = $row['firstname'];
	$lname = $row['lastname'];
	$address = $row['address'];
	$country = $row['country'];
	$email = $row['email'];
  }

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false);

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('dejavusans', '', 10);

// add a page
$pdf->AddPage();

// create some HTML content
$htmlcontent =
"<html>
<body>
    <table width='500' border='0' align='center' cellpadding='5' cellspacing='0'>
      <tr>
        <td width='165'>Extreme Customs Pty. Ltd.</td>
        <td width='165'>TAX INVOICE</td>
        <td width='165' align='left'><?php echo date('M-d-Y'); ?></td>
      </tr>
      <tr>
        <td width='165'>Order No:</td>
        <td width='335' colspan='2'>58972</td>
      </tr>
	  <tr>
	    <td width='500' colspan='3'>&nbsp;</td>
	  </tr>	
	  <tr>
	    <td width='165'>CUSTOMER:</td>
        <td width='335' colspan='2'><?php echo '$fname $lname'; ?></td>
	  </tr>
	  <tr>
        <td width='165'>&nbsp;</td>
        <td width='335' colspan='2'><?php echo '$address'; ?></td>
	  </tr>
	  <tr>
        <td width='165'>&nbsp;</td>
        <td width='335' colspan='2'><?php echo '$country'; ?></td>
	  </tr>
	  <tr>
	    <td width='500' colspan='3'>&nbsp;</td>
	  </tr>	
	  <tr>
	    <td width='165'>Product</td>
	    <td width='165'>Qty</td>
	    <td width='165'>Total</td>
	  </tr>
	</table>
</body>
</html>";

// output the HTML content
$pdf->writeHTML($htmlcontent, true, 0, true, 0);

// test some inline CSS
$inlinecss = '<p>This is just an example of html code to demonstrate some supported CSS inline styles.
<span style="font-weight: bold;">bold text</span>
<span style="text-decoration: line-through;">line-trough</span>
<span style="text-decoration: underline line-through;">underline and line-trough</span>
<span style="color: rgb(0, 128, 64);">color</span>
<span style="background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);">background color</span>
<span style="font-weight: bold;">bold</span>
<span style="font-size: xx-small;">xx-small</span>
<span style="font-size: x-small;">x-small</span>
<span style="font-size: small;">small</span>
<span style="font-size: medium;">medium</span>
<span style="font-size: large;">large</span>
<span style="font-size: x-large;">x-large</span>
<span style="font-size: xx-large;">xx-large</span>
</p>';

$pdf->writeHTML($inlinecss, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();

//Close and output PDF document
$pdf->Output('example_006.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+
?>


I would also like to display the basket data with while loop as below


mysql_select_db("kurt_shop");
$result2 = mysql_query("SELECT * FROM basket");

  	  echo "<table width='600' border='0' align='center' cellpadding='5' cellspacing='0'>";
		
// print headings
	  echo "<tr>";
	  echo "<th align='left' width='200'>Product</th>";	
  	  echo "<th align='left' width='200'>Qty</th>";  		
	  echo "<th align='left' width='200'>Total</th>";	  		
	  echo "</tr>";
					
//Print table script
	 while($row = mysql_fetch_array($result2))
	  {
	    echo "<tr>";
		echo "<td>" . $row['pd_name']. "</td>";
		echo "<td>" .$row['qty']. "</td>";
		echo "<td>R" . $row['pd_price']. ".00</td>";
	    echo "</tr>";
	  }

// get total sum of price field		
$result2 = mysql_query('SELECT SUM(pd_price) AS total FROM basket');
$record = mysql_fetch_assoc($result2);

printf(
        "<tr>
         <td colspan='3'><strong>Sub-Total:</strong> R %s.00</td>
	     </tr>",
    	 $record['total']
	  );
	echo "</form>";
	echo "</table>";
	echo "<br/>";


It looks like the html script cant run php tags inside it.

Not sure whether I can explain it properly.

you’re running a php script already, you’re in a php script in which $htmlcontent gets populated with data, it’s not really html at this point. You can put data in this variable using php, not just the way you’re doing it. I’ll give you an example:

$htmlcontent .= "<td width='165' align='left'>" . date('M-d-Y') . "</td>";

you should note the special .= operator, that one means that the new stuff will be added to anything $htmlcontent already holds. The next thing you should notice is the way php code is added to this data.

Similarly, you don’t echo out your stuff (in the part with your while loop), you put it in a variable, and use that variable in the script.

Hope you understand what I’m talking about, I’m not really good at explaining though. It looks to me that you need to learn a little bit more about php, try w3schools.com

thanks for your response, i will give your suggestions a bash now.

t looks to me that you need to learn a little bit more about php

Yes,I do. Im awaiting my new kevin yank php book i ordered.

Im rebuilding the table now from the top, the date is showing in the pdf. good stuff!

This is my new code below, how do I echo the “$fname & $lname” variables.

Current script is not working.

<?php
//============================================================+
// File name   : example_006.php
// Begin       : 2008-03-04
// Last Update : 2009-09-30
//
// Description : Example 006 for TCPDF class
//               WriteHTML and RTL support
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com s.r.l.
//               Via Della Pace, 11
//               09044 Quartucciu (CA)
//               ITALY
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: WriteHTML and RTL support
 * @author Nicola Asuni
 * @copyright 2004-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
 * @link http://tcpdf.org
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 * @since 2008-03-04
 */

require_once('../config/lang/eng.php');
require_once('../tcpdf.php');

// mysql connection
$con = mysql_connect("localhost","root");
  if(!$con)
	  {
	  die(mysql_error());
	  }
	
// get data from users table
mysql_select_db("kurt_shop");
$result = mysql_query("SELECT * FROM users WHERE username = 'testing'");

while($row = mysql_fetch_array($result))
  {
    $fname = $row['firstname'];
	$lname = $row['lastname'];
	$address = $row['address'];
	$country = $row['country'];
	$email = $row['email'];
  }

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false);

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('dejavusans', '', 10);

// add a page
$pdf->AddPage();
// create some HTML content
$htmlcontent = "<html>
				  <body>
					<table width='500' border='0' align='center' cellpadding='5' cellspacing='0'>
					  <tr>
						<td width='165'>Extreme Customs Pty. Ltd.</td>
						<td width='165'>TAX INVOICE</td>";
		
$htmlcontent .= "<td width='165'>" . date('M-d-Y') . "</td></tr>";

$htmlcontent .=  "<tr>
        		    <td width='165'>Order No:</td>
		  	    	<td width='335' colspan='2'>58972</td>
      			  </tr>";

$htmlcontent .=  "<tr>
	    		    <td width='500' colspan='3'>&nbsp;</td>
	  			  </tr>";	

$htmlcontent .=  "<tr>
					<td width='165'>CUSTOMER:</td>
					<td width='335' colspan='2'>" . echo "$fname $lname"; . "</td>
				  </tr>";

$htmlcontent .= "</table></body></html>";

// output the HTML content
$pdf->writeHTML($htmlcontent, true, 0, true, 0);

$pdf->writeHTML($inlinecss, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();

//Close and output PDF document
$pdf->Output('example_006.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+
?>

okay looks better now but as I said, don’t echo!

you just put it there

$htmlcontent .=  "<tr>

                    <td width='165'>CUSTOMER:</td>

                    <td width='335' colspan='2'>" . $fname . " " . $lname . "</td>

                  </tr>";

it works thanks, now im ganna try the while loop.

Did the code you posted actually work? If so, does it have a header and footer for each page?
Thanks, look forward to hearing from you.:slight_smile:

yip, the code works.

still need to add the basket tbl data to the pdf.

You must have added loop code somewhere since this would only give you 1 (LIFO) db record.
while($row = mysql_fetch_array($result)) {
$fname = $row[‘firstname’];
$lname = $row[‘lastname’];
$address = $row[‘address’];
$country = $row[‘country’];
$email = $row[‘email’];
}

or did I miss something?

one record is correct.

Only for the ‘basket’ table i want to populate all the records, that i must still do.

[QUOTE=TaosBill;4463883]You must have added loop code somewhere since this would only give you 1 (LIFO) db record.
while($row = mysql_fetch_array($result)) {
$fname = $row[‘firstname’];
$lname = $row[‘lastname’];
$address = $row[‘address’];
$country = $row[‘country’];
$email = $row[‘email’];
}

<a href=" http://www.topnflnews.com/">NFL Draft</a>