Improve company productivity with a Business Account.Sign Up


How do I create a table in FPDF pulling data out of MYSQL to fill the cells?

Posted on 2009-07-02
Medium Priority
Last Modified: 2013-12-13
I have a table on a php page that allows entry of data into fields in my database. You can see a picture attached below.The entire table has inputs, meaning the top axis, side axis as well as within the table itself.
The table has a list of the same type of variables along the top (y axis) and another list of the same type of variable along the x axis. The ten inputs along the top of the table are entered into the database called competitors:  c1, c2, c3, c4, c5, c6,c7, c8, c9 and c10.
The variables down the left side are entered into mysql as keywords: k1, k2, k3, k4, k5, k6, k7, k8, k9, k10, respectively.
Each input WITHIN the table is called it's keyword number and d1 along to ten, for example:
the first line of inputs in the table is k1d1, k1d2, k1d3, k1d4, k1d5, k and so on along until k1d10
then the next line WITHIN the table is called k2d1 through to k2d10,
the the third line WITHIN the table has it's input fields k3d1 through to k3d10
fourth line k4d1 going accross until k4d10
and so on, with tenth line down of table having it's inputs within the table as k10d1 going accross to k10d10.

I want to recreate the table as it is entered in this table exactly into a pdf in FPDF.
So the table needs to call these inputs from the database and position them in the pdf table in exactly the same way.
How do I do this nicely?

Below you will see code for entering inputs into the table in the php page,
and then a picture of the table form for COLLECTING THESE inputs, which is basically the same framework I want to recreate in the pdf,  
then code for a DIFFERENT existing table I would like to copy the aesthetics of.

This is the code for another existing table in my fpdf. For my new table, I'd like to keep the aesthetics the same as this but present the different information above: 
case "indexpagestable":
						$cellwidth = ((PAGEWIDTH - (PAGEMARGIN * 2)) * PDF_SCALING) / 6;
						$fontsize = 14;
						$pdf->SetDrawColor(255, 255, 255);
						$pdf->SetFillColor(192, 80, 77);
						$pdf->SetTextColor(255, 255, 255);
						$pdf->Cell($cellwidth * 2, $fontsize, "", 0, 0, "L");
						$pdf->Cell($cellwidth, $fontsize, "Google", 1, 0, "C", true);
						$pdf->Cell($cellwidth, $fontsize, "Yahoo", 1, 0, "C", true);
						$pdf->Cell($cellwidth, $fontsize, "MSN", 1, 1, "C", true);
						$pdf->SetDrawColor(255, 255, 255);
						$pdf->SetFillColor(223, 167, 167);
						$pdf->SetTextColor(0, 0, 0);
						$pdf->Cell($cellwidth, $fontsize, "", 0, 0, "L");
						$pdf->Cell($cellwidth, 14, "Index Pages", 0, 0, "C");
						$pdf->SetTextColor(255, 255, 255);
						$pdf->Cell($cellwidth, $fontsize, $values["googleindexed"], 1, 0, "C", true);
						$pdf->Cell($cellwidth, $fontsize, $values["yahooindexed"], 1, 0, "C", true);
						$pdf->Cell($cellwidth, $fontsize, $values["msnindexed"], 1, 1, "C", true);
						$pdf->SetDrawColor(0, 0, 0);
						$pdf->SetTextColor(0, 0, 0);
And this is the code for collecting the data into the table: 
    <td >Competitors<br /><br />
    <td><input value="<?php echo($_POST['c1']); ?>"  type="text" name="c1" /></td>
    <td><input value="<?php echo($_POST['c2']); ?>"  type="text" name="c2" /></td>
    <td><input value="<?php echo($_POST['c3']); ?>"  type="text" name="c3" /></td>
    <td><input value="<?php echo($_POST['c4']); ?>"  type="text" name="c4" /></td>
    <td><input value="<?php echo($_POST['c5']); ?>"  type="text" name="c5" /></td>
    <td><input value="<?php echo($_POST['c6']); ?>"  type="text" name="c6" /></td>
    <td><input value="<?php echo($_POST['c7']); ?>"  type="text" name="c7" /></td>
    <td><input value="<?php echo($_POST['c8']); ?>"  type="text" name="c8" /></td>
    <td><input value="<?php echo($_POST['c9']); ?>"  type="text" name="c9" /></td>
    <td><input value="<?php echo($_POST['c10']); ?>"  type="text" name="c10" /></td>
    <td width="50"><input value="<?php echo($_POST['k1']); ?>"  type="text" name="k1" id="k1"/></td>
    <td><input value="<?php echo($_POST['k1d1']); ?>"  type="text" name="k1d1" /></td>
    <td><input value="<?php echo($_POST['k1d2']); ?>"  type="text" name="k1d2" /></td>
    <td><input value="<?php echo($_POST['k1d3']); ?>"  type="text" name="k1d3" /></td>
    <td><input value="<?php echo($_POST['k1d4']); ?>"  type="text" name="k1d4" /></td>
    <td><input value="<?php echo($_POST['k1d5']); ?>"  type="text" name="k1d5" /></td>
    <td><input value="<?php echo($_POST['k1d6']); ?>"  type="text" name="k1d6" /></td>
    <td><input value="<?php echo($_POST['k1d7']); ?>"  type="text" name="k1d7" /></td>
    <td><input value="<?php echo($_POST['k1d8']); ?>"  type="text" name="k1d8" /></td>
    <td><input value="<?php echo($_POST['k1d9']); ?>"  type="text" name="k1d9" /></td>
    <td><input value="<?php echo($_POST['k1d10']); ?>"  type="text" name="k1d10" /></td>
    <td><input value="<?php echo($_POST['k2']); ?>"  type="text" name="k2" id="k2"/></td>
    <td><input value="<?php echo($_POST['k2d1']); ?>"  type="text" name="k2d1" /></td>
    <td><input value="<?php echo($_POST['k2d2']); ?>"  type="text" name="k2d2" /></td>
    <td><input value="<?php echo($_POST['k2d3']); ?>"  type="text" name="k2d3" /></td>
    <td><input value="<?php echo($_POST['k2d4']); ?>"  type="text" name="k2d4" /></td>
    <td><input value="<?php echo($_POST['k2d5']); ?>"  type="text" name="k2d5" /></td>
    <td><input value="<?php echo($_POST['k2d6']); ?>"  type="text" name="k2d6" /></td>
    <td><input value="<?php echo($_POST['k2d7']); ?>"  type="text" name="k2d7" /></td>
    <td><input value="<?php echo($_POST['k2d8']); ?>"  type="text" name="k2d8" /></td>
    <td><input value="<?php echo($_POST['k2d9']); ?>"  type="text" name="k2d9" /></td>
    <td><input value="<?php echo($_POST['k2d10']); ?>"  type="text" name="k2d10" /></td>
    <td><input value="<?php echo($_POST['k3']); ?>"  type="text" name="k3" id="k3"/></td>
    <td><input value="<?php echo($_POST['k3d1']); ?>"  type="text" name="k3d1" /></td>
    <td><input value="<?php echo($_POST['k3d2']); ?>"  type="text" name="k3d2" /></td>
    <td><input value="<?php echo($_POST['k3d3']); ?>"  type="text" name="k3d3" /></td>
    <td><input value="<?php echo($_POST['k3d4']); ?>"  type="text" name="k3d4" /></td>
    <td><input value="<?php echo($_POST['k3d5']); ?>"  type="text" name="k3d5" /></td>
    <td><input value="<?php echo($_POST['k3d6']); ?>"  type="text" name="k3d6" /></td>
    <td><input value="<?php echo($_POST['k3d7']); ?>"  type="text" name="k3d7" /></td>
    <td><input value="<?php echo($_POST['k3d8']); ?>"  type="text" name="k3d8" /></td>
    <td><input value="<?php echo($_POST['k3d9']); ?>"  type="text" name="k3d9" /></td>
    <td><input value="<?php echo($_POST['k3d10']); ?>"  type="text" name="k3d10" /></td>
    <td><input value="<?php echo($_POST['k4']); ?>"  type="text" name="k4" id="k4"/></td>
    <td><input value="<?php echo($_POST['k4d1']); ?>"  type="text" name="k4d1" /></td>
    <td><input value="<?php echo($_POST['k4d2']); ?>"  type="text" name="k4d2" /></td>
    <td><input value="<?php echo($_POST['k4d3']); ?>"  type="text" name="k4d3" /></td>
    <td><input value="<?php echo($_POST['k4d4']); ?>"  type="text" name="k4d4" /></td>
    <td><input value="<?php echo($_POST['k4d5']); ?>"  type="text" name="k4d5" /></td>
    <td><input value="<?php echo($_POST['k4d6']); ?>"  type="text" name="k4d6" /></td>
    <td><input value="<?php echo($_POST['k4d7']); ?>"  type="text" name="k4d7" /></td>
    <td><input value="<?php echo($_POST['k4d8']); ?>"  type="text" name="k4d8" /></td>
    <td><input value="<?php echo($_POST['k4d9']); ?>"  type="text" name="k4d9" /></td>
    <td><input value="<?php echo($_POST['k4d10']); ?>"  type="text" name="k4d10" /></td>
    <td><input value="<?php echo($_POST['k5']); ?>"  type="text" name="k5" id="k5"/></td>
    <td><input value="<?php echo($_POST['k5d1']); ?>"  type="text" name="k5d1" /></td>
    <td><input value="<?php echo($_POST['k5d2']); ?>"  type="text" name="k5d2" /></td>
    <td><input value="<?php echo($_POST['k5d3']); ?>"  type="text" name="k5d3" /></td>
    <td><input value="<?php echo($_POST['k5d4']); ?>"  type="text" name="k5d4" /></td>
    <td><input value="<?php echo($_POST['k5d5']); ?>"  type="text" name="k5d5" /></td>
    <td><input value="<?php echo($_POST['k5d6']); ?>"  type="text" name="k5d6" /></td>
    <td><input value="<?php echo($_POST['k5d7']); ?>"  type="text" name="k5d7" /></td>
    <td><input value="<?php echo($_POST['k5d8']); ?>"  type="text" name="k5d8" /></td>
    <td><input value="<?php echo($_POST['k5d9']); ?>"  type="text" name="k5d9" /></td>
    <td><input value="<?php echo($_POST['k5d10']); ?>"  type="text" name="k5d10" /></td>
    <td><input value="<?php echo($_POST['k6']); ?>"  type="text" name="k6" id="k6"/></td>
    <td><input value="<?php echo($_POST['k6d1']); ?>"  type="text" name="k6d1" /></td>
    <td><input value="<?php echo($_POST['k6d2']); ?>"  type="text" name="k6d2" /></td>
    <td><input value="<?php echo($_POST['k6d3']); ?>"  type="text" name="k6d3" /></td>
    <td><input value="<?php echo($_POST['k6d4']); ?>"  type="text" name="k6d4" /></td>
    <td><input value="<?php echo($_POST['k6d5']); ?>"  type="text" name="k6d5" /></td>
    <td><input value="<?php echo($_POST['k6d6']); ?>"  type="text" name="k6d6" /></td>
    <td><input value="<?php echo($_POST['k6d7']); ?>"  type="text" name="k6d7" /></td>
    <td><input value="<?php echo($_POST['k6d8']); ?>"  type="text" name="k6d8" /></td>
    <td><input value="<?php echo($_POST['k6d9']); ?>"  type="text" name="k6d9" /></td>
    <td><input value="<?php echo($_POST['k6d10']); ?>"  type="text" name="k6d10" /></td>
    <td><input value="<?php echo($_POST['k7']); ?>"  type="text" name="k7" id="k7"/></td>
    <td><input value="<?php echo($_POST['k7d1']); ?>"  type="text" name="k7d1" /></td>
    <td><input value="<?php echo($_POST['k7d2']); ?>"  type="text" name="k7d2" /></td>
    <td><input value="<?php echo($_POST['k7d3']); ?>"  type="text" name="k7d3" /></td>
    <td><input value="<?php echo($_POST['k7d4']); ?>"  type="text" name="k7d4" /></td>
    <td><input value="<?php echo($_POST['k7d5']); ?>"  type="text" name="k7d5" /></td>
    <td><input value="<?php echo($_POST['k7d6']); ?>"  type="text" name="k7d6" /></td>
    <td><input value="<?php echo($_POST['k7d7']); ?>"  type="text" name="k7d7" /></td>
    <td><input value="<?php echo($_POST['k7d8']); ?>"  type="text" name="k7d8" /></td>
    <td><input value="<?php echo($_POST['k7d9']); ?>"  type="text" name="k7d9" /></td>
    <td><input value="<?php echo($_POST['k7d10']); ?>"  type="text" name="k7d10" /></td>
    <td><input value="<?php echo($_POST['k8']); ?>"  type="text" name="k8" id="k8"/></td>
    <td><input value="<?php echo($_POST['k8d1']); ?>"  type="text" name="k8d1" /></td>
    <td><input value="<?php echo($_POST['k8d2']); ?>"  type="text" name="k8d2" /></td>
    <td><input value="<?php echo($_POST['k8d3']); ?>"  type="text" name="k8d3" /></td>
    <td><input value="<?php echo($_POST['k8d4']); ?>"  type="text" name="k8d4" /></td>
    <td><input value="<?php echo($_POST['k8d5']); ?>"  type="text" name="k8d5" /></td>
    <td><input value="<?php echo($_POST['k8d6']); ?>"  type="text" name="k8d6" /></td>
    <td><input value="<?php echo($_POST['k8d7']); ?>"  type="text" name="k8d7" /></td>
    <td><input value="<?php echo($_POST['k8d8']); ?>"  type="text" name="k8d8" /></td>
    <td><input value="<?php echo($_POST['k8d9']); ?>"  type="text" name="k8d9" /></td>
    <td><input value="<?php echo($_POST['k8d10']); ?>"  type="text" name="k8d10" /></td>
    <td><input value="<?php echo($_POST['k9']); ?>"  type="text" name="k9" id="k9"/></td>
    <td><input value="<?php echo($_POST['k9d1']); ?>"  type="text" name="k9d1" /></td>
    <td><input value="<?php echo($_POST['k9d2']); ?>"  type="text" name="k9d2" /></td>
    <td><input value="<?php echo($_POST['k9d3']); ?>"  type="text" name="k9d3" /></td>
    <td><input value="<?php echo($_POST['k9d4']); ?>"  type="text" name="k9d4" /></td>
    <td><input value="<?php echo($_POST['k9d5']); ?>"  type="text" name="k9d5" /></td>
    <td><input value="<?php echo($_POST['k9d6']); ?>"  type="text" name="k9d6" /></td>
    <td><input value="<?php echo($_POST['k9d7']); ?>"  type="text" name="k9d7" /></td>
    <td><input value="<?php echo($_POST['k9d8']); ?>"  type="text" name="k9d8" /></td>
    <td><input value="<?php echo($_POST['k9d9']); ?>"  type="text" name="k9d9" /></td>
    <td><input value="<?php echo($_POST['k9d10']); ?>"  type="text" name="k9d10" /></td>
    <td><input value="<?php echo($_POST['k10']); ?>"  type="text" name="k10" id="k10"/></td>
    <td><input value="<?php echo($_POST['k10d1']); ?>"  type="text" name="k10d1" /></td>
    <td><input value="<?php echo($_POST['k10d2']); ?>"  type="text" name="k10d2" /></td>
    <td><input value="<?php echo($_POST['k10d3']); ?>"  type="text" name="k10d3" /></td>
    <td><input value="<?php echo($_POST['k10d4']); ?>"  type="text" name="k10d4" /></td>
    <td><input value="<?php echo($_POST['k10d5']); ?>"  type="text" name="k10d5" /></td>
    <td><input value="<?php echo($_POST['k10d6']); ?>"  type="text" name="k10d6" /></td>
    <td><input value="<?php echo($_POST['k10d7']); ?>"  type="text" name="k10d7" /></td>
    <td><input value="<?php echo($_POST['k10d8']); ?>"  type="text" name="k10d8" /></td>
    <td><input value="<?php echo($_POST['k10d9']); ?>"  type="text" name="k10d9" /></td>
    <td><input value="<?php echo($_POST['k10d10']); ?>"  type="text" name="k10d10" /></td>

