Function KlantAdres
Ik ben zelf niet zo goed in functies maar ik probeer toch het een en ander uit.
maar het lukt niet.
Ik zal het waarschijnlijk verkeerd aanpakken, maar ik weet niet hoe ik dit kan doen.
Mijn bedoeling is dat ik kan oproepen wat het adres van de klant is, dat is Adres, Postcode, Plaats, Land.
Hoe moet ik dit doen, of hoe kan ik dit het beste doen?
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
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
<?php
function KlantAdres($klantid)
{
$sql = "SELECT id,achternaam FROM users WHERE id='".mysql_real_escape_string(htmlentities($klantid))."'";
$query = mysql_query($sql) or die (mysql_error ());
$row = mysql_fetch_assoc($query);
if(mysql_num_rows($query) == 0){
$naam = '<font color="#FF0000">Adres <u>niet</u> gevonden.</font>';
}else{
$countryArray = array("AF"=>"Afghanistan",
"BE"=>"Belgium",
"GT"=>"Guatemala",
"GK"=>"Guernsey",
"GV"=>"Guinea",
"PU"=>"Guinea-Bissau",
"GY"=>"Guyana ",
"HA"=>"Haiti",
"HM"=>"Heard Island and McDonald Islands",
"VT"=>"Holy See (Vatican City)",
"HO"=>"Honduras",
"HK"=>"Hong Kong",
"HQ"=>"Howland Island",
"HU"=>"Hungary ",
"IC"=>"Iceland",
"IN"=>"India",
"XO"=>"Indian Ocean",
"ID"=>"Indonesia",
"IR"=>"Iran",
"IZ"=>"Iraq",
"EI"=>"Ireland",
"IM"=>"Isle of Man",
"IS"=>"Israel",
"IT"=>"Italy ",
"JM"=>"Jamaica",
"JN"=>"Jan Mayen",
"JA"=>"Japan",
"DQ"=>"Jarvis Island",
"JE"=>"Jersey",
"JQ"=>"Johnston Atoll",
"JO"=>"Jordan",
"JU"=>"Juan de Nova Island ",
"KZ"=>"Kazakhstan",
"KE"=>"Kenya",
"KQ"=>"Kingman Reef",
"KR"=>"Kiribati",
"KN"=>"Korea, North",
"KS"=>"Korea, South",
"KU"=>"Kuwait",
"KH"=>"Kyrgyzstan ",
"LA"=>"Laos",
"LG"=>"Latvia",
"LE"=>"Lebanon",
"LT"=>"Lesotho",
"LI"=>"Liberia",
"LY"=>"Libya",
"LS"=>"Liechtenstein",
"LH"=>"Lithuania",
"LU"=>"Luxembourg ",
"WA"=>"Namibia",
"MR"=>"Nauru",
"BQ"=>"Navassa Island",
"NP"=>"Nepal",
"NL"=>"Netherlands",
"NT"=>"Netherlands Antilles",
"NC"=>"New Caledonia",
"NZ"=>"New Zealand",
"NU"=>"Nicaragua",
"NG"=>"Niger",
"NI"=>"Nigeria",
"NE"=>"Niue",
"NF"=>"Norfolk Island",
"CQ"=>"Northern Mariana Islands",
"NO"=>"Norway",
"MU"=>"Oman ",
"ZN"=>"Pacific Ocean",
"PK"=>"Pakistan",
"PS"=>"Palau",
"LQ"=>"Palmyra Atoll",
"PM"=>"Panama",
"PP"=>"Papua New Guinea",
"PF"=>"Paracel Islands",
"PA"=>"Paraguay",
"PE"=>"Peru",
"RP"=>"Philippines",
"PC"=>"Pitcairn Islands",
"PL"=>"Poland",
"PO"=>"Portugal",
"PQ"=>"Puerto Rico ",
"QA"=>"Qatar ",
"RE"=>"Reunion",
"RO"=>"Romania",
"RS"=>"Russia",
"TP"=>"Sao Tome and Principe",
"SA"=>"Saudi Arabia",
"SG"=>"Senegal",
"RB"=>"Serbia",
"SE"=>"Seychelles",
"SL"=>"Sierra Leone",
"SN"=>"Singapore",
"LO"=>"Slovakia",
"SI"=>"Slovenia",
"SO"=>"Somalia",
"SF"=>"South Africa",
"OO"=>"Southern Ocean",
"SP"=>"Spain",
"PG"=>"Spratly Islands",
"CE"=>"Sri Lanka",
"SU"=>"Sudan",
"NS"=>"Suriname",
"SV"=>"Svalbard",
"WZ"=>"Swaziland",
"SW"=>"Sweden",
"SZ"=>"Switzerland",
"TS"=>"Tunisia",
"TU"=>"Turkey",
"AE"=>"United Arab Emirates",
"UK"=>"United Kingdom",
"US"=>"United States",
"UM"=>"United States Pacific Island Wildlife Refuges",
);
$adres -> plaats = $row['woonplaats'];
$adres -> postcode = $row['postcode'];
$adres -> adres = $row['adres'];
$adres -> land = $countryArray[$row['land']];
}
return $adres;
}
?>
function KlantAdres($klantid)
{
$sql = "SELECT id,achternaam FROM users WHERE id='".mysql_real_escape_string(htmlentities($klantid))."'";
$query = mysql_query($sql) or die (mysql_error ());
$row = mysql_fetch_assoc($query);
if(mysql_num_rows($query) == 0){
$naam = '<font color="#FF0000">Adres <u>niet</u> gevonden.</font>';
}else{
$countryArray = array("AF"=>"Afghanistan",
"BE"=>"Belgium",
"GT"=>"Guatemala",
"GK"=>"Guernsey",
"GV"=>"Guinea",
"PU"=>"Guinea-Bissau",
"GY"=>"Guyana ",
"HA"=>"Haiti",
"HM"=>"Heard Island and McDonald Islands",
"VT"=>"Holy See (Vatican City)",
"HO"=>"Honduras",
"HK"=>"Hong Kong",
"HQ"=>"Howland Island",
"HU"=>"Hungary ",
"IC"=>"Iceland",
"IN"=>"India",
"XO"=>"Indian Ocean",
"ID"=>"Indonesia",
"IR"=>"Iran",
"IZ"=>"Iraq",
"EI"=>"Ireland",
"IM"=>"Isle of Man",
"IS"=>"Israel",
"IT"=>"Italy ",
"JM"=>"Jamaica",
"JN"=>"Jan Mayen",
"JA"=>"Japan",
"DQ"=>"Jarvis Island",
"JE"=>"Jersey",
"JQ"=>"Johnston Atoll",
"JO"=>"Jordan",
"JU"=>"Juan de Nova Island ",
"KZ"=>"Kazakhstan",
"KE"=>"Kenya",
"KQ"=>"Kingman Reef",
"KR"=>"Kiribati",
"KN"=>"Korea, North",
"KS"=>"Korea, South",
"KU"=>"Kuwait",
"KH"=>"Kyrgyzstan ",
"LA"=>"Laos",
"LG"=>"Latvia",
"LE"=>"Lebanon",
"LT"=>"Lesotho",
"LI"=>"Liberia",
"LY"=>"Libya",
"LS"=>"Liechtenstein",
"LH"=>"Lithuania",
"LU"=>"Luxembourg ",
"WA"=>"Namibia",
"MR"=>"Nauru",
"BQ"=>"Navassa Island",
"NP"=>"Nepal",
"NL"=>"Netherlands",
"NT"=>"Netherlands Antilles",
"NC"=>"New Caledonia",
"NZ"=>"New Zealand",
"NU"=>"Nicaragua",
"NG"=>"Niger",
"NI"=>"Nigeria",
"NE"=>"Niue",
"NF"=>"Norfolk Island",
"CQ"=>"Northern Mariana Islands",
"NO"=>"Norway",
"MU"=>"Oman ",
"ZN"=>"Pacific Ocean",
"PK"=>"Pakistan",
"PS"=>"Palau",
"LQ"=>"Palmyra Atoll",
"PM"=>"Panama",
"PP"=>"Papua New Guinea",
"PF"=>"Paracel Islands",
"PA"=>"Paraguay",
"PE"=>"Peru",
"RP"=>"Philippines",
"PC"=>"Pitcairn Islands",
"PL"=>"Poland",
"PO"=>"Portugal",
"PQ"=>"Puerto Rico ",
"QA"=>"Qatar ",
"RE"=>"Reunion",
"RO"=>"Romania",
"RS"=>"Russia",
"TP"=>"Sao Tome and Principe",
"SA"=>"Saudi Arabia",
"SG"=>"Senegal",
"RB"=>"Serbia",
"SE"=>"Seychelles",
"SL"=>"Sierra Leone",
"SN"=>"Singapore",
"LO"=>"Slovakia",
"SI"=>"Slovenia",
"SO"=>"Somalia",
"SF"=>"South Africa",
"OO"=>"Southern Ocean",
"SP"=>"Spain",
"PG"=>"Spratly Islands",
"CE"=>"Sri Lanka",
"SU"=>"Sudan",
"NS"=>"Suriname",
"SV"=>"Svalbard",
"WZ"=>"Swaziland",
"SW"=>"Sweden",
"SZ"=>"Switzerland",
"TS"=>"Tunisia",
"TU"=>"Turkey",
"AE"=>"United Arab Emirates",
"UK"=>"United Kingdom",
"US"=>"United States",
"UM"=>"United States Pacific Island Wildlife Refuges",
);
$adres -> plaats = $row['woonplaats'];
$adres -> postcode = $row['postcode'];
$adres -> adres = $row['adres'];
$adres -> land = $countryArray[$row['land']];
}
return $adres;
}
?>
Ten tweede, je moet nog even aan de basis werken.
Je wilt dus een functie die een adres opzoekt, dat gaat vrij goed, en een melding geeft als het niet gevonden is, dat gaat minder.
Om het eerste te laten werken moet je gewoon $adres als array gebruiken, dus $adres['plaats'] ipv $adres->plaats.
Het tweede is lastiger, omdat je daarvoor ook de code buiten de functie aan moet passen. In de functie zelf zou ik doen return false; en dan kan je buiten de functie een foutmelding geven.
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
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
<?php
function KlantAdres($klantid)
{
$sql = "SELECT id,achternaam FROM users WHERE id='".mysql_real_escape_string(htmlentities($klantid))."'";
$query = mysql_query($sql) or die (mysql_error ());
$row = mysql_fetch_assoc($query);
if(mysql_num_rows($query) == 0){
return false;
}
$countryArray = array("AF"=>"Afghanistan",
"UM"=>"United States Pacific Island Wildlife Refuges",
);
$adres['plaats'] = $row['woonplaats'];
$adres['postcode'] = $row['postcode'];
$adres['adres'] = $row['adres'];
$adres['land'] = $countryArray[$row['land']];
return $adres;
}
$adres = KlantAdres(112);
if(!$adres) {
echo '<font color="#FF0000">Adres <u>niet</u> gevonden.</font>';
} else {
echo $adres['plaats'];
}
?>
function KlantAdres($klantid)
{
$sql = "SELECT id,achternaam FROM users WHERE id='".mysql_real_escape_string(htmlentities($klantid))."'";
$query = mysql_query($sql) or die (mysql_error ());
$row = mysql_fetch_assoc($query);
if(mysql_num_rows($query) == 0){
return false;
}
$countryArray = array("AF"=>"Afghanistan",
"UM"=>"United States Pacific Island Wildlife Refuges",
);
$adres['plaats'] = $row['woonplaats'];
$adres['postcode'] = $row['postcode'];
$adres['adres'] = $row['adres'];
$adres['land'] = $countryArray[$row['land']];
return $adres;
}
$adres = KlantAdres(112);
if(!$adres) {
echo '<font color="#FF0000">Adres <u>niet</u> gevonden.</font>';
} else {
echo $adres['plaats'];
}
?>
edit: overigens als je de juiste kolommen ophaalt in je query kan je direct $row teruggeven en hoef je niet eerst alles dubbel toe te wijzen.
Gewijzigd op 27/06/2010 10:16:39 door Jan geen
Gewijzigd op 27/06/2010 12:24:31 door - SanThe -
Hij gebruikt die bij het land, die staat waarschijnlijk als afkorting in de database. $adres['land'] = $countryArray[$row['land']];
Rik geen op 27/06/2010 12:21:56:
Hij gebruikt die bij het land, die staat waarschijnlijk als afkorting in de database. $adres['land'] = $countryArray[$row['land']];
Oep, je hebt gelijk, ik had dat niet gezien. :(
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
<?php
if(mysql_num_rows($query) == 0){
$adres['plaats'] = '<font color="#FF0000">Plaats <u>niet</u> gevonden.</font>';
$adres['postcode'] = '<font color="#FF0000">Postcde <u>niet</u> gevonden.</font>';
$adres['adres'] = '<font color="#FF0000">Adres <u>niet</u> gevonden.</font>';
$adres['land'] = '<font color="#FF0000">Land <u>niet</u> gevonden.</font>';
}
?>
if(mysql_num_rows($query) == 0){
$adres['plaats'] = '<font color="#FF0000">Plaats <u>niet</u> gevonden.</font>';
$adres['postcode'] = '<font color="#FF0000">Postcde <u>niet</u> gevonden.</font>';
$adres['adres'] = '<font color="#FF0000">Adres <u>niet</u> gevonden.</font>';
$adres['land'] = '<font color="#FF0000">Land <u>niet</u> gevonden.</font>';
}
?>
Kan wel, is niet zo mooi met het oog op hergebruik.