PHP Switch没有输出。

时间:2013-05-09 04:47:22

标签: php

我有这个代码,它从另一个从XML文件中获取的变量中获取值。这在我清理代码之前目前有效,现在我不知道它为什么不起作用。如何工作是获取变量并根据该变量的值创建颜色。然后我在这种情况下采用新的变量$ stormScale并将其分配给CSS类,以便像这样显示单元格的颜色。

$td4Style = "{$sbrdr}; {$bbrdr}; padding: 2px 6px 6px 6px; background-color:{$stormScale};";

然后我把那个类当用于输出其他数据的表时使用它。

$tData .= "  <tr><td style='{$td4Style}'>&nbsp;</td></tr>\n";

此开关假设根据值分配颜色,但它现在不起作用,我不知道为什么或我缺少什么。

$stormScale = '';

           switch($ef)
{
              case '0':
                    $stormScale = 'rgba(0, 255, 255, 0.2)';
                             break;
              case '1':
                    $stormScale = 'rgba(0, 0, 255, 0.1)';
                             break;
              case '2':
                    $stormScale = 'rgba(0, 255, 0, 0.2)';
                             break;
              case '3':
                    $stormScale = 'rgba(255, 255, 0, 0.1)';
                             break;
              case '4':
                    $stormScale = 'rgba(255, 153, 51, 0.2)';
                             break;
              case '5':
                    $stormScale = 'rgba(255, 0, 0, 0.2)';
                             break;

}

这是完整的代码。我希望我提供了足够的信息。我错过了什么?

<?php 
#######################################################################################
#
#  GOOGLE MAPS V3 KILLER TORNADOS
#  version 1.00
#
#  This program is free and no license is required.
#  
#
#  mesquiteweather.net
#
#######################################################################################

////  SETTINGS  ////

$GKey = 'ENTER_YOUR_KEY';                    // Enter your Google Maps API Key
$GImage = 'images/watermark_MW_GMap.png';     // Path to your watermark for your Google Map

// Change colors
$bc        = 'True';     // If True cell tables will show color of EF scale, set to false to use CSS style color
$dtColor   = '#FFF';     // Ttile Color  Examples:   "#FC0"   "#FFCC00"   "white"

////  END OF SETTINGS  ////

#######################################################################################

ini_set('display_errors','1');

// overrides from the Carter Lake Settings.php file (if applicable)
global $SITE;
if(isset($SITE['cacheFileDir'])) {$cacheFileDir = $SITE['cacheFileDir']; }
if (isset($SITE['imagesDir']))   {$imagesDir = $SITE['imagesDir'];}
if(isset ($SITE['tz']))          {$ourTZ = $SITE['tz'];}
if(!function_exists('date_default_timezone_set'))
{
    putenv("TZ=" . $ourTZ);
}
else
{
    date_default_timezone_set("$ourTZ");
}

// get path info & protect for cross-site scripting vulnerability
$sri = ($_SERVER['REQUEST_URI']) ? str_replace('#SA', '', htmlspecialchars(strip_tags($_SERVER['REQUEST_URI']))) : '';

//Set path to data file
$data = "http://www.spc.noaa.gov/climo/torn/xml/2013.xml";

$bbrdr = 'border-bottom:thin solid black';       // bottom
$lbrdr = 'border-left:thin solid black';         // left
$rbrdr = 'border-right:thin solid black';        // right
$tbrdr = 'border-top:thin solid black';          // top
$sbrdr = 'border-right:thin solid black; '.
         'border-left:thin solid black';         // side
$tableStyle = "width: 100%; margin:0px auto; background-color:{$bkgColor};";
$td1Style = "{$tbrdr};{$sbrdr}; padding:2px 0px 2px 6px;  background-image:url({$imagesDir}headerbgd2.gif); color:{$dtColor};";
$td2Style = "{$sbrdr}; padding:6px 0px 0px 6px; background-color:{$stormScale};";
$td3Style = "{$sbrdr}; line-height:5px; background-color:{$stormScale};";
$td4Style = "{$sbrdr}; {$bbrdr}; padding: 2px 6px 6px 6px; background-color:{$stormScale};";

//Define table to display after each storm report
$afterTable = "<table style='margin-bottom: 10px;' border='0' cellpadding='0' cellspacing='0' width='100%'><tbody><tr><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7'></td><td class='shadow-mid' width='100%'><img alt='' src='images/1pixel.gif' border='0' height='7' width='7'></td><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7'></td></tr><tbody></table>\n";


