2

I have written the following PHP, to basically show a LI if the status is set to Offline or Delay:

<?php
    if(count($rows) > 0) {  
        foreach($rows as $row)
            {
                $service = $row->service;
                $details = $row->status_details;
                $status = $row->status;
                // status image check       
                    if($status == "ONLINE")
                    {
                        $symbol = "online.png";
                    }
                    if($status == "OFFLINE")
                    {
                        $symbol = "offline.png";
                    }
                    if($status == "DELAY")
                    {
                        $symbol = "delay.png";
                    }
        ?>

        <li class="<?php echo $status; ?>">
            <img src="<?php echo $moduleLoc, $symbol;?>" alt="Service Status <?php echo $service; ?>" /> <span class="details"><?php echo $details; ?></span></li>
        </li>
        <?php   

                }

        ?>
        <?php } ?>

Using CSS, if the class of "ONLINE" is then shown, then LI is hidden.

However, I want to add a statement to be shown if the status is OFFLINE or DELAY, I've written the following, which is working but due to the foreach statement, it's showing for every LI that has either OFFLINE or DELAY. I want it to be shown only once:

<?php
    if(($status == "OFFLINE") || ($status == "DELAY")){
        echo '<li class="systemsGo"><img src="' . $moduleLoc . 'online.png" alt="Online Update" /><span class="details">All Other Applications Are Working Well</span></li>';   
    }
?>
  • Do you want to display this if all the statuses of the results in the foreach loop are OFFLINE or DELAY? Or do you only want to display it if, for example, the status of the first result status is OFFLINE or DELAY? – Lodder Oct 8 '15 at 13:48
  • I want it to display if any of the status is set as OFFLINE or DELAY. Ideally, I'd want another message to be shown if all systems are ONLINE, saying 'All System Are Running Normally' – RustyJoomla Oct 8 '15 at 13:51
2

Right, I've come up with one method.

You first need to define a variable as an array:

$get_status = array();

Then in the foreach loop, you need to combine the status of all the results in this newly defined array, like so:

foreach ($rows as $row)
{
    $get_status[] = $status;
}

After the foreach loop, you'll need to extract the array, like so:

extract($get_status);

then you can check if all the statuses in the array are all the same and display a message, like so:

if (count(array_unique($get_status)) === 1 && end($get_status) === 'ONLINE')
{
    echo 'All systems are online';
}

Full code and tested:

if (count($rows) > 0) 
{
    $get_status = array();

    foreach ($rows as $row)
    {
        $service = $row->service;
        $details = $row->status_details;
        $status  = $row->status;
        $symbol  = '';

        // status image check       
        if ($status == 'ONLINE')
        {
            $symbol = 'online.png';
        }
        if ($status == 'OFFLINE')
        {
            $symbol = 'offline.png';
        }
        if ($status == 'DELAY')
        {
            $symbol = 'delay.png';
        }

        $get_status[] = $status;
    ?>
        <li class="<?php echo $status; ?>">
            <img src="<?php echo $moduleLoc, $symbol;?>" alt="Service Status <?php echo $service; ?>" />
            <span class="details"><?php echo $details; ?></span>
        </li>

    <?php   
    }

    extract($get_status);

    if (count(array_unique($get_status)) === 1 && end($get_status) === 'ONLINE')
    {
        echo 'All systems are online';
    }
    else if (count(array_unique($get_status)) === 1 && end($get_status) === 'OFFLINE')
    {
        echo 'All systems are offline';
    }
    else if (count(array_unique($get_status)) === 1 && end($get_status) === 'DELAY')
    {
        echo 'All systems are delayed';
    }
    else
    {
        echo 'Not the same';
    }
}

Hope this helps

P.S: I've made a few minor tweaks such as replacing double quotes with single quotes and also defined $symbol outside of your if statements, just incase the $status doesn't match (will throw a PHP error otherwise)

  • You're welcome ;) I had some variables commented out and some custom styling for when I was testing. Forgot to remove it before, but have done so now, just incase you accidentally copied it. – Lodder Oct 8 '15 at 15:15
  • Yeah I noticed those and removed them at my end. Thanks again for your help! – RustyJoomla Oct 8 '15 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.