Page 1 of 1

Problem with elseif

Posted: Mon Nov 11, 2002 5:55 am
by Skywalker
This my code. But the php codes doe the elseif statement correct. It is just keeping the first number $InBelNr1 insted of searching for the right areacode and put the write $InBelNr in to $NR.

This is my code.

<?php
$AreaCode = $_POST['AreaCode'];
$InBelNr1 ="0108800844";
$InBelNr2 ="0113880844";
$InBelNr3 ="0138800844";
$InBelNr4 ="0167880844";
$InBelNr5 = 0183880844;
$InBelNr6 = 0208800844;
$InBelNr7 = 0223880844;
$InBelNr8 = 0229880844;
$InBelNr9 = 0238800844;
$InBelNr10 = 0248800844;
$InBelNr11 = 0268800844;
$InBelNr12 = 0308800844;
$InBelNr13 = 0315880844;
$InBelNr14 = 0338800844;
$InBelNr15 = 0344880844;
$InBelNr16 = 0368800844;
$InBelNr17 = 0388800844;
$InBelNr18 = 0408800844;
$InBelNr19 = 0438800844;
$InBelNr20 = 0508800844;
$InBelNr21 = 0513880844;
$InBelNr22 = 0528880844;
$InBelNr23 = 0558800844;
$InBelNr24 = 0570880844;
$InBelNr25 = 0588800844;
$InBelNr26 = 0599880844;
$InBelNr27 = 0708800844;
$InBelNr28 = 0718800844;
$InBelNr29 = 0738800844;
$InBelNr30 = 0748800844;
$InBelNr31 = 0768800844;
$InBelNr32 = 0778800844;


if ($AreaCode ==("010")or("015")or("0174")or("0180")or("0181")or("0186")or("079")) {
$NR = $InBelNr1; }
elseif ($AreaCode ==("0111")or("0113")or("0114")or("0115")or("0117")or("0118")or("0164")or("0166")) {
$NR = $InBelNr2; }
elseif ($AreaCode == (013)or(0161)or(0162)or(0411)or(0416)or(0497)or(0499)) {
$NR = $InBelNr3; }
elseif ($AreaCode == (0111)or(0164)or(0165)or(0166)or(0167)or(0168)or(0187)) {
($NR = $InBelNr4); }
elseif ($AreaCode ==(0111)or(0164)or(0165)or(0166)or(0167)or(0168)or(0187)) {
($NR = $InBelNr5); }
elseif ($AreaCode == (0162)or(0182)or(0183)or(0184)or(0345)or(0347)or(0416)or(0418)or(078)) {
($NR = $InBelNr6); }
elseif ($AreaCode == (020)or(023)or(0294)or(0297)or(0299)or(036)or(075)) {
($NR = $InBelNr7); }
elseif ($AreaCode == (0222)or(0223)or(0224)or(0227)) {
($NR = $InBelNr8); }
elseif ($AreaCode == (0224)or(0226)or(0227)or(0228)or(0229)or(0299)or(072)) {
($NR = $InBelNr9); }
elseif ($AreaCode == (020)or(023)or(0251)or(0252)or(0255)or(0297)) {
($NR = $InBelNr10); }
elseif ($AreaCode == (024)or(0481)or(0485)or(0486)or(0487)or(0488)) {
($NR = $InBelNr11); }
elseif ($AreaCode == (026)or(0313)or(0316)or(0317)or(0318)or(0481)or(0488)or(055)) {
($NR = $InBelNr12); }
elseif ($AreaCode == (030)or(0343)or(0345)or(0346)or(0347)or(0348)) {
($NR = $InBelNr13); }
elseif ($AreaCode == (0314)or(0315)or(0543)or(0544)) {
($NR = $InBelNr14); }
elseif ($AreaCode == (0317)or(0318)or(0343)or(0344)or(0345)or(0418)or(0487)or(0487)or(0488)) {
($NR = $InBelNr15); }
elseif ($AreaCode == (020)or(0294)or(0320)or(033)or(0341)or(035)or(036)) {
($NR = $InBelNr16); }
elseif ($AreaCode == (0321)or(038)or(0522)or(0525)or(0527)or(0529)or(0578)) {
($NR = $InBelNr17); }
elseif ($AreaCode == (040)or(0492)or(0493)or(0495)or(0497)or(0499)) {
($NR = $InBelNr18); }
elseif ($AreaCode == (043)or(045)or(046)) {
($NR = $InBelNr19); }
elseif ($AreaCode == (050)or(0592)or(0594)or(0595)or(0596)or(0598)) {
($NR = $InBelNr20); }
elseif ($AreaCode == (0512)or(0513)or(0514)or(0515)or(0516)or(0561)or(0566)) {
($NR = $InBelNr21); }
elseif ($AreaCode == (0521)or(0522)or(0523)or(0524)or(0528)or(0593)) {
($NR = $InBelNr22); }
elseif ($AreaCode == (026)or(0313)or(0318)or(055)or(0571)or(0575)or(0577)or(0578)){
($NR = $InBelNr23); }
elseif ($AreaCode == (0529)or(0548)or(0570)or(0571)or(0572)or(0573)or(0575)or(0578)) {
($NR = $InBelNr24); }
elseif ($AreaCode == (0511)or(0515)or(0517)or(0518)or(0519)or(0562)or(0566)or(058)) {
($NR = $InBelNr25); }
elseif ($AreaCode == (0591)or(0592)or(0597)or(0598)or(0599)) {
($NR = $InBelNr26); }
elseif ($AreaCode == (015)or(0174)or(070)or(071)or(079)) {
($NR = $InBelNr27); }
elseif ($AreaCode == (0172)or(0252)or(070)or(071)or(079)) {
($NR = $InBelNr28); }
elseif ($AreaCode == (0411)or(0412)or(0413)or(0416)or(0418)or(073)) {
($NR = $InBelNr29); }
elseif ($AreaCode == (053)or(0541)or(0545)or(0546)or(0547)or(047)) {
($NR = $InBelNr30); }
elseif ($AreaCode == (0161)or(0162)or(0165)or(0168)or(076)) {
($NR = $InBelNr31); }
elseif ($AreaCode == (0475)or(0478)or(0493)or(0495)or(077)) {
($NR = $InBelNr32); }
?>

