SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Threaded View

  1. #1
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to use arrays inside a class

    I am trying to use a class to display an RSS feed. For this example, I am using the BBC RSS feed. The code below is supposed to display first four RSS feed entries:
    PHP Code:
    <?php
    class Rss_feed
    {
       public 
    $title = array();
       public 
    $description = array();
       
       public function 
    __construct($feed_url,$n) {
        
    $rss simplexml_load_file($feed_url);
        
    $i 0;
        foreach (
    $rss->channel->item as $feedItem) {
            
    $i++;
            
    $this->title[i] = $feedItem->title;
            
    $this->description[i] = $feedItem->description;
            if (
    $i>=$n) break;
        }
       }
    }

    $BBC_feed = new RSS_feed("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml",4);

    for (
    $i==0$i<=4$i++)

    echo 
    "<h2>" $BBC_feed->title[$i] . "</h2>" "<BR>\n";
    echo 
    $BBC_feed->description[$i] . "\n<BR><BR>";
    }
    ?>
    However, when I try to display arrays title and description by index, they come out empty in HTML. On the other hand, when parsing the RSS feed directly (via $rss = simplexml_load_file($feed_url); echo $rss->channel->item[0]->title; etc) it works fine. So, the problem is in the way I am handling arrays inside a class, I presume. Could you please point out what is wrong with my code? Thank you.
    Last edited by SpacePhoenix; Nov 5, 2013 at 01:04. Reason: placed php tags around php code


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •