0

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;
} 
1
  • $total = "1" should be $total == "1" and < comparison should be using integers not strings. Commented Oct 25, 2016 at 8:12

3 Answers 3

1
//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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help and explanation. I originally was using the == in the first if statement but nothing change so I had removed it. When I added the >= and added back the == that solved the problem. Also I was using the wrong variable. I should have been using $online and not $total. Found that out by echoing those variables by themselves and found it wasn't returning the correct value. Made those changes and now it works great. Thanks for your help and explanation.
Glad to help, I encourage you to keep trying and trying, you are on the right way to learn
0

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;
}

Comments

0

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;
} 

Comments

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.