Posted: Mon Nov 11, 2002 6:05 am
by twigletmac
Basically this doesn't work as expected:

Code: Select all

if ($AreaCode ==("010")or("015")or("0174")or("0180")or("0181")or("0186")or("079")) {
if you want to have multiple conditionals you're going to have to do something like

Code: Select all

if ($AreaCode == '010' or $AreaCode == '015' /*etc*/) {
(on a side note in PHP most people tend to use '||' instead of 'or')

Although for what you're doing having things associated in arrays may be cleaner and easier to maintain.

Mac

Posted: Mon Nov 11, 2002 7:00 am
by seg
twigletmac wrote:Although for what you're doing having things associated in arrays may be cleaner and easier to maintain.
I definatly agree with that. Better yet, taking the concept and moving it into a MySQL database would be even better.

Posted: Mon Nov 11, 2002 8:52 am
by Skywalker
Yes, but I don't know how to put in al the numbers in an array and bring back then the right $InBelNr.

Posted: Mon Nov 11, 2002 9:00 am
by twigletmac
Having this stuff in a database table would be my favoured way to go too but if you want to go the PHP route,

For more information on arrays:
http://www.php.net/manual/en/ref.array.php

You'll probably be most interested in:
array() - for creating an array, and
in_array() and/or array_key_exists() depending how you set up your array.

If you do a search on the web you'll probably find some tutorials too.

Mac

Posted: Mon Nov 11, 2002 12:27 pm
by f1nutter
A switch statement is better then a lot of elseif statements. If you know that the numbers are not going to change, you might not need a database.

It also looks like you are just adding a string to the end of your tests. Look at all those '8800844' substrings.

Code: Select all

&lt;?php

$ending = "8800844";

switch($AreaCode)
{
 case: "010"
 case: "015"
 case: "0174"
 case: "0180"
 case: "0181"
 case: "0186"
 case: "079"
  $NR = "010" .$ending;
  break;
 case: "0111"
 case: "0113"
 case: "0114"
 case: "0115"
 case: "0117"
 case: "0118"
 case: "0164"
 case: "0166"
  $NR = "0113" .$ending;
  break;
 case: "013"
 ...etc...
}
?&gt;
Repeat until you fall asleep!!

You have also mixed strings and numbers (i.e. double quotes)

edit: You also do not have "exclusive" tests, you try 020 twice, which is a waste of processing time.

Posted: Mon Nov 11, 2002 4:02 pm
by Skywalker
Yes I know It can always be done beter, but I am still in a learning period with php so that means I don't have all the things that you guys know.

But thx enyway I'll try this one 2 :D

Posted: Mon Nov 11, 2002 5:27 pm
by phpScott
I don't think any one meant any disrespect or anything but if you post a question people will try to help you out with their knowledge and experience and hopefully help you avoid many of the pitfalls and problems we encountered as we where developing our skills.

Big happy forum thats what we are. :D :D :D

Posted: Mon Nov 11, 2002 5:30 pm
by volka
Image and Image

:D

Posted: Tue Nov 12, 2002 3:57 am
by f1nutter
I'm sorry if my writing style causes offence or I come across a bit sharp, but I find that once I know something, I store it away and its hard to explain things clearly to other people. I will try to be more sympathetic without being too patronising.

Group hug :D

Posted: Tue Nov 12, 2002 4:18 am
by Skywalker
Ok (dan)( in dutch) I like you guys, because you are always replying that's cool :D