Open in new window

Question by:eunoiabella
  • 5
  • 3
  • 2
LVL 111

Accepted Solution

Ray Paseur earned 1000 total points
ID: 24777390
Not sure I understand how best to answer this question, but I will try to provide some guidance.  This is a fairly substantial application development project.  Nothing is very "hard" - it is just a lot of code that needs to be written and tested.  The general logic flow would be like this:

1. Query the data base. Get the values for all the fields in the table (if any) from the data base.
2. Present the table in a form to receive client input.  The form fields are prepopulated with whatever you found in the data base.
3. Receive the form in $_POST, validate its contents, handle errors, update the data base with the new client input.
4. Use the validated fields that you have just put into the data base to create the FPDF object.

The look and feel of the table on the client screen is going to be controlled by HTML table, tr, td and styled by CSS.  The look and feel of the PDF file is going to be controlled by your positioning statements that you send to FPDF.  As far as I know there is no generalized way to turn HTML tables and CSS into a set of FPDF commands.

In using FPDF, I have found it useful to use variables or define() constants for all the color numbers and positioning elements.  Since colors are somewhat dependent on the printer, and since you can adjust positions to about 1/100 inch, being able to tinker with these fields is important to getting a good final output.

HTH, ~Ray
LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 1000 total points
ID: 24778251
There are several HTML2PDF classes and services around.