// Lets parse the XML feed
$xml = simplexml_load_file($data);

//Set initial output to false
    $tData = false;
foreach($xml->fatalities as $fatalities){

    $yrnum = $fatalities['yrnum'];
    $dt = $fatalities['dt'];
    $time = $fatalities['time'];
    $updateTime = DATE("g:i a", STRTOTIME($time));
    $tz = $fatalities['tz'];
    $ef = $fatalities['ef'];
    $location = $fatalities['location'];
    $state = $fatalities['st'];
    $watch = $fatalities['watch'];
    $watchn = str_replace("WT","WW","$watch");
    $deaths = $fatalities['deaths'];

  $stormScale = '';

           switch($ef)
{
              case '0':
                    $stormScale = 'rgba(0, 255, 255, 0.2)';
                             break;
              case '1':
                    $stormScale = 'rgba(0, 0, 255, 0.1)';
                             break;
              case '2':
                    $stormScale = 'rgba(0, 255, 0, 0.2)';
                             break;
              case '3':
                    $stormScale = 'rgba(255, 255, 0, 0.1)';
                             break;
              case '4':
                    $stormScale = 'rgba(255, 153, 51, 0.2)';
                             break;
              case '5':
                    $stormScale = 'rgba(255, 0, 0, 0.2)';
                             break;

}



 // construct data for table display
    $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n";
    $tData .= "<tbody>\n";
    $tData .= "  <tr><td style='{$td1Style}'><b>2013 Killer Tornado #{$yrnum}</b></td></tr>\n";
    $tData .= "  <tr>\n";
    $tData .= "    <td style='{$td2Style}'>Date: <b>{$dt}</b>&nbsp;&nbsp; - &nbsp;&nbsp;</b>Time: <b>{$updateTime}</b>&nbsp;&nbsp; - &nbsp;&nbsp;</b>EF: <b>{$ef}</b>&nbsp;&nbsp; - &nbsp;&nbsp;</             b>Fatalities: <b>{$deaths}</b>&nbsp;&nbsp; - &nbsp;&nbsp;Watch Issued: {$watchn}<b></a></td>\n";
    $tData .= "  </tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'>&nbsp;</td></tr>\n";
    $tData .= "  <tr><td style='{$td4Style}'>County: <b>{$location}</b>&nbsp;&nbsp; - &nbsp;&nbsp;State: <b>{$state}</b></td></tr>\n";
    $tData .= "</tbody>\n";
    $tData .= "</table>\n";
    $tData .=  $afterTable;

 }  

?>

<?php print $tData ?>

-Thanks

2 个答案:

答案 0 :(得分:0)

在运行期间,$ ef的值是多少。可能有助于抛出

default:
    print_r($fatalities);
    die("unhandled ef value ".$ef); 
    break;

进入switch语句,以便更轻松一点

更有可能的是,simplexml不会给你一个数组或者你不期望$ ef值的东西。同样使用simplexml,对返回的数据进行显式转换总是一个好主意。即switch ((string)$ef)

答案 1 :(得分:0)

您在创建变量之前分配变量。像这样改变你的剧本

<?php 
#######################################################################################
#
#  GOOGLE MAPS V3 KILLER TORNADOS
#  version 1.00
#
#  This program is free and no license is required.
#  
#
#  mesquiteweather.net
#
#######################################################################################

////  SETTINGS  ////

$GKey = 'ENTER_YOUR_KEY';                    // Enter your Google Maps API Key
$GImage = 'images/watermark_MW_GMap.png';     // Path to your watermark for your Google Map

// Change colors
$bc        = 'True';     // If True cell tables will show color of EF scale, set to false to use CSS style color
$dtColor   = '#FFF';     // Ttile Color  Examples:   "#FC0"   "#FFCC00"   "white"

////  END OF SETTINGS  ////

#######################################################################################

ini_set('display_errors','1');

// overrides from the Carter Lake Settings.php file (if applicable)
global $SITE;
if(isset($SITE['cacheFileDir'])) {$cacheFileDir = $SITE['cacheFileDir']; }
if (isset($SITE['imagesDir']))   {$imagesDir = $SITE['imagesDir'];}
if(isset ($SITE['tz']))          {$ourTZ = $SITE['tz'];}
if(!function_exists('date_default_timezone_set'))
{
    putenv("TZ=" . $ourTZ);
}
else
{
    date_default_timezone_set("$ourTZ");
}

