PHP rss提要|我希望一次只显示四条公告

PHP rss提要|我希望一次只显示四条公告,php,rss,Php,Rss,以下是我目前的代码: <?php $feed_url = '<link removed>'; $xml_data = simplexml_load_file($feed_url); $i=0; foreach($xml_data->channel->item as $r

以下是我目前的代码:

<?php 
                        $feed_url = '<link removed>'; 
                        $xml_data = simplexml_load_file($feed_url);

                        $i=0; 
                        foreach($xml_data->channel->item as $ritem) { 

                        $e_title       = (string)$ritem->title; 
                        $e_link        = (string)$ritem->link; 
                        $e_pubDate     = (string)$ritem->pubDate; 
                        $e_author      = (string)$ritem->author;
                        $e_content     = $ritem->children("content", true);
                        $e_encoded     = (string)$e_content->encoded; 
                        $description   = substr($e_encoded, 0, 350).'<a style="font-weight: bold;" href="' . $e_link . '"> ...Read More</a>';

                        $n = ($i+1);
                        print "\n"; 
                        print '<h5 class="font-accident-one-bold hovercolor uppercase" style="margin-bottom: 5px;"><a href="' . $e_link . '">'. $e_title .'</a></h2>'."\n"; 
                        print '<p class="date" style="margin: 0px;">'. str_replace('+0000', '', $e_pubDate) .'</p>'."\n"; 
                        print '<p style="margin: 0px;"> <strong>By '. str_replace('invalid@example.com', '', $e_author) .'</strong></p>'."\n"; 
                        print '<p class="small" style="margin-top: 5px; padding-bottom: 5px;">'. $description .'</p>'."\n"; 

                        $i++; 
                        }
以避免中断

$counter = 4;

for(x=0; x= $counter; x++)
{

//do something
//make sure you dont pull same data four times. But try to fetch data one by one. So this loop works for you as this loop makes four rounds
}
或者使用continue

比较您拥有的以下数据:

 if($product_category>4) continue;

它将跳过对循环的控制并继续进行

好吗?然后按最新的数组排序,或者在运行4次后将
foreach
更改为break/stop,或者将其更改为
for
循环。我对PHP非常在行,因此如果您能为我提供一些示例代码,我将不胜感激。不,
foreach
for
循环也是非常基本的,这只是一个计数到4然后停止循环的问题,尝试一下。不要使用返回到结束循环,不幸的是,添加后使用,因为我的页面是如何设置的。添加中断会使我的预加载程序“中断”,它不会停止显示我的gif图像。确定尝试使用for循环而不是foreach。所以你不再需要休息了。同样在for循环中保持$i的计数。我将更新解决方案无论是谁删掉了我的问题,谢谢你让我再等两天来问一个问题,当我在这里尝试学习的时候。