Paid for service


The more complexity the HTML has, the harder it is to convert.

This route is probably easier than manually creating the PDF to match the rendered output.
LVL 111

Expert Comment

by:Ray Paseur
ID: 24778268
@RQ: Great idea.

I tried the seven49 link.  Here is my input URL:

The output is attached here.

Best regards, ~Ray
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

LVL 40

Expert Comment

by:Richard Quadling
ID: 24778288
Hmm. Overall, not bad. Graphics could be better rendered. It looks like it is quite low res (maybe the paid for service offers a greater quality).
LVL 111

Expert Comment

by:Ray Paseur
ID: 24778350
Yes, it got the positioning right.  The JPG-boogers are not so great, but if I used PNG images, that might make the image rendering better.  On this particular site, there are a lot of low-resolution JPG images - artifacts of the day when most of our clients came via slow dial-up lines.

Author Comment

ID: 24810507
Hi Ray and RQuading,
Thanks for your suggestions. The seven49 link is good in theory, however, the problem I have with the is the url I have is one generic one that pulls out entries from my database as I enter...  so how can I differentiate individual records to then convert to pdf?

Author Comment

ID: 24810559
The first picture below shows the process of entry into the database, the code following shows how I am currently calling it, and the second picture shows the pdf table. As you can see it's not quite working properly. The right column of the input table is all the entries that are showing in the pdf and they are on the diagonal instead of a straight line column on the right... Any ideas? How do I get all the inputs show in their right place in the pdf?

