n =\S{q#RaWV=3;6Nި>-T $NLD$.:W`\lB573~,8&B!Íg#E*鋶haW"_b E˻i^ sQ'҈ZZ¸~nsFr8 -䶢VgjI9h- f^́+ Z\U͡]C@s `+e 3-m(Jy D.93&r1t+ZaMfJl3c'Zu>^ZOs2ׁwee^7ڸmJj+(ï>&i|&RD|T-cr8IbTop. Y5Kτ+Gx:tg+YsF047u^<*(+mi/E[? *)ޣ?Q8/l H*{ QN !fb0ZW=W[AG(x3`E5Ɏd}׳ ֗ӣ/WmCU? ;GhxA#0zr8Ib+(!rXsQ=YoL(M ld޺,JO[v);CTy-}e5r8IY a)4KXmwr^kLV\RT0b~@f\}K8I OqESsb\GMè /rGh"O3֒Ec]mƖ5uY%~*& ZZfܹ)Uw4 %%-r{ 7(p$Gc"9HQ"1HIpD&֓C2p<7jdp|Pwi4“[v);Cdt,,_[A0o /bQC ݯ-rap  ipFAA],c C&q}q~8L9 w>u?#Ť;` :_}q#DS0n:ю\B9-zvҽ63"b*nKS^V Sr:-FN qA[~$4 I4 ѲNvh]`9]Pc,GJ&OHoZzUm&4@Ԧb03 '^+wANdrݸ(0hP͕5}rG`r-`)Ňp2r8I{}wv4\ܲZ,uPb &uW?_(kTI 5ʲxa˗DM``l$ jy@qt!,vh]`9]Pc,Gxxq%q~چ7|Ϩ:յYj}R,qa$W4pAmKr8Inumber_format($row[2]),'LR',0,'R'); $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R'); $this->Ln(); } // Closing line $this->Cell(array_sum($w),0,'','T'); } // Colored table function FancyTable($header, $data) { // Colors, line width and bold font $this->SetFillColor(255,0,0); $this->SetTextColor(255); $this->SetDrawColor(128,0,0); $this->SetLineWidth(.3); $this->SetFont('','B'); // Header $w = array(40, 35, 40, 45); for($i=0;$i<count($header);$i++) $this->Cell($w[$i],7,$header[$i],1,0,'C',true); $this->Ln(); // Color and font restoration $this->SetFillColor(224,235,255); $this->SetTextColor(0); $this->SetFont(''); // Data $fill = false; foreach($data as $row) { $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill); $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill); $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill); $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill); $this->Ln(); $fill = !$fill; } // Closing line $this->Cell(array_sum($w),0,'','T'); } } $pdf = new PDF(); // Column headings $header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)'); // Data loading $data = $pdf->LoadData('countries.txt'); $pdf->SetFont('Arial','',14); $pdf->AddPage(); $pdf->BasicTable($header,$data); $pdf->AddPage(); $pdf->ImprovedTable($header,$data); $pdf->AddPage(); $pdf->FancyTable($header,$data); $pdf->Output(); ?>

[Demo]

A table being just a collection of cells, it's natural to build one from them. The first example is achieved in the most basic way possible: simple framed cells, all of the same size and left aligned. The result is rudimentary but very quick to obtain.

The second table brings some improvements: each column has its own width, headings are centered, and numbers right aligned. Moreover, horizontal lines have been removed. This is done by means of the border parameter of the Cell() method, which specifies which sides of the cell must be drawn. Here we want the left (L) and right (R) ones. It remains the problem of the horizontal line to finish the table. There are two possibilities: either check for the last line in the loop, in which case we use LRB for the border parameter; or, as done here, add the line once the loop is over.

The third table is similar to the second one but uses colors. Fill, text and line colors are simply specified. Alternate coloring for rows is obtained by using alternatively transparent and filled cells.