【问题标题】:FPDF - How to change cell size in a foreach phpFPDF - 如何在 foreach php 中更改单元格大小
【发布时间】:2016-04-04 11:50:46
【问题描述】:

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 是“有趣”时,我希望单元格比其他单元格小一点。

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

【问题讨论】:

  • ...我希望单元格比其他单元格小一点。 单元格大小是什么意思?你想减小单元格的高度还是单元格的宽度?还是两者兼而有之?
  • 应该是多少?
  • 细胞!带有标题“有趣”的应该是 45,而不是 90
  • 请看我的回答。

标签: php mysql pdf include fpdf


【解决方案1】:

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

罢工>

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

这是参考:

【讨论】:

  • 我想减少整列;标题和列中的所有单元格!
  • @J.Doe 整列、标题和列中的所有单元格是什么意思。请提供您的 PDF 屏幕截图。
  • @J.Doe 我认为在问题中你的意思是当 $column_heading 是“有趣”时,我想要...,因为 $header 是一个数组并且您无法将数组与字符串 'fun' 进行比较。
  • 我在那里看不到任何屏幕截图,链接已损坏。我已经在我的本地系统上重新创建了您的问题。 Here's the screenshot。现在告诉我你想在哪里进行更改?
  • 我有 3 列,第三列应该比其他列小。这只是一个例子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
相关资源
最近更新 更多