1

i have a problem regarding using if else nested in php, the $hasilkonsentrasi is unreadable, Thankyou

<div class="alert alert-info">
  <?php  
   if ($hasil[0] > $hasil[1]) {
    if ($hasil[0] > $hasil[2]) {
     if ($hasil[0] > $hasil[3]) {
     $hasilkonsentrasi = "Manajemen Keamanan Jaringan";
     }     
    }
   }
   elseif ($hasil[1] > $hasil[0]) {
    if ($hasil[1] > $hasil[2]) {
     if ($hasil[1] > $hasil[3]) {
      $hasilkonsentrasi = "Teknologi Cerdas";
     }
    }
   }
   elseif ($hasil[2] > $hasil[0]) {
    if ($hasil[2] > $hasil[1]) {
     if ($hasil[2] > $hasil [3]) {
      $hasilkonsentrasi = "Manajemen Bisnis";
     }

    }
   }
   elseif ($hasil[3] > $hasil[0]) {
    if ($hasil[3] > $hasil[1]) {
     if ($hasil[3] > $hasil[2]) {
      $hasilkonsentrasi = "Manajemen Data dan Informasi";
     }

    }
   }
   echo "Anda cocok mengambil konsentrasi <strong>$hasilkonsentrasi</strong>";


  $mkj= number_format($hasil[0], 2, '.', '');
  $tc=$hasil[1];
  $mb=$hasil[2];
  $mdi=$hasil[3];
  $jurusan=$hasilkonsentrasi;
  $user->SimpanHasilJurusan($mkj,$tc,$mb,$mdi,$jurusan);
  ?>
 </div>
</div>
3
  • 1
    What do you mean $hasilkonsentrasi is unreadable? And you can probably clean those if-blocks up to make them more readable. Commented Dec 4, 2019 at 11:53
  • 3
    Please, use English words in your variables and strings, so everybody can understand the context and the problem in order to help you Commented Dec 4, 2019 at 11:58
  • i give some logic instrument, sorry if i cant open that code clearly. this is the logic: a is 1.75 b is 1.67 c is 1.82 d is 1.87 and $hasilkonsentrasi will be "d", where the value of "d" is greater than all thank you for ur respond Commented Dec 4, 2019 at 13:32

2 Answers 2

1

syntax

if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if first condition is false and this condition is true;
} else {
    code to be executed if all conditions are false;
} 

try this one in last else there is no need to add condition

<div class="alert alert-info">
  <?php  
   if ($hasil[0] > $hasil[1]) {
    if ($hasil[0] > $hasil[2]) {
     if ($hasil[0] > $hasil[3]) {
     $hasilkonsentrasi = "Manajemen Keamanan Jaringan";
     }     
    }
   }
   elseif ($hasil[1] > $hasil[0]) {
    if ($hasil[1] > $hasil[2]) {
     if ($hasil[1] > $hasil[3]) {
      $hasilkonsentrasi = "Teknologi Cerdas";
     }
    }
   }
   elseif ($hasil[2] > $hasil[0]) {
    if ($hasil[2] > $hasil[1]) {
     if ($hasil[2] > $hasil [3]) {
      $hasilkonsentrasi = "Manajemen Bisnis";
     }

    }
   }
   else {
    if ($hasil[3] > $hasil[1]) {
     if ($hasil[3] > $hasil[2]) {
      $hasilkonsentrasi = "Manajemen Data dan Informasi";
     }

    }
   }
   echo "Anda cocok mengambil konsentrasi <strong>$hasilkonsentrasi</strong>";


  $mkj= number_format($hasil[0], 2, '.', '');
  $tc=$hasil[1];
  $mb=$hasil[2];
  $mdi=$hasil[3];
  $jurusan=$hasilkonsentrasi;
  $user->SimpanHasilJurusan($mkj,$tc,$mb,$mdi,$jurusan);
  ?>
 </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Nice one, but theres still error where $hasilkonsetrasi still unreadable. Sory for bad english , Thank you
1

What you seem to be looking for is the key of the $hasil entry with the largest value. There are many ways to do this, but I would suggest sorting the array, while preserving keys, and then use a switch on the key value. like this:

<div class="alert alert-info">
  <?php  
   arsort($hasil);
   switch(array_key_first($hasil)) {
     case 0  : $hasilkonsentrasi = "Manajemen Keamanan Jaringan";
               break;   
     case 1  : $hasilkonsentrasi = "Teknologi Cerdas";
               break;   
     case 2  : $hasilkonsentrasi = "Manajemen Bisnis";
               break;   
     case 3  : $hasilkonsentrasi = "Manajemen Data dan Informasi";
               break;   
     default : $hasilkonsentrasi = "Not found";
               break;   
   }
   echo "Anda cocok mengambil konsentrasi <strong>$hasilkonsentrasi</strong>";
  ?>
</div>  

This is also still doable when there are 6 entries in the array, whereas if you wanted to do that with if () else the code would really become unreadable.

3 Comments

Very nice answer! :-)
that's what exactly i'm searching for, but unfortunately there some problem : 1. i'm using old php 2. i'm using what link tell, but there was error like Catchable fatal error: Argument 1 passed to array_key_first() must be of the type array, boolean given Thank you for ur reply, sory for bad english
@MadeSuralaga Sorry, that was my mistake, I have corrected the code. Please note that arsort($hasil); actually changes (sorts) $hasil. If you don't want that you have to use a copy of the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.