Texan78 Texan78 - 1 month ago 6
PHP Question

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

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

Comments