FPDF multiple columns on one page

Included is my code for displaying in pdf format data from 2 fields in 119 records. I can get the data to display in one long column spread over 3 pages. I want to display the data on 1 page over several columns. I’m stuck; don’t know how to proceed.

<?php

require('fpdf.php');
require('connection.php'); 
class PDF extends FPDF
{
// Page header
function Header()
{
   
}
 
// Page footer
function Footer()
{
// Position at 2.5 cm from bottom
    $this->SetY(-25);
    //Arial italic 8
    $this->SetFont('Arial','',8);
    //Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
} 
 
$pdf = new PDF();

$pdf->AddPage('P','A4');
// Necessary for x of y page numbers to appear in document
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(false);
 
// Document properties 
$pdf->SetFont('Arial','U',14);
$pdf->Cell(40,10,'Class Counts');
 
// Add date report run
$pdf->SetFont('Arial','',10);
$pdf->Setx(10);
$pdf->Sety(5);
$date =  date("F j, Y");
$pdf->Cell(40,30,'Report date: '.$date);
 
$pdf->SetDrawColor(0, 0, 0); //black
 
// Table header
$pdf->SetFillColor(170, 170, 170); //gray
$pdf->setFont("Arial","","10");
$pdf->setXY(10, 29.5);
// Field names
$pdf->Cell(11, 5, "CLS", 1, 0, "C", 1);
$pdf->Cell(11, 5, "CNT", 1, 0, "C", 1);
 
$y = 35;
$x = 10;  
 
$pdf->setXY($x, $y);
 
// Configure connection script
$query="SELECT * FROM class_counts ORDER BY `Class` ASC LIMIT 0, 500 ";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$total_count = 0;
while($row = mysql_fetch_array($result))

{
// Field data
  		$pdf->SetFillColor(198, 245, 175);
		$pdf->setFont("Arial","","8");
        $pdf->Cell(11, 5, $row['Class'], '', '','C', 1);
        $pdf->Cell(11, 5, $row['Count'], '', '','C', 1);
        $y += 5.5;
        
// If page break needed
        if ($y > 275)
		{
            $pdf->AddPage('P','A4');
            $y = 10;
			
		}      

        $pdf->setXY($x, $y);
        
// Total entries
$total_count += $row['Count'];
}

$pdf->SetX(23);
$pdf->setFont("Arial","B","10");
$pdf->Cell(40,15,"$total_count".' entries','','','L');

$pdf->Output();

The easiest way of breaking data into columns is to retrieve all the data into an array, then use array_chunk() to produce an array of arrays of the size you want. You would then just loop over the resulting array of arrays to produce the output that you want.

I’m new to this; don’t know how to break data into arrays. Can you give specifics related to my code?

The php documentation shows how to use array_chunk - https://secure.php.net/array_chunk

You would need to calculate the number of rows per column based on the total amount of data and the number of columns. Assuming you have fetched all the data into an array named $data -

$columns = 3; // number of columns on a page
// calculate number of rows and break the data into chunks
$num_rows = ceil(count($data) / $columns);
$data = array_chunk($data,$num_rows);

You can use print_r() on the resulting $data array to see what it looks like. You would just use two nested foreach(){} loops to loop over the data to produce the columns on the page. The inner loop would produce one column and would consist of your current code that produces the single column output. After the end of the inner loop, the outer loop would set the XY position to be the start of the next column, then run the inner loop again.

Sponsor our Newsletter | Privacy Policy | Terms of Service