仅在if语句循环中显示第一项

问题描述 投票:0回答:1

此代码:

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;

从道路输出当前天气报告。可以打开/关闭或维护。

我的问题:

可能存在可能导致我不想要的openopenmaintenanceopen的输出。

我尝试过:

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

但是在所有可能的情况下,这都很复杂且杂乱,并且效果不佳。

我想要的:如果有多个报告,则仅显示第一个报告,并在文本后加上*。

非常感谢您的帮助!

php if-statement
1个回答
1
投票

此代码进行了一些更改。

而不是使用输出缓冲,这只是将值一直设置为$status。这样,您可以检查是否设置了先前的值,并将*添加到末尾,或者如果它到达循环的末尾并且$status仍为空,则可以设置打开的文本。

我还将静态文本分配移到了循环之外,因为您不需要每次都设置它们。

最近您两次有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';

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

[设置橙色和红色部分时,您可以删除(strpos($f->description, $exception_text) === false)的测试,如果$exception_text在文本中,则只需忽略该项目。

© www.soinside.com 2019 - 2024. All rights reserved.