Texan78
Texan78

Reputation: 687

If, else with Conditional Operators: equal, greater...

I am having some issues with some else if statements I am currently working on where if a certain value is equal to a certain number display a certain message else if greater then display a different message. I have tried a number of different combinations without success.

What I am trying to accomplish is if the total number of "true" statements from the array is 1 display $notify1Online message and if it is 2 or greater the display the $notifyOnline message else display the $notifyOffline message. I have gone through the PHP manual and done a number of research and played with some different combinations but cannot seem to get this to work. Could someone offer some assistance as to what I am doing wrong.

$resultArr = array();//to store results

//lets execute the query
$executingFetchQuery = $mysqli->query("SELECT `StreamStatus` FROM streamdb WHERE 1");
if($executingFetchQuery)
{
   while($arr = $executingFetchQuery->fetch_assoc())
   {
        $resultArr[] = $arr['StreamStatus'];//storing values into an array
   }
}

$counts = array_count_values($resultArr);//lets count the results
$online = $counts['true'];
$total = (in_array("true", $resultArr));

// Lets assemble the banners to display
$notifyOffline = '<div class="alert alert-danger" role="alert" data-toggle="tooltip" data-placement="top" title=" "> There are currently no active chasers online streaming at this time.</div>';

$notify1Online = '<div class="alert alert-success" role="alert" data-toggle="tooltip" data-placement="top" title=" "> There is currently 1 chaser streaming LIVE... </div>';

$notifyOnline = '<div class="alert alert-success" role="alert" data-toggle="tooltip" data-placement="top" title=" "> There are currently '.$online.' chasers streaming LIVE... </div>';


//lets display the banners
if ( $total = "1" ) {
    echo $notify1Online;
} elseif ( $total < "2" ) {
    echo $notifyOnline;
} else {
    echo $notifyOffline;
}

When I try this it works fine until I try to add the extra elseif statement then it breaks and doesn't display the correct message.

//lets display the banners
if ( $online == true ) {
    echo $notifyOnline;
} else {
    echo $notifyOffline;
} 

Upvotes: 0

Views: 410

Answers (3)

RJParikh
RJParikh

Reputation: 4166

Comparison operator is == not =. Change your if condition and use below code. Also use same type in comparison as you are getting result of $total in integer type so you need to use integer.

$a == $b Equal TRUE if $a is equal to $b after type juggling.

$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.

//lets display the banners
if ( $total == 1 ) {
    echo $notify1Online;
} else if ( $total < 2 ) {
    echo $notifyOnline;
} else {
    echo $notifyOffline;
} 

Upvotes: 0

Rajesh Patel
Rajesh Patel

Reputation: 2036

You should use == equal or === equal for comparasion

//lets display the banners
if ( $total == "1" ) {
    echo $notify1Online;
} elseif ( $total < "2" ) {
    echo $notifyOnline;
} else {
    echo $notifyOffline;
}

Upvotes: 0

javier_domenech
javier_domenech

Reputation: 6253

//lets display the banners
if ( $total == "1" ) {
    echo $notify1Online;
} elseif ( $total >= "2" ) {
    echo $notifyOnline;
} else {
    echo $notifyOffline;
}

=== for comparissons, value and type

== for comparissons, only value

= for asigning value to a variable

< less than

> greather than

<= less or equal

>= greather or equal

Upvotes: 1

Related Questions