Asif
Asif

Reputation: 77

How to apply Three type of conditions in php rephrased version

Here is my rephrased version of the question:

How to apply Three type of conditions in php

I have two tables with status fields, set as disabled and enabled.

I want to get those two condition and throw another query based on the results.

But I am getting empty page , when i compare the results.

Below is the code i am using.

    $sqltable1 = "select * from table1 ";
    $result=$conn->query($sqltable1);
    while($row = $result->fetch_assoc()) {
            echo "row: " . $row["status"]. "<br>";
        }

    $sqltable2 = "select * from table2";
    $result=$conn->query($sqltable2);
    while($row1 = $result->fetch_assoc()) {
            echo "row1: " . $row1["status"]. "<br>";
        }

        if ( $row["status"] ==  "enabled"  && $row1["status"]== "disabled"  )       {
echo "condition 1 is enabled"
        }
        elseif ( $row["status"] ==  "disabled"  && $row1["status"]== "enabled"  )   {
echo "condition 2 is enabled"
        }
        elseif ( $row["status"] ==  "enabled"  && $row1["status"]== "enabled"  )    {
echo "both conditions are enabled"

        }
        elseif ( $row["status"] ==  "disabled"  && $row1["status"]== "disabled"  )  {
echo "both conditions are disabled"

        }

It would be equally great if you guys can provide me with different logic to achieve this.

question: the comparison results are not working. And empty page is disaplyed for comparisons. I need the comparison condition to work so that i can write the code further.

Upvotes: 0

Views: 62

Answers (1)

Francisco Presencia
Francisco Presencia

Reputation: 8841

Note: not an answer. However it didn't fit in the comments. Apparently a real answer. Just a way of dealing with what can become an if hell:

$sqltable1 = "SELECT * FROM table1";
$result = $conn->query($sqltable1);
$row = $result->fetch_assoc());

$sqltable2 = "SELECT * FROM table2";
$result = $conn->query($sqltable2);
$row1 = $result->fetch_assoc());

$condition = $row["status"] == "enabled" ? 10 : 0;
$condition += $row1["status"] == "enabled" ? 1 : 0;

switch($condition) {
  // 1 disabled, 2 disabled
  case 00:
    // Code
    break;

  // 1 disabled, 2 enabled
  case 01:
    // Code
    break;

  // 1 enabled, 2 disabled
  case 10:
    // Code
    break;

  // 1 enabled, 2 enabled
  case 11:
    // Code
    break;
  }

Upvotes: 1

Related Questions