-1

Sorry for the messy code but basically this code will only ever make $booktype = "ICT" and if I remove the "else" from elseif, it makes it equal ENGLISH.

Whats going wrong?

I've echoed POST_ Book room and that comes out fine.

if ($_POST['bookroom'] == "142" || "040" || "139"|| "104") {
    $booktype = "ICT";
} elseif ($_POST['bookroom'] == "015" || "016" || "017" || "018" || "027" || "028") {
    $booktype = "MATHS";
} elseif ($_POST['bookroom'] == "E03") {
    $booktype = "MUSIC";
} elseif ($_POST['bookroom'] == "202" || "204" || "205" || "206" || "207") {
    $booktype = "ENGLISH";
}
Leandros
  • 16,805
  • 9
  • 69
  • 108

4 Answers4

5

At the end of the day, you're not using 'or' correctly. To shorten your code, you can do your if-statement like this:

if(in_array($_POST['bookroom'], array("142","040","139","104"))) {

To properly use an "or" or "and" clause, you'd need to make the statement self-contained. That is,

if(this option is true || this option is true || this option is true)
if($a == 4 || $a == 5 || $a == 6)
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
1

You can't chain conditions like this. Change it to

$_POST['bookroom'] == "142" || $_POST['bookroom'] == "040" || [...]

Also, using a switch case with fall-throughs would be more readable here.

mrks
  • 8,033
  • 1
  • 33
  • 62
1

You can also change it to use in_array function:

From:

if ($_POST['bookroom'] == "142" || "040" || "139"|| "104"){

to

if (in_array($_POST['bookroom'], array("142","040","139","104"))){
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

You can use switch !

switch($_POST['bookroom']){

case "142":
...
break;

case "040":
...
break;

}
Brotheryura
  • 1,158
  • 13
  • 21
  • Don't use switch! It is messy, hard to read, a bad solution in general. Especially in this particular very simple problem that is solvable with `in_array` function. – Linek May 04 '14 at 22:13
  • @Linek Why switch is bad solution О_о?! – Brotheryura May 04 '14 at 22:19
  • Generally, switch is not bad as a concept but it is bad to use in this particular example. Try go Google "Why is switch bad" and you will find links like this: http://stackoverflow.com/questions/4417070/switch-statements-are-bad Should answer a couple of questions. – Linek May 04 '14 at 22:21