We help IT Professionals succeed at work.

FPDF Looping

musickmann
musickmann asked
on
So I've created an online employment application that generates a PDF form...and I just realized I ran into a little snag with the PDF and the previous employers section.
I have the form setup so they enter one previous employer, then they click the button to add more, basic javascript clone kinda thing.
So now I'm looping through and creating the form, wanting to create something like this:
The first line is this:
Employer Name | Location | Phone } From Date | To Date | Supervisor
followed by the data from the form
then the next line has:
Position | Duties | Reason for Leaving
followed by the form data

What I'm getting is two lines of labels and then two lines of data, like:
Employer Name | Location | Phone } From Date | To Date | Supervisor
Position | Duties | Reason for Leaving
First line responses
Second line responses

I understand how the code is working to create this...but I'm hitting a brick wall on how to mix in the looping through the array and the creating of the labels...

Please help...I'm supposed to have this system up tomorrow AM and just noticed the problem...

Comment
Watch Question

Commented:
Can you post your code up?

It sounds like your have your labels inside the loop.
musickmannData Analyst

Author

Commented:
Duh...I had it copied to the clipboard and ready..totally forgot to paste it.

$this->Cell(40,6,'Employer Name',1);
        $this->Cell(40,6,'Location',1);
        $this->Cell(30,6,'Phone',1);
        $this->Cell(20,6,'From Date',1);
        $this->Cell(20,6,'To Date',1);
        $this->Cell(40,6,'Supervisor',1);
        $this->Ln();
        $this->Cell(40,6,'Position',1);
        $this->Cell(80,6,'Duties',1);
        $this->Cell(60,6,'Reason for leaving',1);
        $this->Ln();
        
        $count = count($_POST['PrevEmpName1']);
        $x=0;
        while($x < $count){
        if($count == 1){
          $emp = $_POST['PrevEmpName1'];
          $emploc = $_POST['PrevEmpLoc1'];
          $empphone = $_POST['PrevEmpPhone1'];
          $empdf = $_POST['PrevEmpDateFrom1'];
          $empdt = $_POST['PrevEmpDateTo1'];
          $empsup = $_POST['PrevEmpSuper1'];
          $empjo = $_POST['PrevEmpJob1'];
          $empjd = $_POST['PrevEmpJobDuty1'];
          $emplv = $_POST['PrevEmpLeave1'];
        }else{
          $emp = $_POST['PrevEmpName1'][$x];
          $emploc = $_POST['PrevEmpLoc1'][$x];
          $empphone = $_POST['PrevEmpPhone1'][$x];
          $empdf = $_POST['PrevEmpDateFrom1'][$x];
          $empdt = $_POST['PrevEmpDateTo1'][$x];
          $empsup = $_POST['PrevEmpSuper1'][$x];
          $empjo = $_POST['PrevEmpJob1'][$x];
          $empjd = $_POST['PrevEmpJobDuty1'][$x];
          $emplv = $_POST['PrevEmpLeave1'][$x];
        }       
        
        $this->Cell(40,6,$emp,1);
        $this->Cell(40,6,$emploc,1);
        $this->Cell(30,6,$empphone,1);
        $this->Cell(20,6,$empdf,1);
        $this->Cell(20,6,$empdt,1);
        $this->Cell(40,6,$empsup,1);
        $this->Ln();
        $this->Cell(40,18,$empjo,1);
        $this->Cell(80,18,$empjd,1);
        $this->Cell(60,18,$emplv,1);
        $this->Ln();
        $x++;
        }

Open in new window

Commented:
Can you post the current output as well?
musickmannData Analyst

Author

Commented:
Here is the generated PDF as well as the serialized post array
1276224885.txt
1276224885.pdf

Commented:
Call me crazy but your PDF looks correct, for the section of code you posted, I do not see anything repeating.  What am I missing?
musickmannData Analyst

Author

Commented:
The employment history section. The first two lines are labels, I'm trying to do a line of labels then the corresponding data on the next line and en the next line of labels. Does that make sense?
Commented:
Gotcha...sorry, slow tonight.

Just move the labels into the loop.  I believe you can simply move them inside but you will have to run it to be sure.

One thing I did notice is that you have a wrapping issue, you may want to check this out (http://www.fpdf.de/downloads/addons/49/)  It is an easy line wrapping function.

        
        $count = count($_POST['PrevEmpName1']);
        $x=0;
        while($x < $count){
        if($count == 1){
          $emp = $_POST['PrevEmpName1'];
          $emploc = $_POST['PrevEmpLoc1'];
          $empphone = $_POST['PrevEmpPhone1'];
          $empdf = $_POST['PrevEmpDateFrom1'];
          $empdt = $_POST['PrevEmpDateTo1'];
          $empsup = $_POST['PrevEmpSuper1'];
          $empjo = $_POST['PrevEmpJob1'];
          $empjd = $_POST['PrevEmpJobDuty1'];
          $emplv = $_POST['PrevEmpLeave1'];
        }else{
          $emp = $_POST['PrevEmpName1'][$x];
          $emploc = $_POST['PrevEmpLoc1'][$x];
          $empphone = $_POST['PrevEmpPhone1'][$x];
          $empdf = $_POST['PrevEmpDateFrom1'][$x];
          $empdt = $_POST['PrevEmpDateTo1'][$x];
          $empsup = $_POST['PrevEmpSuper1'][$x];
          $empjo = $_POST['PrevEmpJob1'][$x];
          $empjd = $_POST['PrevEmpJobDuty1'][$x];
          $emplv = $_POST['PrevEmpLeave1'][$x];
        }       
        $this->Cell(40,6,'Employer Name',1);
        $this->Cell(40,6,'Location',1);
        $this->Cell(30,6,'Phone',1);
        $this->Cell(20,6,'From Date',1);
        $this->Cell(20,6,'To Date',1);
        $this->Cell(40,6,'Supervisor',1);
        $this->Ln();        
        $this->Cell(40,6,$emp,1);
        $this->Cell(40,6,$emploc,1);
        $this->Cell(30,6,$empphone,1);
        $this->Cell(20,6,$empdf,1);
        $this->Cell(20,6,$empdt,1);
        $this->Cell(40,6,$empsup,1);
        $this->Ln();
        $this->Cell(40,6,'Position',1);
        $this->Cell(80,6,'Duties',1);
        $this->Cell(60,6,'Reason for leaving',1);
        $this->Ln();
        $this->Cell(40,18,$empjo,1);
        $this->Cell(80,18,$empjd,1);
        $this->Cell(60,18,$emplv,1);
        $this->Ln();
        $x++;
        }

Open in new window

musickmannData Analyst

Author

Commented:
No apology needed...My brain is mush, I've been staring at this for the longest time thinking, I know how to do this, but I just couldn't get my brain to cooperate. I kept trying to break up the loop...now I just feel like an idiot.

Thanks SO much for helping me out...and I will look at the line breaking thing...this is my first PDF creation...so it was a huge learning curve, I'm sure there's a ton of things that could be done a little better and make it look a little cleaner, so I appreciate the suggestion. I need to work in some multi line fields like address and such as well, but accounting for something that can take a few characters or many is difficult for me, so I just put the address on one line by it self.

Thanks again! This is the perfect example of why I love this site.
musickmannData Analyst

Author

Commented:
Simple and easy. A perfect example of why I love this site!