回应数组密钥..怎么样?

时间:2012-04-27 20:21:47

标签: php codeigniter simplepie

我希望将数组键的值回显到此控制器的视图中。我无法弄明白。因此,对于第一个Feed,我想将mmafighting回显到视图中。然后它将遍历其余部分。

这是我的控制器代码:

function index()
{
$this->load->library('simplepie');
       $feeds = array('mmafighting' => 'http://www.mmafighting.com/rss/current',
                    'bbc' => 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml',
                    'bloodyelbow' => 'http://www.bloodyelbow.com/rss',
                    'ufc' => 'http://www.ufc.com/rss/news',
                    'hackernews' => 'http://news.ycombinator.com/rss',
                    'msnbc' => 'http://www.msn.com/rss/news.aspx',
                    'msnbc2' => 'http://www.msn.com/rss/msnmoney.aspx',
                    'msnbc3' => 'http://www.msn.com/rss/msnshopping_top.aspx'

                 );
    foreach ($feeds as $site=>$url)
    {
        $data['feed'][$site] = new SimplePie();
        $data['feed'][$site]->set_feed_url($url);
        $data['feed'][$site]->set_cache_location(APPPATH.'cache');
        $data['feed'][$site]->set_cache_duration(300);
        $data['feed'][$site]->init();
        $data['feed'][$site]->handle_content_type();
    }
    $this->load->view('feedsview', $data);
}

这是我的观看代码:

<?php
$feedCount = 0; 
$rowCount = 0;
?>
<?php foreach ($feed as $site): ?>

    <?php if ($site->data): ?>
            <div class="feed">
            <h2><?php // echo original array key here ?></h2>
            <ul>
            <?php $items = $site->get_items(0, 5); ?>
            <?php foreach ($items as $item): ?>
                <li><a href="<?php echo $item->get_link(); ?>"><?php echo $item->get_title(); ?></a></li>
            <?php endforeach; ?>
        </ul>
        </div>
        <?php endif; ?>



<?php $feedCount++; ?>
<?php if ($feedCount === 3) {
    echo '<div style="clear:both;">&nbsp;</div>';
    $feedCount = 0;
    $rowCount++;
        if ($rowCount === 2) {
            echo '<div style="width:1000px; margin-bottom:20px;height:100px; border:1px solid #252525; float:left;">images</div>';
            }
    } ?>
    <?php endforeach; ?>

1 个答案:

答案 0 :(得分:0)

我不确定我理解但是这样做:

<?php foreach ($feed as $key => $site): ?>

    <?php if ($site->data): ?>
            <div class="feed">
            <h2><?php echo $key ?></h2>
            <ul>