2014-02-14 75 views
-2

我想从特定行中获取<td>,但我总是遇到Warning: Invalid argument supplied for foreach()错误。警告:为foreach()提供的无效参数PHP DOM

我的代码:

$dom = new domDocument; 
@$dom->loadHTML($notes); #$notes - value of textarea 
$dom->preserveWhiteSpace = false; 
$rows = $dom->getElementsByTagName('tr'); 
$moths = $rows->item(0)->nodeValue; #first row 
$mothlast = $rows->item($rows->length - 1)->nodeValue; #lastrow 

foreach ($moths as $moth) 
{ 
    $cols_mother = $moth->getElementsByTagName('td'); trying to get the columns of the first row of table 
    $rli_mother = strip_tags($cols_mother->item(0)->nodeValue); 

    echo $rli_mother; #first row 
} 

任何帮助将欣然赞赏。谢谢!!!

+0

确保'$ moths'实际上有一个值,并且在尝试循环它之前是一个数组 –

+0

是的它有一个值。它实际上是我桌子的第一排。 – user3310979

+2

你确定吗? PHP似乎并不这么认为。 –

回答

0

您应该使用

foreach($rows as $row){ 
    $td = .... 

$飞蛾不是<tr>秒的数组,但$行是

如果你想在第一行才刚刚用

$first = $rows->item(0); 

这将返回整个节点并允许您使用它,其中nodeValue只是返回一个包含值的字符串

+0

那么我将如何获得仅第一行的列? – user3310979

+0

我只想要第一行的第一列..我该如何得到它? – user3310979

+0

把我给你的东西和尝试。并阅读这个http://www.php.net/manual/en/class.domdocument.php – ElefantPhace

相关问题