// get path info & protect for cross-site scripting vulnerability
$sri = ($_SERVER['REQUEST_URI']) ? str_replace('#SA', '', htmlspecialchars(strip_tags($_SERVER['REQUEST_URI']))) : '';

//Set path to data file
$data = "http://www.spc.noaa.gov/climo/torn/xml/2013.xml";

$bbrdr = 'border-bottom:thin solid black';       // bottom
$lbrdr = 'border-left:thin solid black';         // left
$rbrdr = 'border-right:thin solid black';        // right
$tbrdr = 'border-top:thin solid black';          // top
$sbrdr = 'border-right:thin solid black; '.
         'border-left:thin solid black';         // side
$tableStyle = "width: 100%; margin:0px auto; background-color:{$bkgColor};";

//Define table to display after each storm report
$afterTable = "<table style='margin-bottom: 10px;' border='0' cellpadding='0' cellspacing='0' width='100%'><tbody><tr><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7'></td><td class='shadow-mid' width='100%'><img alt='' src='images/1pixel.gif' border='0' height='7' width='7'></td><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7'></td></tr><tbody></table>\n";


// Lets parse the XML feed
$xml = simplexml_load_file($data);

//Set initial output to false
    $tData = false;
foreach($xml->fatalities as $fatalities){

    $yrnum = $fatalities['yrnum'];
    $dt = $fatalities['dt'];
    $time = $fatalities['time'];
    $updateTime = DATE("g:i a", STRTOTIME($time));
    $tz = $fatalities['tz'];
    $ef = $fatalities['ef'];
    $location = $fatalities['location'];
    $state = $fatalities['st'];
    $watch = $fatalities['watch'];
    $watchn = str_replace("WT","WW","$watch");
    $deaths = $fatalities['deaths'];

  $stormScale = '';

       switch($ef)
    {
          case '0':
            $stormScale = 'rgba(0, 255, 255, 0.2)';
                 break;
          case '1':
            $stormScale = 'rgba(0, 0, 255, 0.1)';
                 break;
          case '2':
            $stormScale = 'rgba(0, 255, 0, 0.2)';
                 break;
          case '3':
            $stormScale = 'rgba(255, 255, 0, 0.1)';
                 break;
          case '4':
            $stormScale = 'rgba(255, 153, 51, 0.2)';
                 break;
          case '5':
            $stormScale = 'rgba(255, 0, 0, 0.2)';
                 break;

    }
$td1Style = "{$tbrdr};{$sbrdr}; padding:2px 0px 2px 6px;  background-image:url({$imagesDir}headerbgd2.gif); color:{$dtColor};";
$td2Style = "{$sbrdr}; padding:6px 0px 0px 6px; background-color:{$stormScale};";
$td3Style = "{$sbrdr}; line-height:5px; background-color:{$stormScale};";
$td4Style = "{$sbrdr}; {$bbrdr}; padding: 2px 6px 6px 6px; background-color:{$stormScale};";


 // construct data for table display
    $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n";
    $tData .= "<tbody>\n";
    $tData .= "  <tr><td style='{$td1Style}'><b>2013 Killer Tornado #{$yrnum}</b></td></tr>\n";
    $tData .= "  <tr>\n";
    $tData .= "    <td style='{$td2Style}'>Date: <b>{$dt}</b>&nbsp;&nbsp; - &nbsp;&nbsp;</b>Time: <b>{$updateTime}</b>&nbsp;&nbsp; - &nbsp;&nbsp;</b>EF: <b>{$ef}</b>&nbsp;&nbsp; - &nbsp;&nbsp;</             b>Fatalities: <b>{$deaths}</b>&nbsp;&nbsp; - &nbsp;&nbsp;Watch Issued: {$watchn}<b></a></td>\n";
    $tData .= "  </tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'>&nbsp;</td></tr>\n";
    $tData .= "  <tr><td style='{$td4Style}'>County: <b>{$location}</b>&nbsp;&nbsp; - &nbsp;&nbsp;State: <b>{$state}</b></td></tr>\n";
    $tData .= "</tbody>\n";
    $tData .= "</table>\n";
    $tData .=  $afterTable;

 }  

?>

<?php print $tData ?>