duannuan0074 2015-04-24 22:55
浏览 94
已采纳

我如何限制每页php中显示的文章数量(从RSS中检索)? (例如每页10篇文章)

Here is the code : I retrieve data from an RSS and i show them in a php page. These data are items (Articles) which contains title, date, description ... Now i want to show just 10 articles per page. Please how can i do that. Thank you

 <?php 


    $fichier = 'http://korben.info/feed';
    $dom = new DOMDocument();
    $slashNS = "http://purl.org/rss/1.0/modules/slash/";


    if (!$dom->load($fichier)) {
        die('Impossible to load the XML');
    }

    $itemList = $dom->getElementsByTagName('item');


    foreach ($itemList as $item) {
        $titre = $item->getElementsByTagName('title');
        $date = $item->getElementsByTagName('pubDate');

            if ($titre->length > 0) {
                 echo $titre->item(0)->nodeValue;
              } else {
                 echo '(sans titre)';
        }

         echo date (" d/m/Y - H:i", strtotime($date->item(0)->nodeValue))."\t";
             echo '<br />' . 'Nmber of comments : ' . $item->getElementsByTagNameNS($slashNS, 'comments')->item(0)->nodeValue;
            echo '<br />'."
";

            $desc = $item->getElementsByTagName('description');
            if ($desc->length > 0) {
             echo ' '.$desc->item(0)->nodeValue."
";
            }

            $lien = $item->getElementsByTagName('link');
            if ($lien->length >0) {
                echo ' <a href="'.$lien->item(0)->nodeValue.'">Article complet</a>';
         }


         echo '<br/>'."
".'<br/>'."
".'<br/>'."
";   

    }
    ?>
  • 写回答

1条回答 默认 最新

  • duanbinian2243 2015-04-24 22:57
    关注

    One thing you can do is break the foreach loop when a counter reaches 10.

    $i = 0;
    foreach ($itemList as $item) {
        $i++;
        // ...
        if($i>9){ break; }
    }
    

    Or, restructure into a for() loop, and surround ALL the code inside with an if() statement similar to above.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料