Rijen om en om kleuren met TCPDF
Als ik naar hun voorbeelden kijk, vooral voorbeeld 11 spreekt mij enorm aan, is het maken van een table met om en om kleuren erin.
Zij gebruiken in hun functie deze code ervoor.:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Colored table
public function ColoredTable1($header1,$data1) {
// Colors, line width and bold font
$this->SetFillColor(255, 0, 0);
$this->SetTextColor(255);
$this->SetDrawColor(128, 0, 0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Header
$w = array(15, 15, 150); //kolom 1 breedte,kolom 2 breedte,kolom 3 breedte,kolom 4 breedte,kolom 5 breedte, enz.
$num_headers = count($header1);
for($i = 0; $i < $num_headers; ++$i) {
$this->Cell($w[$i], 7, $header1[$i], 1, 0, 'C', 1);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(224, 235, 255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = 0;
foreach($data1 as $row) {
$this->Cell($w[0], 6, $row[0], 'LR', 0, 'C', $fill); //L=links, R=Rechts, C=Center
$this->Cell($w[1], 6, $row[1], 'LR', 0, 'C', $fill); //L=links, R=Rechts, C=Center
$this->Cell($w[2], 6, $row[2], 'LR', 0, 'L', $fill); //L=links, R=Rechts, C=Center
//$this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w), 0, '', 'T'); //Afsluit lijn van table
}
public function ColoredTable1($header1,$data1) {
// Colors, line width and bold font
$this->SetFillColor(255, 0, 0);
$this->SetTextColor(255);
$this->SetDrawColor(128, 0, 0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B');
// Header
$w = array(15, 15, 150); //kolom 1 breedte,kolom 2 breedte,kolom 3 breedte,kolom 4 breedte,kolom 5 breedte, enz.
$num_headers = count($header1);
for($i = 0; $i < $num_headers; ++$i) {
$this->Cell($w[$i], 7, $header1[$i], 1, 0, 'C', 1);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(224, 235, 255);
$this->SetTextColor(0);
$this->SetFont('');
// Data
$fill = 0;
foreach($data1 as $row) {
$this->Cell($w[0], 6, $row[0], 'LR', 0, 'C', $fill); //L=links, R=Rechts, C=Center
$this->Cell($w[1], 6, $row[1], 'LR', 0, 'C', $fill); //L=links, R=Rechts, C=Center
$this->Cell($w[2], 6, $row[2], 'LR', 0, 'L', $fill); //L=links, R=Rechts, C=Center
//$this->Cell($w[3], 6, number_format($row[3]), 'LR', 0, 'R', $fill);
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w), 0, '', 'T'); //Afsluit lijn van table
}
Iets verder gebruiken ze dan in hun code.:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
// set font
$pdf->SetFont('helvetica', '', 8);
// add a page
$pdf->AddPage();
//plaats een test text
$pdf->Write(0, 'test 1', '', 0, '', true, 0, false, false, 0);
// column1 titles
$header = array('Van', 'Tot', 'Opmerking');
// data loading
$data = $pdf->LoadData('data/table_data_demo.txt');
// print colored table
$pdf->ColoredTable($header, $data);
$pdf->SetFont('helvetica', '', 8);
// add a page
$pdf->AddPage();
//plaats een test text
$pdf->Write(0, 'test 1', '', 0, '', true, 0, false, false, 0);
// column1 titles
$header = array('Van', 'Tot', 'Opmerking');
// data loading
$data = $pdf->LoadData('data/table_data_demo.txt');
// print colored table
$pdf->ColoredTable($header, $data);
Dit is voor mij de ideale uitvoer wat ik in de pdf wil zien.
Echter nu is mijn vraag, heeft dit iemand ook al een getest maar met gebruik van gegevens uit sql ipv die text file?
Ik heb al verschillende codes geprobeerd, maar niks werkt.
Ik kom nu alleen met een werkende uit zonder gekleurde mooie balken met deze code.:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$tbl_header = '<table border="1">';
$tbl_footer = '</table>';
$tbl ='';
$con=mysqli_connect("localhost","root","xxxxx","xxxxxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM metingen LIMIT 10");
while($row = mysqli_fetch_array($result))
{
$id = $row['Id'];
$key = $row['Orders'];
$key1 = $row['Batch_Nr'];
$tbl .= '<tr><td>' . $id . '</td><td>' . $key . '</td><td>' . $key1 . '</td></tr>';
}
// Print text using writeHTMLCell()
$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
// close and output PDF document
$pdf->Output('Dienstrapport', 'I');
$tbl_footer = '</table>';
$tbl ='';
$con=mysqli_connect("localhost","root","xxxxx","xxxxxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM metingen LIMIT 10");
while($row = mysqli_fetch_array($result))
{
$id = $row['Id'];
$key = $row['Orders'];
$key1 = $row['Batch_Nr'];
$tbl .= '<tr><td>' . $id . '</td><td>' . $key . '</td><td>' . $key1 . '</td></tr>';
}
// Print text using writeHTMLCell()
$pdf->writeHTML($tbl_header . $tbl . $tbl_footer, true, false, false, false, '');
// close and output PDF document
$pdf->Output('Dienstrapport', 'I');
Gewijzigd op 02/04/2019 12:48:53 door - Ariën -
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
$i = 1;
while(......)
{
if($i % 2)
{
// Even rijnummer
}else{
// Oneven rijnummer
}
$i++;
}
while(......)
{
if($i % 2)
{
// Even rijnummer
}else{
// Oneven rijnummer
}
$i++;
}
Gewijzigd op 02/04/2019 12:44:53 door - Ariën -
Er zijn inderdaad meerdere wegen die naar Rome leiden.
Mijn code nu is.:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$tbl_style = '<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:10px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#000000;}
.tg th{font-family:Arial, sans-serif;font-size:10px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#000000;}
.tg .tg-obcb{font-size:16px;background-color:#6665cd;border-color:#000000;text-align:center;vertical-align:middle}
.tg .tg-e4il{font-size:16px;background-color:#6665cd;border-color:#000000;text-align:center;vertical-align:middle}
.tg .tg-cey4{font-size:10px;border-color:#000000;text-align:left;vertical-align:middle}
.tg .tg-cey5{font-size:12px;border-color:#000000;text-align:center;vertical-align:middle}
.tg .tg-cey6{font-size:12px;background-color:#6665cd;color:#ffffff;border-color:#000000;text-align:center;vertical-align:middle}
.tg .tg-0pky{border-color:#000000;text-align:center;vertical-align:middle}
</style>';
$tbl_header = '<table border="1" class="tg">';
$tbl_columns = '<tr><th class="tg-cey6" width="53" height="20" border="1">Van</th><th class="tg-cey6" width="53" height="20" border="1">Tot</th><th class="tg-cey6" width="532" height="20" border="1">Opmerking</th></tr>';
$tbl_footer = '</table>';
//$tbl ='';
$con=mysqli_connect("localhost","root","xxxxx","xxxxxxxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Prep1Opm LIMIT 10");
$i = 0;
$color="1";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$key = $row['StartTijd'];
$key1 = $row['EindTijd'];
$key2 = $row['Opmerking'];
if($color == 1){
$tbl .= '<tr bgcolor="#FFFFFF" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$color="2";
}else {
$tbl .= '<tr bgcolor="#EEEEEE" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$color="1";
}}
// Print text using writeHTMLCell()
$pdf->writeHTML($tbl_style . $tbl_header . $tbl_columns . $tbl . $tbl_footer, true, false, false, false, '');
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:10px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#000000;}
.tg th{font-family:Arial, sans-serif;font-size:10px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#000000;}
.tg .tg-obcb{font-size:16px;background-color:#6665cd;border-color:#000000;text-align:center;vertical-align:middle}
.tg .tg-e4il{font-size:16px;background-color:#6665cd;border-color:#000000;text-align:center;vertical-align:middle}
.tg .tg-cey4{font-size:10px;border-color:#000000;text-align:left;vertical-align:middle}
.tg .tg-cey5{font-size:12px;border-color:#000000;text-align:center;vertical-align:middle}
.tg .tg-cey6{font-size:12px;background-color:#6665cd;color:#ffffff;border-color:#000000;text-align:center;vertical-align:middle}
.tg .tg-0pky{border-color:#000000;text-align:center;vertical-align:middle}
</style>';
$tbl_header = '<table border="1" class="tg">';
$tbl_columns = '<tr><th class="tg-cey6" width="53" height="20" border="1">Van</th><th class="tg-cey6" width="53" height="20" border="1">Tot</th><th class="tg-cey6" width="532" height="20" border="1">Opmerking</th></tr>';
$tbl_footer = '</table>';
//$tbl ='';
$con=mysqli_connect("localhost","root","xxxxx","xxxxxxxxx");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Prep1Opm LIMIT 10");
$i = 0;
$color="1";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$key = $row['StartTijd'];
$key1 = $row['EindTijd'];
$key2 = $row['Opmerking'];
if($color == 1){
$tbl .= '<tr bgcolor="#FFFFFF" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$color="2";
}else {
$tbl .= '<tr bgcolor="#EEEEEE" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$color="1";
}}
// Print text using writeHTMLCell()
$pdf->writeHTML($tbl_style . $tbl_header . $tbl_columns . $tbl . $tbl_footer, true, false, false, false, '');
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
if ($color == 1) {
$tbl .= '<tr bgcolor="#FFFFFF" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$color="2";
} else {
$tbl .= '<tr bgcolor="#EEEEEE" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$color="1";
}
$tbl .= '<tr bgcolor="#FFFFFF" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$color="2";
} else {
$tbl .= '<tr bgcolor="#EEEEEE" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$color="1";
}
Kun je natuurlijk reduceren tot:
Code (php)
1
2
3
4
2
3
4
$fill = 0;
$bgColor = ($fill ? '#eeeeee' : '#ffffff');
$tbl .= '<tr bgcolor="'.$bgColor.'" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$fill = 1 - $fill;
$bgColor = ($fill ? '#eeeeee' : '#ffffff');
$tbl .= '<tr bgcolor="'.$bgColor.'" border="1"><td width="53" height="19" class="tg-0pky" border="1">' . $key . '</td><td width="53" height="17" class="tg-0pky" border="1">' . $key1 . '</td><td width="532" height="17" class="tg-cey4" border="1">' . $key2 . '</td></tr>';
$fill = 1 - $fill;
Het enige wat verschilt is de achtergrondkleur. Beetje zonde om een hoop code te dupliceren. Vooral als je die later weer wilt wijzigen, dan zou je die in de oorspronkelijke opzet op twee plaatsen moeten aanpassen.
Gewijzigd op 02/04/2019 22:15:13 door Thomas van den Heuvel