source: trunk/phpgwapi/inc/fpdf/tutorial/tuto5.php @ 7673

Revision 7673, 2.2 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2define('FPDF_FONTPATH','../font/');
3require('../fpdf.php');
4
5class PDF extends FPDF
6{
7//Load data
8function LoadData($file)
9{
10        //Read file lines
11        $lines=file($file);
12        $data=array();
13        foreach($lines as $line)
14                $data[]=explode(';',chop($line));
15        return $data;
16}
17
18//Simple table
19function BasicTable($header,$data)
20{
21        //Header
22        foreach($header as $col)
23                $this->Cell(40,7,$col,1);
24        $this->Ln();
25        //Data
26        foreach($data as $row)
27        {
28                foreach($row as $col)
29                        $this->Cell(40,6,$col,1);
30                $this->Ln();
31        }
32}
33
34//Better table
35function ImprovedTable($header,$data)
36{
37        //Column widths
38        $w=array(40,35,40,45);
39        //Header
40    $header_count = count($header);
41        for($i=0;$i<$header_count;++$i)
42                $this->Cell($w[$i],7,$header[$i],1,0,'C');
43        $this->Ln();
44        //Data
45        foreach($data as $row)
46        {
47                $this->Cell($w[0],6,$row[0],'LR');
48                $this->Cell($w[1],6,$row[1],'LR');
49                $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
50                $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
51                $this->Ln();
52        }
53        //Closure line
54        $this->Cell(array_sum($w),0,'','T');
55}
56
57//Colored table
58function FancyTable($header,$data)
59{
60        //Colors, line width and bold font
61        $this->SetFillColor(255,0,0);
62        $this->SetTextColor(255);
63        $this->SetDrawColor(128,0,0);
64        $this->SetLineWidth(.3);
65        $this->SetFont('','B');
66        //Header
67        $w=array(40,35,40,45);
68    $header_count = count($header);
69        for($i=0;$i<$header_count;++$i)
70                $this->Cell($w[$i],7,$header[$i],1,0,'C',1);
71        $this->Ln();
72        //Color and font restoration
73        $this->SetFillColor(224,235,255);
74        $this->SetTextColor(0);
75        $this->SetFont('');
76        //Data
77        $fill=0;
78        foreach($data as $row)
79        {
80                $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
81                $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
82                $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
83                $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
84                $this->Ln();
85                $fill=!$fill;
86        }
87        $this->Cell(array_sum($w),0,'','T');
88}
89}
90
91$pdf=new PDF();
92//Column titles
93$header=array('Country','Capital','Area (sq km)','Pop. (thousands)');
94//Data loading
95$data=$pdf->LoadData('countries.txt');
96$pdf->SetFont('Arial','',14);
97$pdf->AddPage();
98$pdf->BasicTable($header,$data);
99$pdf->AddPage();
100$pdf->ImprovedTable($header,$data);
101$pdf->AddPage();
102$pdf->FancyTable($header,$data);
103$pdf->Output();
104?>
Note: See TracBrowser for help on using the repository browser.