case "keywordstable":
                                    $cellwidth = ((PAGEWIDTH - (PAGEMARGIN * 2)) * PDF_SCALING) / 12;
                                    $pdf->Cell($cellwidth * 2, FONTHEIGHT, "Competitors", 0, 0, "L");
                                    for ($i=0; $i<count($values["competitornames"]); $i++)
                                          $pdf->Cell($cellwidth, FONTHEIGHT, $values["competitornames"][$i], 0, 0, "L");


                                    foreach ($values["keywords"] as $key => $keyword)
                                          $pdf->Cell($cellwidth * 2, FONTHEIGHT, $keyword, 0, 0, "L");
                                          for ($j=0; $j<count($values["competitors"][$key]); $j++)
                                                $pdf->Cell($cellwidth, FONTHEIGHT, $values["competitors"][$key][$i], 0, 0, "C");

Author Comment

ID: 24812461
Anyone able to help me?.... the url to pdf suggestion wasn't useful because my url stays the same for multiple database records and I need to pull specific tables out of the database....
I'd really love it if someone could help me....
LVL 111

Expert Comment

by:Ray Paseur
ID: 24814941
Well, as I said at the top, "This is a fairly substantial application development project" and EE is not well suited to doing application development - we are more of a forum where one can ask and answer questions - designing, writing and debugging program code is often beyond our ability - for that sort of thing a paid developer is what you need.

I recommended FPDF because it has worked for me when I needed to produce PDF files and I had the data available to me in a PHP script.  Richard recommended direct HTML-to-PDF classes as an easier alternative.  But either approach will require considerable time to integrate and debug.  I think we have answered the question and shown you some suitable tools, but we are not really in a position to do the debugging.

Best of luck with it, ~Ray
LVL 111

Expert Comment

by:Ray Paseur
ID: 24995791
Split with Richard.  That's my $0.02, ~Ray

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Among the most obnoxious of Exchange errors is error 1216 – Attached Database Mismatch error of the Jet Database Engine. When faced with this error, users may have to suffer from mailbox inaccessibility and in worst situations, permanent data loss.
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question