Saumay Paul
Saumay Paul

Reputation: 425

multiple if condition php

I am new to PHP and I am making this page and I want to display the Records from database IF there is any value in the records. I am using If condition but, it's not working. Here's my code:

 $active_proof=FALSE;
 while ($row_proof = mysqli_fetch_assoc($res_proof)){

if ($row_proof['proof_cat'] == "Proof of Address"  && $row_proof['proof_cat'] == "Proof of Identity"  ){

           echo '<tr>

                <td>'.$row_proof['proof_cat'].'</td>
                <td>'.$row_proof['proof_doc'].'</td>

            </tr>';
            $active_proof = TRUE;
        }else{
        $active_proof  = FALSE;
    }

    }

Help me guys!

Upvotes: 0

Views: 39

Answers (4)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

If I don't misinterpret your code, then you need this $row_proof['proof_cat'] and $row_proof['proof_doc'] not two $row_proof['proof_cat'] to check for "Proof of Address" and "Proof of Identity" at same time because it is not possible. So I think you did some typo here that's why it is not working for you.

But if you just want to check any of the value is equal of $row_proof['proof_cat'] then use in_array() instead like below

If you want to check any of equal to

if (in_array($row_proof['proof_cat'], ["Proof of Address", "Proof of Identity"])) {
 //your code will goes here
}

If it is a typo: why I interpret it as typo?

Your this code-block tell me to think it as a typo.

'.$row_proof['proof_cat'].'

'.$row_proof['proof_doc'].'

 $active_proof=FALSE;
 while ($row_proof = mysqli_fetch_assoc($res_proof)){

if ($row_proof['proof_cat'] == "Proof of Address"  && $row_proof['proof_doc'] == "Proof of Identity"  ){

           echo '<tr>

                <td>'.$row_proof['proof_cat'].'</td>
                <td>'.$row_proof['proof_doc'].'</td>

            </tr>';
            $active_proof = TRUE;
        }else{
        $active_proof  = FALSE;
    }

    }

I

Upvotes: 0

Dr M L M J
Dr M L M J

Reputation: 2397

Are you sure to check both values for proof_cat ? Or for proof_cat and proof_doc ?? I think it should be...

$active_proof=FALSE;
 while ($row_proof = mysqli_fetch_assoc($res_proof)){

if (($row_proof['proof_cat'] == "Proof of Address")  && ($row_proof['proof_doc'] == "Proof of Identity"  )){

       echo '<tr>

            <td>'.$row_proof['proof_cat'].'</td>
            <td>'.$row_proof['proof_doc'].'</td>

        </tr>';
        $active_proof = TRUE;
    }else{
    $active_proof  = FALSE;
    }

 }

Upvotes: 1

Devon Bessemer
Devon Bessemer

Reputation: 35337

A string can't be equal to 2 distinct values, I'm assuming you'd want a logical OR (||) operator instead of AND (&&).

A clean way of doing this, especially with more than 2 values is using in_array:

if (in_array($row_proof['proof_cat'], ["Proof of Address", "Proof of Identity"])) {

Upvotes: 0

Rinsad Ahmed
Rinsad Ahmed

Reputation: 1933

This piece of code will never become true

if ($row_proof['proof_cat'] == "Proof of Address"  && $row_proof['proof_cat'] == "Proof of Identity"  ){

as a single column 'proof_cat' cannot carry 2 values. If you meant OR then it will become

if ($row_proof['proof_cat'] == "Proof of Address"  || $row_proof['proof_cat'] == "Proof of Identity"  ){

Upvotes: 1

Related Questions