【问题标题】:separating tables values into different cells将表格值分隔到不同的单元格中
【发布时间】:2019-06-27 15:57:20
【问题描述】:

所以我已经开始掌握使用 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 行,并且值的日期是:

22/09/2002 和 10/10/18

这些只会显示在与 22/09/12、10/10/18 相同的单元格上

那么我将如何更改它,以便第二个值等等会出现在第一个单元格下方?

希望这是有道理的

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

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


}

【问题讨论】:

    标签: php html fpdf


    【解决方案1】:

    假设您的表单检查确保每行中的所有 4 个字段都已完成,您可以简单地使用 foreach 浏览所有内容并将其添加到 PDF。

    首先,不要内爆您的 _POST 数组。让它们保持原样,您就可以使用foreach 来查看它们。

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

    如果您想将 from、to 和 schoolName 数据放在一行中,您可以使用以下内容:

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

    【讨论】:

    • 您好,抱歉,但是通过这种方式,$from 值只会出现在它旁边的单元格上,我将如何更改它以使其位于其下方?我认为它可能会添加 $pdf->Ln();最后,但这也会导致第一个 $to、$schoolName 和 $examResults 换行,而不是在第一个 $from 值旁边
    • 查看我的答案的编辑。将单元格调用的第 5 个参数更改为 1 以移动到下一行。您对cell 的调用是告诉 fPDF 在输出内容的末尾保持在同一行。
    • 这使得第二个 $from 值进入下一行,但随后其他值出现在其下方,是 $to、$schoolName 和 $examResults 的另一个 foreach 还是我做错了?
    • 我已经编辑了我的问题以显示我现在在做什么,你介意看看我哪里出错了吗?
    • 我已经设法让它工作了,我已经用我所做的编辑了我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多