简体   繁体   中英

FPDF - How to change cell size in a foreach php

http://phppot.com/php/generate-pdf-from-mysql-data-using-fpdf/ I use this code, but it doesn´t work.

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

i wanted to change the foreach command, i want to check the mysql, and when the $header is 'fun', i want the cell a little bit smaller than the other ones.

I try it with if, but it doesn´t work Please help!

To decrease the width of the cell as per your condition, you can do something like this:

\n

 
 
 
  
  // 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
 
  

Edited:

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

Here's the reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM