Search code examples
phphtmlfpdf

separating tables values into different cells


So I have started to get to grip with using fpdf but one thing I am not sure about is separating array values onto difference cells in the pdf form.

This is the part of the form that I am having issues with. The form itself is okay and goes through my php file.

<table class="qualifications" id="qualifications" >
  <tr>
  <td><b>Date From</b></td>
  <td><b>Date To</b></td>
  <td><b>Name of School/College/Institute or Professional Body</b></td>
  <td><b>Examinations Taken and Results</b></td>
  </tr>
  <tr>
  <td><input type="text" name="date_from[]" id="date_from" /></td>
  <td><input type="text" name="date_to[]" id="date_to"/></td>
  <td><input type="text" name="school_name[]" id="school_name" /></td>
  <td><input type="text" name="results[]" id="results"/></td>
  </tr>
</table>
<a href="#" title="" class="add-row">Add Row</a>

The user is able to add as many rows as they like using the add row link

This is how I get the values from the table:

if( isset($_POST['date_from']) && is_array($_POST['date_from']) ) {
    $from = implode(', ', $_POST['date_from']);

}

if( isset($_POST['date_to']) && is_array($_POST['date_to']) ) {
    $to = implode(', ', $_POST['date_to']);

}

if( isset($_POST['school_name']) && is_array($_POST['school_name']) ) {
    $schoolName = implode(', ', $_POST['school_name']);

}

if( isset($_POST['results']) && is_array($_POST['results']) ) {
    $examResults = implode(', ', $_POST['results']);

}

When it comes to populating the cell in the pdf form I have been doing something like:

 $pdf->Cell($width_cell[0],10,$from,1,0,'C'); 

But this just keeps each value entered into the table in one cell

Say for example if someone had entered 2 rows into the qualifications table and the date from values were:

22/09/2002 and 10/10/18

These would just display on the same cell as 22/09/12, 10/10/18

So how would I go about changing it so that the second value and so on would go beneath the first cell instead?

Hope this made sense

Edit: This is how I am now populating the cells:

  $width_cell=array(30,25,50,90);

$pdf->Cell($width_cell[0],10,'Date From',1,0); // First header column 
$pdf->Cell($width_cell[1],10,'Date To',1,0); // Second header column
$pdf->Cell($width_cell[2],10,'Name of School',1,0); // Third header column 
$pdf->Cell($width_cell[3],10,'Examinations Taken & Results',1,1); // Fourth header column

  $pdf->SetFont('Arial','',10);
 foreach ($from as $point => $data) {
 $pdf->Cell($width_cell[0],10,$data,1,1,'C');

}

 foreach ($to as $point => $data) {
    $pdf->Cell($width_cell[1],10,$data,1,1,'C');

 }

 foreach ($schoolName as $point => $data) {
    $pdf->Cell($width_cell[2],10,$data,1,1,'C');

 }   


 foreach ($examResults as $point => $data) {
    $pdf->Cell($width_cell[3],10,$data,1,1,'C');    

 }

Edit 2: I have now got it working, I changed the way I populated the cells to:

$pdf->Cell($width_cell[0],10,'Date From',1,0); // First header column 
$pdf->Cell($width_cell[1],10,'Date To',1,0); // Second header column
$pdf->Cell($width_cell[2],10,'Name of School',1,0); // Third header column 
$pdf->Cell($width_cell[3],10,'Examinations Taken & Results',1,1); // Fourth header column

  $pdf->SetFont('Arial','',10);
 foreach ($from as $point => $data) {
  $pdf->Cell($width_cell[0],10,$data,1,0,'C'); 
  $pdf->Cell($width_cell[1],10,$to[$point],1,0,'C');
  $pdf->Cell($width_cell[2],10,$schoolName[$point],1,0,'C');
  $pdf->Cell($width_cell[3],10,$examResults[$point],1,1,'C');


}

Solution

  • Assuming your form checking ensures that all 4 of the fields are completed in each row you may simply use foreach to go through everything and add it to the PDF.

    First, don't implode your _POST arrays. Leave them intact and you'll be able to use foreach to go through them.

    $from        = (isset($_POST['date_from'])   ? $_POST['date_from']   : array());
    $to          = (isset($_POST['date_to'])     ? $_POST['date_to']     : array());
    $schoolName  = (isset($_POST['school_name']) ? $_POST['school_name'] : array());
    $examResults = (isset($_POST['results'])     ? $_POST['results']     : array());
    
    foreach ($from as $point => $data) {
    
        $pdf->Cell($width_cell[0],10,$data,1,1,'C');
        // if you want to put the "to" in another cell it would be referenced
        // as $to[$point] or $schoolName[$point] etc
    }
    

    If you wanted to put the from, to and schoolName data on one line you could use something like:

    $pdf->Cell($width_cell[0],10,'From: '        . $data       . 
                                 ' To: '         . $to[$point] . 
                                 ' School Name ' . $schoolName[$point],1,1,'C');