Storm1
Storm1

Reputation: 209

only show first item in if-statment loop

This code:

require_once 'feed.php';

$title = 'Ev 134';

ob_start();
foreach(Feed('example.url') as $f ) {
    if (strpos($f->title, $title) !== false) {

        $green_color    = 'green';
        $orange_color   = 'orange';
        $red_color      = 'red';
        $closed_text    = 'closed';
        $maintenance_text   = 'maintenance';
        $exception_text = 'could be';

        if (strpos($f->title, $title) !== false){

            if(strpos($f->description, $closed_text) !== false){
                echo (strpos($f->description, $exception_text) === false) ?
                     '<span style="color:'.$red_color.';text-shadow: 2px 2px #a50000;">closed</span>' :
                    '<span style="color:'.$green_color.'">Open</span>' ;

            } else if(strpos($f->description, $maintenance_text) !== false){
                echo (strpos($f->description, $exception_text) === false) ?
                     '<span style="color:'.$orange_color.'">maintenance</span>' :
                     '<span style="color:'.$green_color.'">Open</span>' ;

            } else {
                echo '<span>Open</span>';
            }
        }
    }
}
$status = ob_get_contents();
ob_end_clean();

echo $status;

Outputs the current weather report from a road. Eather open/closed or maintenance.

My problem:

There could potentially be to outputs which would result in openopen or maintenanceopen which I don`t want.

I have tried:

if ($status = 'OpenOpen'){
  $status = 'Open';
}

but it's complicated and messy to this with all possible scenarios + it doesn't work that well.

What I want: if there are multiple reports only show the first and put * behind the text.

Any help is greatly appreciated!

Upvotes: 0

Views: 30

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

This code make a couple of changes.

Rather than using output buffering, this just sets values into $status all through. This allows you to check if a previous value has been set and add * to the end, or if it gets to the end of the loop and $status is still empty, it can set the open text.

I've also moved the static text assignments outside of the loop as you don't need to set them each time.

Lastly you had if (strpos($f->title, $title) !== false) in twice, so this removes that...

$green_color    = 'green';
$orange_color   = 'orange';
$red_color      = 'red';
$closed_text    = 'closed';
$maintenance_text   = 'maintenance';
$exception_text = 'could be';

$status = "";
$records = 0;
foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=604') as $f ) {
    $records++;
    if (strpos($f->title, $title) !== false) {
        if(strpos($f->description, $closed_text) !== false){
            // If no previous value, set main text,otherwise add *
            if ( empty($status) )   {
                $status = (strpos($f->description, $exception_text) === false) ?
                    '<span style="color:'.$red_color.';text-shadow: 2px 2px #a50000;">closed</span>'
                    : '<span style="color:'.$green_color.'">Open</span>' ;
            }
            else    {
                $status .= "*";
            }

        } 
        else if(strpos($f->description, $maintenance_text) !== false){
            if ( empty($status) )   {
                $status = (strpos($f->description, $exception_text) === false) ?
                    '<span style="color:'.$orange_color.'">maintenance</span>' :
                    '<span style="color:'.$green_color.'">Open</span>' ;

            }
            else    {
                $status .= "*";
            }
        }
    }
}

// If still empty, say open
if ( empty ( $status ) ){
    $status = '<span>Open</span>';
    if ( $records > 0 ) {
        $status.="*";
    }
}
echo $status;

You may be able to remove the tests for (strpos($f->description, $exception_text) === false) when setting the orange and red parts and just ignore that item if the $exception_text is in the text.

Upvotes: 1

Related Questions