2013-05-09 65 views
0

我有这样的代码,它从XML文件中获取其他变量的值。这目前工作之前,我清理了代码,现在我不知道为什么它不工作。它如何工作是接受变量并基于该变量的值创建颜色。然后,我在这种情况下采用新的变量$ stormScale并将其分配给CSS类,以便像这样显示该单元格的颜色。PHP开关没有输出。

$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

+1

为什么不只是使用类和风格的CSS _in CSS_? – elclanrs 2013-05-09 04:51:15

+1

您在表格显示中使用'$ stormScale'的位置?我找不到你在这里粘贴的代码。 – Roopendra 2013-05-09 04:56:36

+0

@elclanrs 1.单元格颜色不是静态的,它们是根据分配给表的数据值动态创建的。 2.这是我正在开发的一个脚本,它是一个将与其他人共享的模板集的附加内容。所以它需要非常简单的即插即用,而且不需要很多设置。上传并去。 3.使用内联样式可以实现这一点,因为样式不会是全局的,只会为CSS添加不必要的样式。 – Texan78 2013-05-09 05:29:17

回答

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 ?> 
+1

谢谢你的完美! – Texan78 2013-05-09 05:18:36

0

在运行过程中什么是$ EF值。可能有助于扔

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

到switch语句摆脱多一点光

更可能的则不是simplexml的是给你的,你不希望为$ EF值的数组或东西。另外对于simplexml,对返回的数据进行显式转换总是一个好主意。即switch ((string)$ef)