douxun3496 2014-02-14 16:11
浏览 59
已采纳

警告:为foreach()PHP DOM提供的参数无效

I am trying to get the <td> from a specific row, but I always encounter the Warning: Invalid argument supplied for foreach() error.

MY CODE:

$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
}

Any help would be gladly appreciated. thank you!!!

  • 写回答

1条回答 默认 最新

  • dongnaoxia0927 2014-02-14 16:27
    关注

    You should be using

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

    $moths is not an array of <tr>s, but $rows is

    If you're wanting the first row only then just use

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

    This will return the whole node and allow you to use it, where as nodeValue just returns a string containing the value

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看