douquanzhan0315 2014-10-07 15:42
浏览 28
已采纳

RSS到HTML,具有不同数量的元素

I've adapted the code found here http://www.w3schools.com/php/php_ajax_rss_reader.asp to turn XML into HTML, and it works fine.

But what I'm stuck on is getting it to show all the items in a feed when the feed can have varying numbers of items. The feed is published daily and can have anywhere from 12-20 articles in it, and I want to show all of them.

In the For Loop for ($i=0; $i<=12; $i++) if I set the condition to be greater than the number of articles, I get an error PHP Fatal error: Call to a member function getElementsByTagName(), so I can't just set it to a big number.

I get the same error if I just remove the condition.

I can't figure out how to count the number of items, either; if I could do that the solution would be easy.

The feed is created in-house so I could ask my colleague to insert the number of items in the feed; is that the best way to go about it?

Thanks!

  • 写回答

1条回答 默认 最新

  • duanfu4446 2014-10-07 21:25
    关注

    If you don't know the number of items in the feed, you can go through them all using a foreach loop. Here is an example using the RSS feed from the PHP tag on StackOverflow. Have a look at the rss format so you can see what each entry looks like, and compare it to the code below.

    # start off like the w3schools code...
    $xml=("https://stackoverflow.com/feeds/tag?tagnames=php&sort=newest");
    
    $xmlDoc = new DOMDocument();
    $xmlDoc->load($xml);
    
    # StackOverflow uses the <entry> element for each separate item.
    # find all the "entry" items. This returns an array of matching entry elements
    $items = $xmlDoc->getElementsByTagName('entry');
    
    # go through the array of "entry" elements one at a time
    # $items is the array of <entry> elements
    # $i is set to each <entry> in turn, starting from the first one on the page
    foreach ($items as $i) {
    
        # some sample code to get the title, tags, and link
        $title = $i->getElementsByTagName('title')->item(0)->nodeValue;
        $href = $i->getElementsByTagName('link')->item(0)->getAttribute('href');
        $tags = $i->getElementsByTagName('category');
        $tag_arr = [];
        foreach ($tags as $t) {
            $tag_arr[] = $t->getAttribute('term');
        }
        echo "Title: $item_title; tags: " . implode(", ", $tag_arr) . ";
    href: $href
    
    ";
    }
    

    Using a foreach loop means you are not stuck with having to work out how many items you have in your array, and you don't have to set up an array iterator using for ($i = 0; $i < 500; $i++).

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

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路