0

Consider having 4 different RSS news feeds URL's

$urls[0]['news'] = "url1";
$urls[1]['news'] = "url2";
$urls[2]['news'] = "url3";
$urls[3]['news'] = "url4";

The following function should get 4 news titles from each of the url's

function getRssFeeds($urls,$type){
        //Prepare XML objects
        $xmls[$type] = array();
        //Prepare item objects
        $items[$type] = array();
        //Prepare news titles/links arrays
        $article_titles[$type] = array();
        $encoded_titles[$type] = array();
        $article_links[$type] = array();
            //Fill XML objects
        for($i=0; $i<4; $i++){
            $xmls[$i][$type] = simplexml_load_file($urls[$i][$type]);
            //Prepare news items
            $items[$i][$type] = $xmls[$i][$type]->channel->item;
            for($i=0; $i<4; $i++){
                $article_titles[$i][$type] = $items[$i][$type]->title;
                $encoded_titles[$i][$type] = iconv("UTF-8","windows-1251",$article_titles[$i][$type]);
            }
            //$article_links[$type][$i] = $items[$type][$i]->link;
        }
        return $encoded_titles;
    }

After using the following to get the values:

 type='';
    function printRssFeed($urls,$type){
            $titles = getRssFeeds($urls,$type);
            foreach($titles as $title)
            {
                echo $title[$type]."<hr/>";
            }
        }

I get undefined offset error. If I remove the inner for loop of the getRssFeeds() function I get only 1 new title from each URL.

6
  • 3
    Why don't your write one function that works per feed and then just iterate over you data? That's what iterations are for: Do the same as for one but for multiple. Commented Oct 11, 2011 at 14:32
  • because I want to get combined data from multiple feeds Commented Oct 11, 2011 at 14:35
  • either just combine the data later on, or just iterate over your data again and display it. It's combined in the display then. Right now you're introducing too much logic into your function and that leads to errors because everything looks too complicated for you. Keep things simple, divide the tasks, that reduces possibilities of errors and will get you better code. Commented Oct 11, 2011 at 14:37
  • the op resets his $i variable to 0 in the inner FOR loop Commented Oct 11, 2011 at 14:37
  • @George Use a different variable in your inner for loop Commented Oct 11, 2011 at 14:43

2 Answers 2

2

in this code you are resetting $i to 0 in your inner for loop.

 for($i=0; $i<4; $i++){
        $xmls[$i][$type] = simplexml_load_file($urls[$i][$type]);
        //Prepare news items
        $items[$i][$type] = $xmls[$i][$type]->channel->item;
        for($i=0; $i<4; $i++){
            $article_titles[$i][$type] = $items[$i][$type]->title;
            $encoded_titles[$i][$type] = iconv("UTF-8","windows-1251",$article_titles[$i][$type]);
        }
        //$article_links[$type][$i] = $items[$type][$i]->link;
    }

try changing your inner for loop variable to a different one. Also when you define your arrays it seems that you are not following the same structure.

$xmls[$i][$type] does not = your original instantiation of $xmls[$type] = array(); this is true for all your other arrays.

so I think your array structure is off because you add a top level of $type and then when you iterate you use a $i as you top level key.

try to remove the instantiations of the arrays in the beginning

function getRssFeeds($urls,$type){
    //Fill XML objects
    for($i=0; $i<4; $i++){
        $xmls[$i][$type] = simplexml_load_file($urls[$i][$type]);
        //Prepare news items
        $items[$i][$type] = $xmls[$i][$type]->channel->item;
        for($i=0; $i<4; $i++){
            $article_titles[$i][$type] = $items[$i][$type]->title;
            $encoded_titles[$i][$type] = iconv("UTF-8","windows-1251",$article_titles[$i][$type]);
        }
        //$article_links[$type][$i] = $items[$type][$i]->link;
    }
    return $encoded_titles;
}
6
  • And how should I repair it. If I knew I wouldn't ask Commented Oct 11, 2011 at 14:55
  • 1
    @george im thinking sorry for the wait Commented Oct 11, 2011 at 14:56
  • @george try and get rid of all the initial instantiations. And if that doesnt fix it it may then be a structural iteration problem with your for looping Commented Oct 11, 2011 at 14:57
  • could it be that you are not setting your $type = 'news'; before you printRSSFeeds??? Commented Oct 11, 2011 at 15:02
  • Maybe. Later I call the function with $type = 'sport' and $type = 'gossip' Commented Oct 11, 2011 at 15:04
0

try this in your inner for loop

$j = 0;  
  for($j=0; $j<4; $j++){  
                $article_titles[$j][$type] = $items[$i][$type]->title;  
                $encoded_titles[$j][$type] = iconv("UTF-8","windows-1251",$article_titles[$j][$type]);  
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.