FPDF-如何在foreach php中更改单元格大小


FPDF - How to change cell size in a foreach php

http://phppot.com/php/generate-pdf-from-mysql-data-using-fpdf/我使用了这个代码,但它不起作用。

    <?php
require_once("dbcontroller.php");
$db_handle = new DBController();
$result = $db_handle->runQuery("SELECT * FROM toy");
$header = $db_handle->runQuery("SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='blog_samples' 
    AND `TABLE_NAME`='toy'");
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);      
foreach($header as $heading) {
    foreach($heading as $column_heading)
        $pdf->Cell(90,12,$column_heading,1);
}
foreach($result as $row) {
    $pdf->SetFont('Arial','',12);   
    $pdf->Ln();
    foreach($row as $column)
        $pdf->Cell(90,12,$column,1);
}
$pdf->Output();
?>

我想更改foreach命令,我想检查mysql,当$header是'fun'时,我希望这个单元格比其他单元格小一点。

我试着用if,但它不起作用请帮忙!

要根据您的情况减小单元格的宽度,您可以执行以下操作:

// your code
foreach($header as $heading){
    foreach($heading as $column_heading){
        if($column_heading == "fun"){
            $pdf->Cell(45,12,$column_heading,1);
        }else{
            $pdf->Cell(90,12,$column_heading,1);
        }
    }
}
// your code

编辑:

// your code
$pdf->SetFont('Arial','B',12); 
foreach($header as $heading){
    $counter = 1;
    foreach($heading as $column_heading){
        if($counter > 3){
             $pdf->Cell(45,12,$column_heading,1);
        }else{
             $pdf->Cell(90,12,$column_heading,1);
        }
        ++$counter;
    }
}
foreach($result as $row){
    $counter = 1;
    $pdf->SetFont('Arial','',12);   
    $pdf->Ln();
    foreach($row as $column){
        if($counter > 3){
            $pdf->Cell(45,12,$column,1);
        }else{
            $pdf->Cell(90,12,$column,1);
        }
        ++$counter;
    }
}
$pdf->Output();

以下是参考资料:

  • FPDF Cell文件