Empty check
Hallo iedereen, ik ben net nieuw op het forum en begin net met php, dus hoop dat dit ik dit topic op de goeie plek post.
Ik ben bezig om een factuur systeem te maken, waarin je naam, bedrijfsnaam en adres kunt invullen, en hij deze waardes kopieert naar een pdf bestand.
Dat is mij al gelukt, echter is het ook mogelijk om werkzaamheden,kosten en aantal toe te voegen.
Nu wil ik dat, mijn script alleen de velden kopieer naar de pdf, waar ook daadwerkelijk iets ingevuld is. Zodat ik geen witregels in mijn pdf krijg.
Hoop dat ik het zo goed mogelijk heb omschreven en dat iemand mij even op weg kan helpen, alvast bedankt!
Groeten, Thierry
Ik ben bezig om een factuur systeem te maken, waarin je naam, bedrijfsnaam en adres kunt invullen, en hij deze waardes kopieert naar een pdf bestand.
Dat is mij al gelukt, echter is het ook mogelijk om werkzaamheden,kosten en aantal toe te voegen.
Nu wil ik dat, mijn script alleen de velden kopieer naar de pdf, waar ook daadwerkelijk iets ingevuld is. Zodat ik geen witregels in mijn pdf krijg.
Hoop dat ik het zo goed mogelijk heb omschreven en dat iemand mij even op weg kan helpen, alvast bedankt!
Groeten, Thierry
Kan je de code geven waarmee je de velden in de PDF zet?
Een leeg-check:
Niet empty() gebruiken, want 0 is dan ook leeg
Een leeg-check:
Niet empty() gebruiken, want 0 is dan ook leeg
Dat kan ik wel :), ik heb alleen paar aanpassingen gedaan hoor.
Het script waar hij alles naar pdf kopieert, komt ergens anders vandaan.
Alvast bedankt!
Het script waar hij alles naar pdf kopieert, komt ergens anders vandaan.
Alvast bedankt!
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// variables
$bedrijfsnaam = $_POST["bedrijfsnaam"];
$naam = $_POST["naam"];
$adres = $_POST["adres"];
$postcode = $_POST["postcode"];
$plaats = $_POST["plaats"];
$refNummer = $_POST["refNummer"];
//arrays
$product[0] = $_POST["product[0]"];
$product[1] = $_POST["product[1]"];
$product[2] = $_POST["product[2]"];
$product[3] = $_POST["product[3]"];
$product[4] = $_POST["product[4]"];
$product[5] = $_POST["product[5]"];
$product[6] = $_POST["product[6]"];
$product[7] = $_POST["product[7]"];
$product[8] = $_POST["product[8]"];
$product[9] = $_POST["product[9]"];
$product[10] = $_POST["product[10]"];
$product[11] = $_POST["product[11]"];
$product[12] = $_POST["product[12]"];
$product[13] = $_POST["product[13]"];
$product[14] = $_POST["product[14]"];
$product[15] = $_POST["product[15]"];
$product[16] = $_POST["product[16]"];
$product[17] = $_POST["product[17]"];
$product[18] = $_POST["product[18]"];
$product[19] = $_POST["product[19]"];
$product[20] = $_POST["product[20]"];
$product[21] = $_POST["product[21]"];
$product[22] = $_POST["product[22]"];
$product[23] = $_POST["product[23]"];
$product[24] = $_POST["product[24]"];
$product[25] = $_POST["product[25]"];
$product[26] = $_POST["product[26]"];
$product[27] = $_POST["product[27]"];
$product[28] = $_POST["product[28]"];
$product[29] = $_POST["product[29]"];
$aantal[0] = $_POST["aantal[0]"];
$aantal[1] = $_POST["aantal[1]"];
$aantal[2] = $_POST["aantal[2]"];
$aantal[3] = $_POST["aantal[3]"];
$aantal[4] = $_POST["aantal[4]"];
$aantal[5] = $_POST["aantal[5]"];
$aantal[6] = $_POST["aantal[6]"];
$aantal[7] = $_POST["aantal[7]"];
$aantal[8] = $_POST["aantal[8]"];
$aantal[9] = $_POST["aantal[9]"];
$aantal[10] = $_POST["aantal[10]"];
$aantal[11] = $_POST["aantal[11]"];
$aantal[12] = $_POST["aantal[12]"];
$aantal[13] = $_POST["aantal[13]"];
$aantal[14] = $_POST["aantal[14]"];
$aantal[15] = $_POST["aantal[15]"];
$aantal[16] = $_POST["aantal[16]"];
$aantal[17] = $_POST["aantal[17]"];
$aantal[18] = $_POST["aantal[18]"];
$aantal[19] = $_POST["aantal[19]"];
$aantal[20] = $_POST["aantal[20]"];
$aantal[21] = $_POST["aantal[21]"];
$aantal[22] = $_POST["aantal[22]"];
$aantal[23] = $_POST["aantal[23]"];
$aantal[24] = $_POST["aantal[24]"];
$aantal[25] = $_POST["aantal[25]"];
$aantal[26] = $_POST["aantal[26]"];
$aantal[27] = $_POST["aantal[27]"];
$aantal[28] = $_POST["aantal[28]"];
$aantal[29] = $_POST["aantal[29]"];
$prijs[0] = $_POST["prijs[0]"];
$prijs[1] = $_POST["prijs[1]"];
$prijs[2] = $_POST["prijs[2]"];
$prijs[3] = $_POST["prijs[3]"];
$prijs[4] = $_POST["prijs[4]"];
$prijs[5] = $_POST["prijs[5]"];
$prijs[6] = $_POST["prijs[6]"];
$prijs[7] = $_POST["prijs[7]"];
$prijs[8] = $_POST["prijs[8]"];
$prijs[9] = $_POST["prijs[9]"];
$prijs[10] = $_POST["prijs[10]"];
$prijs[11] = $_POST["prijs[11]"];
$prijs[12] = $_POST["prijs[12]"];
$prijs[13] = $_POST["prijs[13]"];
$prijs[14] = $_POST["prijs[14]"];
$prijs[15] = $_POST["prijs[15]"];
$prijs[16] = $_POST["prijs[16]"];
$prijs[17] = $_POST["prijs[17]"];
$prijs[18] = $_POST["prijs[18]"];
$prijs[19] = $_POST["prijs[19]"];
$prijs[20] = $_POST["prijs[20]"];
$prijs[21] = $_POST["prijs[21]"];
$prijs[22] = $_POST["prijs[22]"];
$prijs[23] = $_POST["prijs[23]"];
$prijs[24] = $_POST["prijs[24]"];
$prijs[25] = $_POST["prijs[25]"];
$prijs[26] = $_POST["prijs[26]"];
$prijs[27] = $_POST["prijs[27]"];
$prijs[28] = $_POST["prijs[28]"];
$prijs[29] = $_POST["prijs[29]"];
class MYPDF extends TCPDF {
public function Header() {
$this->setJPEGQuality(90);
$this->Image('logo.png', 120, 10, 75, 0, 'PNG', 'adres van het logo(logo verplicht) zonder www&http');
}
public function Footer() {
$this->SetY(-15);
$this->SetFont(PDF_FONT_NAME_MAIN, 'I', 8);
$this->Cell(0, 10, '©2010 innovative-webdesign.nl', 0, false, 'C');
}
public function CreateTextBox($textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L') {
$this->SetXY($x+20, $y); // 20 = margin left
$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
$this->Cell($width, $height, $textval, 0, false, $align);
}
}
// create a PDF object
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document (meta) information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Henk Schepers');
$pdf->SetTitle('Facturatiescript v0.22 pre pre-beta');
// add a page
$pdf->AddPage();
// create address box
$pdf->CreateTextBox($bedrijfsnaam, 0, 55, 80, 10, 10, 'B');
$pdf->CreateTextBox($naam, 0, 60, 80, 10, 10);
$pdf->CreateTextBox($adres, 0, 65, 80, 10, 10);
$pdf->CreateTextBox($postcode . $plaats, 0, 70, 80, 10, 10);
// invoice title / number
$pdf->CreateTextBox('Factuur #1337', 0, 90, 120, 20, 16);
// date, order ref
$pdf->CreateTextBox('Datum: '.date('Y-m-d'), 0, 100, 0, 10, 10, '', 'R');
$pdf->CreateTextBox('ref.: #2674', 0, 105, 0, 10, 10, '', 'R');
// list headers
$pdf->CreateTextBox('Aantal', 0, 120, 20, 10, 10, 'B', 'C');
$pdf->CreateTextBox('Product(en) en\of Services', 20, 120, 90, 10, 10, 'B');
$pdf->CreateTextBox('Prijs', 110, 120, 30, 10, 10, 'B', 'R');
$pdf->CreateTextBox('Bedrag', 140, 120, 30, 10, 10, 'B', 'R');
$pdf->Line(20, 129, 195, 129);
// some example data
$orders[] = array('quant' => 1, 'descr' => 'Facturatiesysteem', 'price' => 75);
$orders[] = array('quant' => 40, 'descr' => 'Uurloon', 'price' => 10.45);
$orders[] = array('quant' => 3, 'descr' => 'Garantie eventuele fouten\bugs 1jaar', 'price' => 9.95);
$currY = 128;
$total = 0;
foreach ($orders as $row) {
$pdf->CreateTextBox($row['quant'], 0, $currY, 20, 10, 10, '', 'C');
$pdf->CreateTextBox($row['descr'], 20, $currY, 90, 10, 10, '');
$pdf->CreateTextBox('€'.$row['price'], 110, $currY, 30, 10, 10, '', 'R');
$amount = $row['quant']*$row['price'];
$pdf->CreateTextBox('€'.$amount, 140, $currY, 30, 10, 10, '', 'R');
$currY = $currY+5;
$total = $total+$amount;
}
$pdf->Line(20, $currY+4, 195, $currY+4);
// output the total row
$pdf->CreateTextBox('Totaal', 20, $currY+5, 135, 10, 10, 'B', 'R');
$pdf->CreateTextBox('€'.number_format($total, 2, '.', ''), 140, $currY+5, 30, 10, 10, 'B', 'R');
// some payment instructions or information
$pdf->setXY(20, $currY+30);
$pdf->SetFont(PDF_FONT_NAME_MAIN, '', 10);
$pdf->MultiCell(175, 10, '<em>Gelieve het bovengenoemde totaalbedrag binnen 10 dagen over te maken op rekening nummer 0000000
(Rabobank) t.n.v. innovative-webdesign, onder vermelding van het factuurnummer</em>. <br />', 0, 'L', 0, 1, '', '', true, null, true);
//Close and output PDF document
$pdf->Output('wip.pdf', 'F');
[/code]
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// variables
$bedrijfsnaam = $_POST["bedrijfsnaam"];
$naam = $_POST["naam"];
$adres = $_POST["adres"];
$postcode = $_POST["postcode"];
$plaats = $_POST["plaats"];
$refNummer = $_POST["refNummer"];
//arrays
$product[0] = $_POST["product[0]"];
$product[1] = $_POST["product[1]"];
$product[2] = $_POST["product[2]"];
$product[3] = $_POST["product[3]"];
$product[4] = $_POST["product[4]"];
$product[5] = $_POST["product[5]"];
$product[6] = $_POST["product[6]"];
$product[7] = $_POST["product[7]"];
$product[8] = $_POST["product[8]"];
$product[9] = $_POST["product[9]"];
$product[10] = $_POST["product[10]"];
$product[11] = $_POST["product[11]"];
$product[12] = $_POST["product[12]"];
$product[13] = $_POST["product[13]"];
$product[14] = $_POST["product[14]"];
$product[15] = $_POST["product[15]"];
$product[16] = $_POST["product[16]"];
$product[17] = $_POST["product[17]"];
$product[18] = $_POST["product[18]"];
$product[19] = $_POST["product[19]"];
$product[20] = $_POST["product[20]"];
$product[21] = $_POST["product[21]"];
$product[22] = $_POST["product[22]"];
$product[23] = $_POST["product[23]"];
$product[24] = $_POST["product[24]"];
$product[25] = $_POST["product[25]"];
$product[26] = $_POST["product[26]"];
$product[27] = $_POST["product[27]"];
$product[28] = $_POST["product[28]"];
$product[29] = $_POST["product[29]"];
$aantal[0] = $_POST["aantal[0]"];
$aantal[1] = $_POST["aantal[1]"];
$aantal[2] = $_POST["aantal[2]"];
$aantal[3] = $_POST["aantal[3]"];
$aantal[4] = $_POST["aantal[4]"];
$aantal[5] = $_POST["aantal[5]"];
$aantal[6] = $_POST["aantal[6]"];
$aantal[7] = $_POST["aantal[7]"];
$aantal[8] = $_POST["aantal[8]"];
$aantal[9] = $_POST["aantal[9]"];
$aantal[10] = $_POST["aantal[10]"];
$aantal[11] = $_POST["aantal[11]"];
$aantal[12] = $_POST["aantal[12]"];
$aantal[13] = $_POST["aantal[13]"];
$aantal[14] = $_POST["aantal[14]"];
$aantal[15] = $_POST["aantal[15]"];
$aantal[16] = $_POST["aantal[16]"];
$aantal[17] = $_POST["aantal[17]"];
$aantal[18] = $_POST["aantal[18]"];
$aantal[19] = $_POST["aantal[19]"];
$aantal[20] = $_POST["aantal[20]"];
$aantal[21] = $_POST["aantal[21]"];
$aantal[22] = $_POST["aantal[22]"];
$aantal[23] = $_POST["aantal[23]"];
$aantal[24] = $_POST["aantal[24]"];
$aantal[25] = $_POST["aantal[25]"];
$aantal[26] = $_POST["aantal[26]"];
$aantal[27] = $_POST["aantal[27]"];
$aantal[28] = $_POST["aantal[28]"];
$aantal[29] = $_POST["aantal[29]"];
$prijs[0] = $_POST["prijs[0]"];
$prijs[1] = $_POST["prijs[1]"];
$prijs[2] = $_POST["prijs[2]"];
$prijs[3] = $_POST["prijs[3]"];
$prijs[4] = $_POST["prijs[4]"];
$prijs[5] = $_POST["prijs[5]"];
$prijs[6] = $_POST["prijs[6]"];
$prijs[7] = $_POST["prijs[7]"];
$prijs[8] = $_POST["prijs[8]"];
$prijs[9] = $_POST["prijs[9]"];
$prijs[10] = $_POST["prijs[10]"];
$prijs[11] = $_POST["prijs[11]"];
$prijs[12] = $_POST["prijs[12]"];
$prijs[13] = $_POST["prijs[13]"];
$prijs[14] = $_POST["prijs[14]"];
$prijs[15] = $_POST["prijs[15]"];
$prijs[16] = $_POST["prijs[16]"];
$prijs[17] = $_POST["prijs[17]"];
$prijs[18] = $_POST["prijs[18]"];
$prijs[19] = $_POST["prijs[19]"];
$prijs[20] = $_POST["prijs[20]"];
$prijs[21] = $_POST["prijs[21]"];
$prijs[22] = $_POST["prijs[22]"];
$prijs[23] = $_POST["prijs[23]"];
$prijs[24] = $_POST["prijs[24]"];
$prijs[25] = $_POST["prijs[25]"];
$prijs[26] = $_POST["prijs[26]"];
$prijs[27] = $_POST["prijs[27]"];
$prijs[28] = $_POST["prijs[28]"];
$prijs[29] = $_POST["prijs[29]"];
class MYPDF extends TCPDF {
public function Header() {
$this->setJPEGQuality(90);
$this->Image('logo.png', 120, 10, 75, 0, 'PNG', 'adres van het logo(logo verplicht) zonder www&http');
}
public function Footer() {
$this->SetY(-15);
$this->SetFont(PDF_FONT_NAME_MAIN, 'I', 8);
$this->Cell(0, 10, '©2010 innovative-webdesign.nl', 0, false, 'C');
}
public function CreateTextBox($textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L') {
$this->SetXY($x+20, $y); // 20 = margin left
$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
$this->Cell($width, $height, $textval, 0, false, $align);
}
}
// create a PDF object
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document (meta) information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Henk Schepers');
$pdf->SetTitle('Facturatiescript v0.22 pre pre-beta');
// add a page
$pdf->AddPage();
// create address box
$pdf->CreateTextBox($bedrijfsnaam, 0, 55, 80, 10, 10, 'B');
$pdf->CreateTextBox($naam, 0, 60, 80, 10, 10);
$pdf->CreateTextBox($adres, 0, 65, 80, 10, 10);
$pdf->CreateTextBox($postcode . $plaats, 0, 70, 80, 10, 10);
// invoice title / number
$pdf->CreateTextBox('Factuur #1337', 0, 90, 120, 20, 16);
// date, order ref
$pdf->CreateTextBox('Datum: '.date('Y-m-d'), 0, 100, 0, 10, 10, '', 'R');
$pdf->CreateTextBox('ref.: #2674', 0, 105, 0, 10, 10, '', 'R');
// list headers
$pdf->CreateTextBox('Aantal', 0, 120, 20, 10, 10, 'B', 'C');
$pdf->CreateTextBox('Product(en) en\of Services', 20, 120, 90, 10, 10, 'B');
$pdf->CreateTextBox('Prijs', 110, 120, 30, 10, 10, 'B', 'R');
$pdf->CreateTextBox('Bedrag', 140, 120, 30, 10, 10, 'B', 'R');
$pdf->Line(20, 129, 195, 129);
// some example data
$orders[] = array('quant' => 1, 'descr' => 'Facturatiesysteem', 'price' => 75);
$orders[] = array('quant' => 40, 'descr' => 'Uurloon', 'price' => 10.45);
$orders[] = array('quant' => 3, 'descr' => 'Garantie eventuele fouten\bugs 1jaar', 'price' => 9.95);
$currY = 128;
$total = 0;
foreach ($orders as $row) {
$pdf->CreateTextBox($row['quant'], 0, $currY, 20, 10, 10, '', 'C');
$pdf->CreateTextBox($row['descr'], 20, $currY, 90, 10, 10, '');
$pdf->CreateTextBox('€'.$row['price'], 110, $currY, 30, 10, 10, '', 'R');
$amount = $row['quant']*$row['price'];
$pdf->CreateTextBox('€'.$amount, 140, $currY, 30, 10, 10, '', 'R');
$currY = $currY+5;
$total = $total+$amount;
}
$pdf->Line(20, $currY+4, 195, $currY+4);
// output the total row
$pdf->CreateTextBox('Totaal', 20, $currY+5, 135, 10, 10, 'B', 'R');
$pdf->CreateTextBox('€'.number_format($total, 2, '.', ''), 140, $currY+5, 30, 10, 10, 'B', 'R');
// some payment instructions or information
$pdf->setXY(20, $currY+30);
$pdf->SetFont(PDF_FONT_NAME_MAIN, '', 10);
$pdf->MultiCell(175, 10, '<em>Gelieve het bovengenoemde totaalbedrag binnen 10 dagen over te maken op rekening nummer 0000000
(Rabobank) t.n.v. innovative-webdesign, onder vermelding van het factuurnummer</em>. <br />', 0, 'L', 0, 1, '', '', true, null, true);
//Close and output PDF document
$pdf->Output('wip.pdf', 'F');
[/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
<?PHP
$product[0] = $_POST["product[0]"];
...
$product[29] = $_POST["product[29]"];
$aantal[0] = $_POST["aantal[0]"];
...
$aantal[29] = $_POST["aantal[29]"];
$prijs[0] = $_POST["prijs[0]"];
...
$prijs[29] = $_POST["prijs[29]"];
?>
$product[0] = $_POST["product[0]"];
...
$product[29] = $_POST["product[29]"];
$aantal[0] = $_POST["aantal[0]"];
...
$aantal[29] = $_POST["aantal[29]"];
$prijs[0] = $_POST["prijs[0]"];
...
$prijs[29] = $_POST["prijs[29]"];
?>
Werkt dit niet gewoon?
Maar als ik hem dan $prijs en $product en $aantal dan door laat geven aan de pdf, geeft ie dat ook niet alle lege velden mee?
Doet hij nu ook hoor.
En als je van de names van de velden 'orders[n][price]', 'orders[n][quant]' en 'orders[n][descr]', met n als 'hoeveelste', heb je meteen de array in elkaar zoals je die later ook gebruikt.
Welk van de drie wil je op leegheid testen?
Stel dat dat aantal/quant is, gebruik dan het volgende:
Deze haalt alle items weg die geen getal zijn of 0
En als je van de names van de velden 'orders[n][price]', 'orders[n][quant]' en 'orders[n][descr]', met n als 'hoeveelste', heb je meteen de array in elkaar zoals je die later ook gebruikt.
Welk van de drie wil je op leegheid testen?
Stel dat dat aantal/quant is, gebruik dan het volgende:
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<?php
$orders = array_filter($orders,
function($item) {
return (ctype_digit($item['quant']) && $item['quant'] !== 0);
}
);
?>
$orders = array_filter($orders,
function($item) {
return (ctype_digit($item['quant']) && $item['quant'] !== 0);
}
);
?>
Deze haalt alle items weg die geen getal zijn of 0
Gewijzigd op 25/10/2010 22:02:49 door Pim -
Ik moet ze allemaal op leeg checken :)
ik snap die functie die je gemaakt hebt wel redelijk, alleen snap ik deze zin niet helemaal:
En als je van de names van de velden 'orders[n][price]', 'orders[n][quant]' en 'orders[n][descr]', met n als 'hoeveelste', heb je meteen de array in elkaar zoals je die later ook gebruikt.
sorry :p
ik snap die functie die je gemaakt hebt wel redelijk, alleen snap ik deze zin niet helemaal:
En als je van de names van de velden 'orders[n][price]', 'orders[n][quant]' en 'orders[n][descr]', met n als 'hoeveelste', heb je meteen de array in elkaar zoals je die later ook gebruikt.
sorry :p
Gewijzigd op 26/10/2010 17:40:01 door Thierry Vredeveld
Als je een formulier hebt kan je het volgende doen:
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
Je ziet bij de naam "product[]" staan. Hiermee geef je aan dat je na je request een array terug wilt krijgen met daarin, in dit geval, 6 keys (van 0 t/m 5) met elk hun eigen waarde.
Dus als je dan dit doet:
zal je zien dat je een array geprint krijgt met de ingevoerde waarden.
Omdat in dit geval $_POST['product'] een array is, kan je met een foreach door de array wandelen en controleren of de waarden zijn ingevuld, dus zo:
(wel even controleren of $_POST['product'] wel echt een array is en of die wel één of meerdere waarden bevatten (want een foreach gaat in de fout als ie niet kan loopen))
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="product[]" value="" />
Je ziet bij de naam "product[]" staan. Hiermee geef je aan dat je na je request een array terug wilt krijgen met daarin, in dit geval, 6 keys (van 0 t/m 5) met elk hun eigen waarde.
Dus als je dan dit doet:
Code (php)
zal je zien dat je een array geprint krijgt met de ingevoerde waarden.
Omdat in dit geval $_POST['product'] een array is, kan je met een foreach door de array wandelen en controleren of de waarden zijn ingevuld, dus zo:
Code (php)
(wel even controleren of $_POST['product'] wel echt een array is en of die wel één of meerdere waarden bevatten (want een foreach gaat in de fout als ie niet kan loopen))
Gewijzigd op 26/10/2010 17:50:33 door Arjan -
Dus als je van het eerste item van het prijs veld de naam 'orders[0][price]' maakt, bij de hoeveelheid 'orders[0][quant]' en bij de beschrijving 'orders[0][descr]'. Voor het tweede item vervang je de 0 voor een 1, voor het derde item de 0 voor een 2 enz.
Als ze alle niet leeg mogen zijn, wordt de filter zo:
Array filter heelt dus de items weg, waarbij de functie true geeft. Die functie (een Closure in dit geval) controleert dus of de hoeveelheid een nummer is en niet nul en of de prijs en beschrijving velden niet leeg zijn.
Als ze alle niet leeg mogen zijn, wordt de filter zo:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$orders = array_filter($_POST['orders'],
function($item) {
return (
ctype_digit($item['quant']) &&
$item['quant'] !== '0' &&
$item['price'] !== '' &&
$item['descr'] !== ''
);
}
);
?>
$orders = array_filter($_POST['orders'],
function($item) {
return (
ctype_digit($item['quant']) &&
$item['quant'] !== '0' &&
$item['price'] !== '' &&
$item['descr'] !== ''
);
}
);
?>
Array filter heelt dus de items weg, waarbij de functie true geeft. Die functie (een Closure in dit geval) controleert dus of de hoeveelheid een nummer is en niet nul en of de prijs en beschrijving velden niet leeg zijn.
Ja zo word het al stuk duidelijker :D
Kan het nu niet toepassen, maar ga het zometeen sowieso proberen, bedankt alvast!
Kan het nu niet toepassen, maar ga het zometeen sowieso proberen, bedankt alvast!
En toch is dit erg omslachtig. Je wilt gewoon rechtstreeks een array terug krijgen uit je request lijkt mij. Je moet nu alle getallen handmatig invoeren.
Je kan toch veel beter zoiets doen:
<input type="text" name="product[]" value="" />
<input type="text" name="price[]" value="" />
<input type="text" name="descr[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="price[]" value="" />
<input type="text" name="descr[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="price[]" value="" />
<input type="text" name="descr[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="price[]" value="" />
<input type="text" name="descr[]" value="" />
Zo krijg je dus bij elkaar horende keys in zowel product, price en descr en zit je niet te kloten met alle getallen handmatig invoeren.
Edit: Eigenlijk dus hetgeen wat SilverWolf NL al aan het begin aangaf. Het gaat er om dat je die onhandige $product[0] = $_POST['product[0]']; kwijt bent, want dat is zeer omslachtig. Daarna kan je heel simpel alles doen met de arrays wat je wilt.
Je kan toch veel beter zoiets doen:
<input type="text" name="product[]" value="" />
<input type="text" name="price[]" value="" />
<input type="text" name="descr[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="price[]" value="" />
<input type="text" name="descr[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="price[]" value="" />
<input type="text" name="descr[]" value="" />
<input type="text" name="product[]" value="" />
<input type="text" name="price[]" value="" />
<input type="text" name="descr[]" value="" />
Zo krijg je dus bij elkaar horende keys in zowel product, price en descr en zit je niet te kloten met alle getallen handmatig invoeren.
Edit: Eigenlijk dus hetgeen wat SilverWolf NL al aan het begin aangaf. Het gaat er om dat je die onhandige $product[0] = $_POST['product[0]']; kwijt bent, want dat is zeer omslachtig. Daarna kan je heel simpel alles doen met de arrays wat je wilt.
Gewijzigd op 26/10/2010 17:58:59 door Arjan -
Ja dit is idd wel wat makkelijker, hoewel de velden waarschijnlijk toch dynamisch gemaakt worden.
Dan wordt de code:
Fouten worden zo genegeerd. Als ze ze bij wil houden, moet je de if splitsen en een array met de fouten vullen.
Dan wordt de code:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$count = (isset($_POST['descr']) && is_array($_POST['descr'])) ? count($_POST['descr']) : 0;
$orders = array();
for($i = 0; $i < $count; ++$i) {
if (ctype_digit($_POST['quant'][$i]) &&
$_POST['quant'][$i] !== '0' &&
$_POST['price'][$i] !== '' &&
$_POST['descr'][$i] !== ''
)
$orders[] = array(
'quant' => $_POST['quant'][$i],
'price' => $_POST['price'][$i],
'descr' => $_POST['descr'][$i],
);
}
print_r($orders);
?>
$count = (isset($_POST['descr']) && is_array($_POST['descr'])) ? count($_POST['descr']) : 0;
$orders = array();
for($i = 0; $i < $count; ++$i) {
if (ctype_digit($_POST['quant'][$i]) &&
$_POST['quant'][$i] !== '0' &&
$_POST['price'][$i] !== '' &&
$_POST['descr'][$i] !== ''
)
$orders[] = array(
'quant' => $_POST['quant'][$i],
'price' => $_POST['price'][$i],
'descr' => $_POST['descr'][$i],
);
}
print_r($orders);
?>
Fouten worden zo genegeerd. Als ze ze bij wil houden, moet je de if splitsen en een array met de fouten vullen.
Gewijzigd op 26/10/2010 18:23:44 door Pim -
Ok dit is nu mijn 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// variables
$bedrijfsnaam = $_POST["bedrijfsnaam"];
$naam = $_POST["naam"];
$adres = $_POST["adres"];
$postcode = $_POST["postcode"];
$plaats = $_POST["plaats"];
$refNummer = $_POST["refNummer"];
$descr = $_POST["descr"];
$quant = $_POST["quant"];
$price = $_POST["price"];
class MYPDF extends TCPDF {
public function Header() {
$this->setJPEGQuality(90);
$this->Image('logo.png', 120, 10, 75, 0, 'PNG', 'adres van het logo(logo verplicht) zonder www&http');
}
public function Footer() {
$this->SetY(-15);
$this->SetFont(PDF_FONT_NAME_MAIN, 'I', 8);
$this->Cell(0, 10, '©2010 innovative-webdesign.nl', 0, false, 'C');
}
public function CreateTextBox($textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L') {
$this->SetXY($x+20, $y); // 20 = margin left
$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
$this->Cell($width, $height, $textval, 0, false, $align);
}
}
// create a PDF object
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document (meta) information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Henk Schepers');
$pdf->SetTitle('Facturatiescript v0.22 pre pre-beta');
// add a page
$pdf->AddPage();
// create address box
$pdf->CreateTextBox($bedrijfsnaam, 0, 55, 80, 10, 10, 'B');
$pdf->CreateTextBox($naam, 0, 60, 80, 10, 10);
$pdf->CreateTextBox($adres, 0, 65, 80, 10, 10);
$pdf->CreateTextBox($postcode . $plaats, 0, 70, 80, 10, 10);
// invoice title / number
$pdf->CreateTextBox('Factuur #1337', 0, 90, 120, 20, 16);
// date, order ref
$pdf->CreateTextBox('Datum: '.date('Y-m-d'), 0, 100, 0, 10, 10, '', 'R');
$pdf->CreateTextBox('ref.: #2674', 0, 105, 0, 10, 10, '', 'R');
// list headers
$pdf->CreateTextBox('Aantal', 0, 120, 20, 10, 10, 'B', 'C');
$pdf->CreateTextBox('Product(en) en\of Services', 20, 120, 90, 10, 10, 'B');
$pdf->CreateTextBox('Prijs', 110, 120, 30, 10, 10, 'B', 'R');
$pdf->CreateTextBox('Bedrag', 140, 120, 30, 10, 10, 'B', 'R');
$pdf->Line(20, 129, 195, 129);
// some example data
$count = (isset($_POST['descr']) && is_array($_POST['descr'])) ? count($_POST['descr']) : 0;
$orders = array();
for($i = 0; $i < $count; ++$i)
{
if (ctype_digit($_POST['quant'][$i]) &&
$_POST['quant'][$i] !== '0' &&
$_POST['price'][$i] !== '' &&
$_POST['descr'][$i] !== ''
)
$orders[] = array(
'quant' => $_POST['quant'][$i],
'price' => $_POST['price'][$i],
'descr' => $_POST['descr'][$i],
);
}
print_r($orders);
$currY = 128;
$total = 0;
foreach ($orders as $row) {
$pdf->CreateTextBox($row['quant'], 0, $currY, 20, 10, 10, '', 'C');
$pdf->CreateTextBox($row['descr'], 20, $currY, 90, 10, 10, '');
$pdf->CreateTextBox('€'.$row['price'], 110, $currY, 30, 10, 10, '', 'R');
$amount = $row['quant']*$row['price'];
$pdf->CreateTextBox('€'.$amount, 140, $currY, 30, 10, 10, '', 'R');
$currY = $currY+5;
$total = $total+$amount;
}
$pdf->Line(20, $currY+4, 195, $currY+4);
// output the total row
$pdf->CreateTextBox('Totaal', 20, $currY+5, 135, 10, 10, 'B', 'R');
$pdf->CreateTextBox('€'.number_format($total, 2, '.', ''), 140, $currY+5, 30, 10, 10, 'B', 'R');
// some payment instructions or information
$pdf->setXY(20, $currY+30);
$pdf->SetFont(PDF_FONT_NAME_MAIN, '', 10);
$pdf->MultiCell(175, 10, '<em>Gelieve het bovengenoemde totaalbedrag binnen 10 dagen over te maken op rekening nummer 0000000
(Rabobank) t.n.v. innovative-webdesign, onder vermelding van het factuurnummer</em>. <br />', 0, 'L', 0, 1, '', '', true, null, true);
//Close and output PDF document
$pdf->Output('wip.pdf', 'F');
[/code]
Maar ik krijg niks te zien in mijn pdf, heb ik iets fout?
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// variables
$bedrijfsnaam = $_POST["bedrijfsnaam"];
$naam = $_POST["naam"];
$adres = $_POST["adres"];
$postcode = $_POST["postcode"];
$plaats = $_POST["plaats"];
$refNummer = $_POST["refNummer"];
$descr = $_POST["descr"];
$quant = $_POST["quant"];
$price = $_POST["price"];
class MYPDF extends TCPDF {
public function Header() {
$this->setJPEGQuality(90);
$this->Image('logo.png', 120, 10, 75, 0, 'PNG', 'adres van het logo(logo verplicht) zonder www&http');
}
public function Footer() {
$this->SetY(-15);
$this->SetFont(PDF_FONT_NAME_MAIN, 'I', 8);
$this->Cell(0, 10, '©2010 innovative-webdesign.nl', 0, false, 'C');
}
public function CreateTextBox($textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L') {
$this->SetXY($x+20, $y); // 20 = margin left
$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
$this->Cell($width, $height, $textval, 0, false, $align);
}
}
// create a PDF object
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document (meta) information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Henk Schepers');
$pdf->SetTitle('Facturatiescript v0.22 pre pre-beta');
// add a page
$pdf->AddPage();
// create address box
$pdf->CreateTextBox($bedrijfsnaam, 0, 55, 80, 10, 10, 'B');
$pdf->CreateTextBox($naam, 0, 60, 80, 10, 10);
$pdf->CreateTextBox($adres, 0, 65, 80, 10, 10);
$pdf->CreateTextBox($postcode . $plaats, 0, 70, 80, 10, 10);
// invoice title / number
$pdf->CreateTextBox('Factuur #1337', 0, 90, 120, 20, 16);
// date, order ref
$pdf->CreateTextBox('Datum: '.date('Y-m-d'), 0, 100, 0, 10, 10, '', 'R');
$pdf->CreateTextBox('ref.: #2674', 0, 105, 0, 10, 10, '', 'R');
// list headers
$pdf->CreateTextBox('Aantal', 0, 120, 20, 10, 10, 'B', 'C');
$pdf->CreateTextBox('Product(en) en\of Services', 20, 120, 90, 10, 10, 'B');
$pdf->CreateTextBox('Prijs', 110, 120, 30, 10, 10, 'B', 'R');
$pdf->CreateTextBox('Bedrag', 140, 120, 30, 10, 10, 'B', 'R');
$pdf->Line(20, 129, 195, 129);
// some example data
$count = (isset($_POST['descr']) && is_array($_POST['descr'])) ? count($_POST['descr']) : 0;
$orders = array();
for($i = 0; $i < $count; ++$i)
{
if (ctype_digit($_POST['quant'][$i]) &&
$_POST['quant'][$i] !== '0' &&
$_POST['price'][$i] !== '' &&
$_POST['descr'][$i] !== ''
)
$orders[] = array(
'quant' => $_POST['quant'][$i],
'price' => $_POST['price'][$i],
'descr' => $_POST['descr'][$i],
);
}
print_r($orders);
$currY = 128;
$total = 0;
foreach ($orders as $row) {
$pdf->CreateTextBox($row['quant'], 0, $currY, 20, 10, 10, '', 'C');
$pdf->CreateTextBox($row['descr'], 20, $currY, 90, 10, 10, '');
$pdf->CreateTextBox('€'.$row['price'], 110, $currY, 30, 10, 10, '', 'R');
$amount = $row['quant']*$row['price'];
$pdf->CreateTextBox('€'.$amount, 140, $currY, 30, 10, 10, '', 'R');
$currY = $currY+5;
$total = $total+$amount;
}
$pdf->Line(20, $currY+4, 195, $currY+4);
// output the total row
$pdf->CreateTextBox('Totaal', 20, $currY+5, 135, 10, 10, 'B', 'R');
$pdf->CreateTextBox('€'.number_format($total, 2, '.', ''), 140, $currY+5, 30, 10, 10, 'B', 'R');
// some payment instructions or information
$pdf->setXY(20, $currY+30);
$pdf->SetFont(PDF_FONT_NAME_MAIN, '', 10);
$pdf->MultiCell(175, 10, '<em>Gelieve het bovengenoemde totaalbedrag binnen 10 dagen over te maken op rekening nummer 0000000
(Rabobank) t.n.v. innovative-webdesign, onder vermelding van het factuurnummer</em>. <br />', 0, 'L', 0, 1, '', '', true, null, true);
//Close and output PDF document
$pdf->Output('wip.pdf', 'F');
[/code]
Maar ik krijg niks te zien in mijn pdf, heb ik iets fout?
Gewijzigd op 26/10/2010 23:39:12 door Thierry Vredeveld
Heeft iemand hier nog een toevoeging aan? Ik ben samen met Thierry aan dit script bezig, alleen het posten van die producten werkt nog niet..




