-1

I'm currently designing a page, with 20 boxes, where each box represents an RSS feed, using simplepie as my parser.

Currently the script calls the first image from each feed, discards the rest and sets that image as the box's background-image.

With 20 feeds, I want to keep the code small and efficient as possible. Though I understand the separate setting of $feed01, $feed02 is a necessary messy evil, that's required for simplepie to work.

What I can't understand is why imageurl() returns a null value.

Can anyone educate me on why this could be?

Many Thanks

<?php
require_once('isnotabear/php/autoloader.php');

$feed01 = new SimplePie();
$feed01 -> set_feed_url ('http://link/to/rss01');
$success = $feed01 -> init();
$feed01 -> handle_content_type();
$max = $feed01 -> get_item_quantity(); // Where do we end?

$feed02 = new SimplePie();
$feed02 -> set_feed_url ('http://link/to/rss02');
$success = $feed02 -> init();
$feed02 -> handle_content_type();
$max = $feed02 -> get_item_quantity(); // Where do we end?

$start = (isset($_GET['start']) && !empty($_GET['start'])) ? $_GET['start'] : 0; // Where do we start?
$length = (isset($_GET['length']) && !empty($_GET['length'])) ? $_GET['length'] : 1; // How many per page?


function imageurl()
{
    if ($success): foreach($feedin->get_items($start, $length) as $item): $feedin =     $item-> get_feed();
    {
        $replace = preg_match( '@src="([^"]+)"@' , $item->get_content(), $match );
        $match = preg_replace ('"src="', '', $match);
        $match = str_replace('"', "'", $match);
        echo $match[0];
    }
    endforeach;
    endif;
};

?>
<!DOCTYPE HTML>
... YADA YADA YADA ...

<a href="#" style="background-image:url(<?php $feedin = $feed01; imageurl(); ?>)"><div> <span>FEED NAME</span></div></a>

2 Answers2

1

$success isn't defined inside imageurl(), so it'll always be null/false

you probably want:

function imageurl() {
   global $success;
   if ($success) { .... }
}

But don't use globals like that. Pass $success in as a parameter. It'll be far cleaner.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

This is part-way between a comment and an answer, but I wanted the space to show the code. Your function uses a really awkward mix of PHP syntax and some entirely misleading punctuation:

  • the if(): ... endif; and foreach(): ... endforeach; syntax is extremely old-fashioned, and generally used only when using PHP as a template language (i.e. in the middle of HTML) rather than in a standalone function like this
  • the {, }, and indenting relate to neither the if nor the foreach; PHP will ignore them, but they'll confuse anyone reading your code
  • the ; at the end is also ignored - a function ends at its }

So your code should actually look like this (note I've also added $success and $feedin as parameters which you should pass in to the function):

function imageurl($success, $feedin)
{
    if ($success)
    {
        foreach($feedin->get_items($start, $length) as $item)
        {
            $feedin = $item->get_feed();
            $replace = preg_match( '@src="([^"]+)"@' , $item->get_content(), $match );
            $match = preg_replace ('"src="', '', $match);
            $match = str_replace('"', "'", $match);
            echo $match[0];
        }
    }
}

Oh, and you probably want to return $match[0] rather than echo it. Otherwise, it's still going to return null whatever you pass in.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Super helpful,thank you so much for explaining, I can normally understand other languages with a semi-comprehension but I just PHP an illogical mess. Again, thank you very much! – BarclayMontana Mar 13 '13 at 23:10