1

I am trying to include a php parser that will retrieve a rss 2.0 file:

<?php

  $doc = new DOMDocument();
  $doc->load('http://www.domain.com/blog/feed/');
  $arrFeeds = array();
  foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array ( 
      'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
      'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
      'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
      'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
      );
    array_push($arrFeeds, $itemRSS);
  }

//And trying to parse the return into a template:

$view_doc->assign("DOCID", $arrFeeds);

?>

But the output given is simply "Array" :(

I hope that you can give me some hints guys :)

Kind Regards!

1
  • Have you considered using the open project SimplePie? I have used it in the past for RSS processing and its very good! Commented May 14, 2011 at 0:05

4 Answers 4

2

Remove $docidtrue = return $arrFeeds; and replace $docidtrue with $arrFeeds. It should work as you wanted.

It would look like that:

<?php

$doc = new DOMDocument();
$doc->load('http://www.domain.com/blog/feed/');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    );
    array_push($arrFeeds, $itemRSS);
}

//And trying to parse the return into a template:

$view_doc->assign("DOCID", $arrFeeds);

?>

return is not what you wanted. Maybe you looked for break (which exits the whole current loop) or continue (which leaves current loop iteration and goes to the next one).

EDIT:

Because you need some string to be passed to assign() method, we can make something that will display the list of items instead of gathering them for further processing :)

<?php

$doc = new DOMDocument();
$doc->load('http://www.domain.com/blog/feed/');
$feeds = '';
foreach ($doc->getElementsByTagName('item') as $node) {
    $feeds .= '<div>'
        .'<h2>'.$node->getElementsByTagName('title')->item(0)->nodeValue.'</h2>' // title
        .'<p>'.$node->getElementsByTagName('description')->item(0)->nodeValue.'</p>' // desc
        .'<p><a href="'.$node->getElementsByTagName('link')->item(0)->nodeValue.'">link</a></p>' // link
        .'<p>'.$node->getElementsByTagName('pubDate')->item(0)->nodeValue.'</p>' // date
        .'</div>';
}

//And trying to parse the return into a template:

$view_doc->assign("DOCID", $feeds);

?>

This has to work :)

But remember - what I just did is just wrong. You should parse the variable resulting from the original solution within the template. I have just created it in the wrong place and passed generated string into the view. View is just for transforming data into displayable code (HTML).

Sign up to request clarification or add additional context in comments.

11 Comments

Some details about return are here: php.net/manual/en/function.return.php
@Tadeck tried that before and the output simply appears like "array" :( Thanks.
That is because what you do in $view_doc->assign(). If you could provide its source or at least say what do you expect in its second parameter, that would be helpful. From what you say I can simply assume that $view_doc->assign("DOCID", $some_option) tries to display $some_option as a string and it is evaluated to "array".
Tell: 1) what is $view_doc?, 2) what needs to be passed to $view_doc->assign() as second parameter.
I am sorry for not making referenge to the engine behind the scene, is xTemplate Makes scense what you said, i'll see what kind of information i can parse
|
2
$docidtrue = return $arrFeeds;

Correct, return just returns a value to the caller of the function. You can't assign it to anything because it leaves the function right away.

I understand that, but i tried every possible ways, even like Tadeck suggested and the output is simply "Array"

$view_doc->assign("DOCID", $docidtrue);

What is $view_doc? Since it's calling assign, I'm going to assume it's some templating engine, most likely smarty, and if so what's the template that prints the results look like?

4 Comments

I understand that, but i tried every possible ways, even like Tadeck suggested and the output is simply "Array"
@Souza See the request for clarifications inline.
@Tadeck Well as noted in the comment above, Souza is getting "Array" output, but there's no echo. This most likely means the array is not getting looped over properly in the template, which is why I'm asking for the template code
Oh, we just both asked for the same thing, but you were first by 2 minutes :) Yes, I know it would be helpful - the best thing that should be done is to change the code displaying it in the view, but because of the lack what we need to change I proposed temporary solution with generating the string and passing it instead of array. I know it breaks good practices, but Souza is learning :) and he needs something to play with - if he gives the code of the template, we would be able to help him more.
0

you can't write: $docidtrue = return $arrFeeds; especially in a foreach that means it's just gonna break the first time... moreover you can't do an affectation from a return

Comments

0

the problem here kind of looks like the "$docidtrue = return $arrFeeds;" code based on the "unexpected T_RETURN" message. I'd say that you should probably remove the "$docid ="

Comments

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.