PHP Declarations If - Get a result in While Loop

advertisements

I may be going about this the wrong way, but I have a simple setup in which I am trying to do the following:

<?php
...
if ($variable == "this")
{
    echo "this";
}
elseif ($variable == "that")
{
    echo "that";
}
else
{
   echo "neither";
}
...
?>

In my case, I am doing the if statements within a while loop, so $variable may equal this within one iteration, and that within another iteration, and therefore I get both results displayed whenever $variable is equal to this or that - which is what I want. So far so good.

However, if neither result is true for ALL of the $variable iterations, I only want to display neither one time (whenever all iterations of $variable does not equal to this or that within my while loop). Since I am in a while loop, I end up getting neither multiple times, once for each time that $variable is not equal to either this or that.

So my question is - how do I get the result of neither to only appear once when none of the $variable iterations are equal to this or that?

EDIT: Clarified post a bit...and here's a more complete example with my code (simplified a bit so as to not take up much space):

$results = $con->query("SELECT * FROM `order`"); <-- $con is just a mysqli_connect statement.
if ($results)
{
    while($obj = $results->fetch_object())
    {
        $status = $obj->status;
        if ($status == "this")
        {
            echo "this"; <-- this is actually html code, but let's pretend it's just this.
        }
        elseif ($status == "that")
        {
            echo "that";
        }
        else
        {
            echo "neither";
        }
    }
}


Your edit certainly makes a lot of difference.

First, are you able to change the SQL query that you use for search? If yes, you can try this:

// Perform search only for the valid status, which are 'this' and 'that'
$results = $con->query("SELECT * FROM `order` WHERE status IN ('this', 'that')");

if ($results)
{
    if ($results->num_rows == 0)
    {
        echo 'neither';
    }
    else
    {
        while($obj = $results->fetch_object())
        {
            if ($obj->status == 'this')
            {
                echo 'this';
            }
            elseif ($obj->status == 'that')
            {
                echo 'that';
            }
        }
    }
}

Alternatively, if changing the query is not an option, you will need to loop twice: Once to check whether you have at least a this or that, and second to print the actual result.

$has_this_or_that = false;
$results = $con->query("SELECT * FROM `order`"); <-- $con is just a mysqli_connect statement.
if ($results)
{
    while($obj = $results->fetch_object() && ! $has_this_or_that)
    {
        $status = $obj->status;

        if ($status == 'this' || $status == 'that')
        {
            $has_this_or_that = true;
        }
    }

    if ($has_this_or_that)
    {
        // Reference: http://php.net/manual/en/mysqli-result.data-seek.php
        $results->data_seek(0);

        while($obj = $results->fetch_object())
        {
            $status = $obj->status;

            if ($status == 'this')
            {
                echo 'this'; <-- this is actually html code, but let's pretend it's just this.
            }
            elseif ($status == 'that')
            {
                echo 'that';
            }
        }
    }
    else
    {
        echo 'neither';
    }

        else
        {
            echo 'neither';
        }
    }
}