开发者

php integer not responding to conditional

开发者 https://www.devze.com 2023-03-30 16:52 出处:网络
Answer /* Thanks everyone :) my guess: the while loop is running through every line of HTML code that has a value (class=\"\" type=\"\" id=\"\" etc. – many of them are blank), but I only

Answer

/* Thanks everyone :)
my guess: the while loop is running through every line of HTML code that 
has a value (class="" type="" id="" etc. – many of them are blank), but I only 
need it to count "1" if it has run through the switch up to case 5, at which 
point it grabs the last bit of the weather for a particular day.  On case 5 is 
when I need to trigger $weatherIterations-- */
$i = 0;
$weatherIterations = 3;

$htmlWeather = "";
if (empty($htmlText)) {
$htmlWeather = "<p>Weather information is not currently available.<p>";
} else {

 $parser = new HtmlParser($htmlText);

while ( $parser->parse() && ($weatherIterations > 0) ) {
    if( trim($parser->iNodeValue) != '' ) { 
        // hey, no laughing at the newbie
            switch ($i) {
                case 0:
                    $htmlWeather .= "<div id='weather'><span class='weather-title'>".$parser-&g开发者_运维问答t;iNodeValue."</span><br />";
                    break;
                case 1:
                    $htmlWeather .= "<span class='weather-hi'>".$parser->iNodeValue."<br />";
                    break;
                case 2:
                    $htmlWeather .= $parser->iNodeValue."</span><br />";
                    break;
                case 3:
                    $htmlWeather .= "<span class='weather-lo'>".$parser->iNodeValue."<br />";
                    break;
                case 4:
                    $htmlWeather .= $parser->iNodeValue."</span><br />";
                    break;
                case 5:
                    $htmlWeather .= "<span class='weather-desc'>".$parser->iNodeValue."</span></div>";
                    $weatherIterations--;
                    break;
                default:
                    $htmlWeather = "Oops! Houston, we have a problem.";
                    print $htmlWeather;
                    exit;
                } // switch
            $i<5 ? $i++: $i=0;
        } // if trim
} // while
print $htmlWeather;
} // if empty html

This is baffling me. None of the conditionals inside the while() loop execute. If I move $weatherIterations > 0 out of the while loop, and place it in a conditional of its own, the variable goes 2,1,0,-1,-2...-150 and is not registered as an integer by the conditional if($weatherIterations<=0){}. Please advice.

Update

If I remove "&& ($weatherIterations > 0)" from the while loop, and echo $weatherIterations before decrementing it, it results in

3
2
1
0
-1
...
-150
title= Wednesday
Hi= Hi
Temp= 29 / 85 
Lo= Lo
Temp= 24 / 76 
Desc= Sunny Periods, showers
title= Thursday
 ...
 Desc= Sunny Periods

So it's making a bunch of while loops before it executes any of the IF() conditionals. Ideas?

Question

$i = 0;
$weatherIterations = 3;
$htmlWeather = "";

   ...

while ( $parser->parse() && ($weatherIterations > 0) ) {
$weatherIterations--;
    if ($parser->iNodeType == NODE_TYPE_TEXT || $parser->iNodeType == NODE_TYPE_COMMENT) {
    if( trim($parser->iNodeValue) != '' ) { 
        // hey, no laughing at the newbie
            switch ($i) {
                case 0:
                    $htmlWeather .= "<div id='weather'><span class='weather-title'>title= ".$parser->iNodeValue."</span><br>";
                    break;
                case 1:
                    $htmlWeather .= "<span class='weather-hi'>Hi= ".$parser->iNodeValue."<br>";
                    break;
                case 2:
                    $htmlWeather .= "Temp= ".$parser->iNodeValue."</span><br>";
                    break;
                case 3:
                    $htmlWeather .= "<span class='weather-lo'>Lo= ".$parser->iNodeValue."<br>";
                    break;
                case 4:
                    $htmlWeather .= "Temp= ".$parser->iNodeValue."</span><br>";
                    break;
                case 5:
                    $htmlWeather .= "<span class='weather-desc'>Desc= ".$parser->iNodeValue."</span></div>";
                    break;
                } // switch
            $i<5 ? $i++: $i=0;

        } // if trim
        } // if parser
} // while
print $htmlWeather;
0

精彩评论

暂无评论...
验证码 换一张
取 消