Search code examples
phpfpdf

Bold the last row in table


How can I make the last row Bold in fpdf?

here is my code

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();
            }
        }

Updated Code : Added conditions for the rows until reach the last row.

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();
        }

The sample code above creates the row and column my target is to make the last row bold


Solution

  • Simply, you can count() rows and then check if current processed row is the last one.

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

    Here is the working code.

    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();
                }