加粗表格中的最后一行

时间:2017-11-22 10:00:40

标签: php fpdf

如何在Bold中制作最后一行fpdf

这是我的代码

foreach ($data as $row) {
            $cnt = 0;
                foreach ($row as $col) {
                    if($cnt < 1){
                        $this->Cell(9,5,$col,1,0,'C');
                    } elseif ($cnt < 2) {
                        $this->Cell(18,5,$col,1,0,'C');
                    } else {
                        $this->Cell(18,5, $col, 1, 0,'R'); 
                    }
                    $cnt++;
                }
              $this->Ln();
            }
        }

更新的代码:添加了行的条件,直到到达最后一行。

foreach ($data as $row) {
            $cnt = 0;
            if ($currentRow === $totalRows) {
                $this->SetFont('Arial','B'); 
                foreach ($row as $col) {
                    if($cnt < 1){
                        $this->Cell(9,5,$col,1,0,'C');
                    } elseif ($cnt < 2) {
                        $this->Cell(18,5,$col,1,0,'C');
                    } else {
                        $this->Cell(18,5, $col, 1, 0,'R'); 
                    }
                    $cnt++;
                }

            } else {
                $this->SetFont('Arial'); 
                foreach ($row as $col) {
                    if($cnt < 1){
                        $this->Cell(9,5,$col,1,0,'C');
                    } elseif ($cnt < 2) {
                        $this->Cell(18,5,$col,1,0,'C');
                    } else {
                        $this->Cell(18,5, $col, 1, 0,'R'); 
                    }
                    $cnt++;
                }
            }
            $currentRow++;
            $this->Ln();
        }

上面的示例代码创建了我的目标是使最后一行变为粗体

的行和列

1 个答案:

答案 0 :(得分:0)

简单地说,您可以count()行,然后检查当前处理的行是否是最后一行。

$totalRows = count($data);
$currentRow = 1;
foreach ($data as $row) {
    // (...)
    if ($currentRow === $totalRows) {
        // this is the last row
    }
    $currentRow++;
}

这是工作代码。

foreach ($data as $row) {
                $cnt = 0;
                if ($currentRow === $totalRows) {
                    $this->SetFont('Arial','B'); 
                    foreach ($row as $col) {
                        if($cnt < 1){
                            $this->Cell(9,5,$col,1,0,'C');
                        } elseif ($cnt < 2) {
                            $this->Cell(18,5,$col,1,0,'C');
                        } else {
                            $this->Cell(18,5, $col, 1, 0,'R'); 
                        }
                        $cnt++;
                    }

                } else {
                    $this->SetFont('Arial'); 
                    foreach ($row as $col) {
                        if($cnt < 1){
                            $this->Cell(9,5,$col,1,0,'C');
                        } elseif ($cnt < 2) {
                            $this->Cell(18,5,$col,1,0,'C');
                        } else {
                            $this->Cell(18,5, $col, 1, 0,'R'); 
                        }
                        $cnt++;
                    }
                }
                $currentRow++;
                $this->Ln();
            }