tags:

views:

97

answers:

5

Hi,

I have a slight problem with the echo statement outputting wrongly. Forexample when i do

echo "<div id=\"twitarea\">" . fetchtwitter($rss) . "</div>";

it displays function output but OUTSIDE of twitarea div. What is the cause of this behavior perhaps syntax?

thanks in advance

Here is the actual function

require_once('includes/magpie/rss_fetch.inc');
$rssaldelo = fetch_rss('http://twitter.com/statuses/user_timeline/12341234.rss');
function fetchtwitter($rsskey){
foreach ($rsskey->items as $item) {
    $href = $item['link'];
    $title = $item['title'];
    print "<li class=\"softtwit\"><a href=" . $href . "    target=\"_blank\">$title</a></li><br>";
    } }
+1  A: 

fetchtwitter() probably does an echo() of its own, instead of returning the string. The function is executed while echo prepares the whole string for output, before the string is printed.

Pekka
yes it does actually. It displays the rss feeds via echo perhaps i should use print inside of fetchtwitter?
@user check Andy's answer.
Pekka
Or use a return instead.
Abs
+1  A: 

Does fetchtwitter(...) write the output directly to the browser instead of returning it? Try something like:

<?php

ob_start();
fetchtwitter($rss);
$twitter = ob_get_clean();

echo "<div id=\"twitarea\">" . $twitter . "</div>";

?>

Or if you can modify the source of fetchtwitter(), get it to concatenate and return the string instead of echoing it.

Andy Shellam
Why using output buffering ? Useless if you want to echo it.
Matthieu
Because the OP wants to echo it within the <div> tags, but the fetchtwitter() function is obviously using echo internally. It wasn't clear when I wrote the answer if the OP had access to the source code for the fetchtwitter() function. There are plenty of reasons to use output buffering even if you want to echo it. Hence the ob_get_clean which returns the output so you can echo it **when you want**.
Andy Shellam
What about my answer then ? I think I'm not following you.
Matthieu
Yeah that would work too, but ideally the OP would just modify the fetchtwitter() function to return the string - IMO it's not a flexible solution to have any function writing directly to the output stream, especially when that function is called "fetch..." or "get..." which implies that it's going to give you something back.
Andy Shellam
+3  A: 

Simply :

<?php
echo "<div id=\"twitarea\">";
fetchtwitter($rss);
echo "</div>";
?>

fetchtwitter($rss) is echoing output (it doesn't return it).

With that you don't have to modify fetchtwitter().

Matthieu
As in another comment, I'd advise modifying fetchtwitter because with a name like "fetch..." or "get..." it implies you're going to get something back.
Andy Shellam
A: 

you could try delimiters maybe it helps

$twitter = fetchtwitter($rss);
ob_start();
echo <<<HTML;
<div id="twitarea">$twitter</div>
HTML;
echo ob_get_clean();

update You can modify your function like this too

require_once('includes/magpie/rss_fetch.inc');
$rssaldelo = fetch_rss('http://twitter.com/statuses/user_timeline/12341234.rss');
function fetchtwitter($rsskey){ 
  $bfr =""; 
  foreach ($rsskey->items as $item){
    $href = $item['link'];
    $title = $item['title'];
    $bfr .= "<li class=\"softtwit\"><a href=" . $href . "> target=\"_blank\">$title</a></li><br>";
  } 
  return $bfr; 
}
markcial
Too much complicated, you don't need that.
Matthieu
A: 

In case you didn't see my comment.

Try using a return in your fetchtwitter() function rather than the echo that you have in there.

Abs