0

I have a problem, I can't get the result of my code. When I try to debug the code. I using IF statement in my code.

It's just simple, example : if kelas(based on user_id login) = 1, then redirect it to kelas1.php, if kelas = 2, the redirect to kelas 2, else no have a kelas.

Here it's my code :

<?php
session_start();

if(!isset($_SESSION['user_id'])) {      
    header('Location: form_login_siswa.html');
}
$user_id = $_SESSION['user_id'];
include ("config.php");
$query = "SELECT kelas FROM t_siswa WHERE user_id = '$user_id'";
$hasil = mysql_query($query);

while ($data = mysql_fetch_array($hasil)) {
    $kelas = $data['kelas'];

    if($kelas = 1) {
        include ("kelas1.php");
    }
    if($kelas = 2) {
        include ("kelas2.php");
    } else {
        echo "Tidak ada kelas";
    }
}
?>

Anyone, please help to solve the problem. Appreciated with your helps.

Thank you.

1
  • 1
    You should probably start by reading the language reference portion of the PHP manual. If you're not comfortable using conditionals yet, then it's very likely that there are other fundamentals of the PHP language you're also insufficiently versed in, and this will seriously impede any other efforts you make to advance your knowledge if you don't have your foundations set. Also, format your code neatly. Find a standard coding style and follow it. Otherwise, it's going to make debugging problems in your code very difficult and introduce errors. Commented Jul 6, 2012 at 14:58

6 Answers 6

4

You're missing an else and also not using the correct operator for comparison.

Also, switch around the comparison so that the first value is always a valid value.

if(1 == $kelas)
{
    include ("kelas1.php");
}
else if(2 == $kelas)
{
    include ("kelas2.php");
}
else
{
    echo "Tidak ada kelas";
}
Sign up to request clarification or add additional context in comments.

6 Comments

or use switch and case, the same effect
Switching the comparison is not necessary. It only prevents you from making an error because 1 = $kelas will throw an error.
yea, but i mean if you have 1 variable and many options, you can use switch and case to check all values rather than elseif or else if. But both ways give the same effect.
I've tried the code, but still not working... when I debug the code, nothing change.
what does "not working" mean? Is your code printing "Tidak ada kelas" all the time? Check what is in the $data variable if the results are not what you're expecting them to be.
|
2
$kelas = 1

is assignment

$kelas == 1

is comparison

Comments

1
if($kelas = 1)

should be

if($kelas == 1)

Comments

1

Try using == instead of = . The "=" is an assignment operator and the expression gets the value of the right operand.

if($kelas == 1)
{
    include ("kelas1.php");
}
if($kelas == 2)
{
    include ("kelas2.php");
}
else
{
    echo "Tidak ada kelas";
}

Also, the statement will always echo "Tidak ada kelas"; if ($kelas == 1). Was this intentional?

Comments

0

In your if statements using == for testing comparison rather than single = which is for assignment

Comments

0

Take a look at the elsif structure.

while ($data = mysql_fetch_array($hasil)) {
  $kelas = $data['kelas'];

  if($kelas == 1) {
    include ("kelas1.php");
  } elseif($kelas == 2) {
    include ("kelas2.php");
  } else {
    echo "Tidak ada kelas";
  }
}

Or the switch structure:

while ($data = mysql_fetch_array($hasil)) {
  switch($data['kelas']){
    case 1:
      include ("kelas1.php");
      break;
    case 2:
      include ("kelas2.php");
      break;
    default:
    echo "Tidak ada kelas";
  }
}

1 Comment

can you check by echoing in each condition,just to check in which condition it is going ? @user1473757

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.