1
        <li id="weather" class="widget-container widget-dark-blue">
            <h3 class="widget-title">Weather</h3>
            <?php include (TEMPLATEPATH . '/widgets/weather.php'); ?>
        </li>

weather.php 向天气服务发出 curl 请求并返回一个表格。使用 DomDocument 类,我可以读取td's. 我正在将当前天气状况的类名应用于div.weather.

<?php

require_once('classes/SmartDOMDocument.class.php');

$url = 'some/domain...';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);

$str = curl_exec($curl);

$dom = new SmartDOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);

$tds = $xpath->query('//div/table/tr/td');

foreach ($tds as $key => $cell) {
    if ($key==1) {
        $condition = $cell->textContent;
        //$cell->parentNode->setAttribute('class', 'hello');
        echo "<div class='weather " . strtolower($condition) ."'>";
        ...

?>

一切正常。我唯一的问题是,是否有一种 PHP 方法可以将类名 $condition 应用于保存信息的列表项?因此,我不想在我的 li#weather 中有一个带有 $condtion 的类,我希望有一个 li#weather 类。

<li id="weather" class="widget-container widget-dark-blue $condition">

有什么方法可以将 $condition 类应用于包含所有内容的列表。我可以很容易地用 javascript/jquery 做到这一点。但是我想知道是否有一些服务器端解决方案。

谢谢你

4

1 回答 1

2

也许你可以尝试类似的东西:

$parent = $cell->parentNode;

while ($parent->tagName != 'li')
{
    $parent = $parent->parentNode;
}

$class = $parent->getAttribute('class');
$parent->setAttribute('class', $class . ' ' . strtolower($condition));
于 2011-03-24T22:14:13.107 回答