将表值分成不同的单元格

时间:2019-06-27 15:57:20

标签: php html fpdf

所以我已经开始使用fpdf了,但是我不确定的一件事是将数组值分离到pdf形式的不同单元格中。

这是我遇到问题的表格的一部分。表单本身还可以,并通过我的php文件。

<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>

用户可以使用“添加行”链接添加任意多的行

这是我从表中获取值的方法:

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']);

}

以pdf格式填充单元格时,我一直在做类似的事情:

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

但这只是将输入到表中的每个值保留在一个单元格中

例如,假设有人在资格表中输入了2行,而值的日期为:

2002年9月22日和18/10/10

这些只会显示在12年9月22日,18/10/10的同一单元格中

那么我将如何更改它,以使第二个值等等位于第一个单元格下面?

希望这很有意义

编辑:这是我现在填充单元格的方式:

  $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');    

 }

编辑2:现在,它开始工作了,我将填充单元格的方式更改为:

$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');


}

1 个答案:

答案 0 :(得分:2)

假设您要进行表单检查,以确保每行中的所有4个字段都都完整填写,您可以简单地使用if (MessageBox.Show("test", "Confirm", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Question) == MessageBoxResult.Yes) { // user clicked yes } else { // user clicked no } 浏览所有内容并将其添加到PDF。

首先,不要破坏您的foreach数组。保持它们完好无损,您将可以使用_POST浏览它们。

foreach

如果您希望将from,to和schoolName数据放在一行上,则可以使用以下内容:

$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
}
相关问题