-1

I'm trying to use an if statement so that if the variable isn't set it wil display "noimg.jpg" or no thumbnail, but it isn't doing that at the moment, it's displaying a broken thumbnail.

$feed = new SimplePie_random_sort();

$feed->set_feed_url(array(

    'http://www.thelocal.de/feeds/rss.php',
    'http://dailymail.co.uk',
    'http://www.exberliner.com/',
    'http://www.telegraph.co.uk/news/worldnews/europe/germany/',
));


$feed->set_item_limit(3);
//enable caching
$feed->enable_cache(true);

//provide the caching folder
$feed->set_cache_location('cache');

//set the amount of seconds you want to cache the feed
$feed->set_cache_duration(1800);

//init the process
$feed->init();

//let simplepie handle the content type (atom, RSS...)
$feed->handle_content_type();

<?php foreach ($feed->get_items() as $item): ?>
<?php if ($enclosure = $item->get_enclosure())
    {
    echo '<img src="' . $enclosure->get_link() . '" class="feed-thumb" />';
    }
    else echo'<img src="noimg.jpg">';
    ?>
    <?php endforeach; ?>

the results from the var_dump: object(SimplePie_Enclosure)#399 (27) { ["bitrate"]=> NULL ["captions"]=> NULL ["categories"]=> NULL ["channels"]=> NULL ["copyright"]=> NULL ["credits"]=> NULL ["description"]=> NULL ["duration"]=> NULL ["expression"]=> NULL ["framerate"]=> NULL ["handler"]=> NULL ["hashes"]=> NULL ["height"]=> NULL ["javascript"]=> NULL ["keywords"]=> NULL ["lang"]=> NULL ["length"]=> NULL ["link"]=> NULL ["medium"]=> NULL ["player"]=> NULL ["ratings"]=> NULL ["restrictions"]=> array(1) { [0]=> object(SimplePie_Restriction)#220 (3) { ["relationship"]=> string(5) "allow" ["type"]=> NULL ["value"]=> string(7) "default" } } ["samplingrate"]=> NULL ["thumbnails"]=> NULL ["title"]=> NULL ["type"]=> NULL ["width"]=> NULL } I'm not sure how to amend the if statement based on this info?

tom harrison
  • 3,273
  • 11
  • 42
  • 71
  • 1
    Do you intend to assign `$item->get_enclosure()` to `$enclosure`? – baao Aug 01 '15 at 23:05
  • 1
    This should be quite easy to debug. Do a `var_dump($item->get_enclosure())` before the `if` block to see what it contains. It probably does not contain what you think it contains. – Sverri M. Olsen Aug 01 '15 at 23:06
  • I've added the full code as I mistakenly thought this would be an easy code fix. I'm using the simple pie feed to pull a thumbnail from an rss feed and want it to display a custom thumbnail if it is unable to dislay the thumb – tom harrison Aug 01 '15 at 23:22

1 Answers1

2

You have an assignment inside your if

if ($enclosure = $item->get_enclosure())

This assignment is evaluated to the value you are assigning to $enclosure. If that value is truey, then the condition evaluates to true. Otherwise, it will evaluate to false. The problem is that in some cases you expect it to evaluate as false, but it evaluates as true. The solution is to refine your conditional to evaluate the right condition and make sure you do not forget the value assignment. It is not a bad idea to put a var_dump($enclosure) inside the block represented as curly brackets and reproduce the issue. What is dumped? An object? How is it truey? Take the info you have gathered this way and fix the conditional.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • I've added the var_dump results, not sure how to amend the if statement..sorry new to php – tom harrison Aug 01 '15 at 23:55
  • Put the $enclosure = $item->get_enclosure() before the if. And then modify the if like this: if($enclosure->get_link()) since the link is null in your var_dump output and the new condition should serve the purpose you intended. – Lajos Arpad Aug 02 '15 at 00:32
  • just to check I've done this correctly as it's now displaying the broken thumbnails for all items and no images at all where it was before `get_enclosure()) ?> get_link()) { echo ''; } else echo''; ?>` – tom harrison Aug 02 '15 at 00:53
  • You have an extra ) in get_enclosure()) ?>, use get_enclosure(); ?> instead... – Lajos Arpad Aug 02 '15 at 00:56
  • Using `get_enclosure(); ?>` i get `syntax error, unexpected ';'` – tom harrison Aug 02 '15 at 09:13
  • This code will now display the thumbs however it still shows the broken thumbnails `get_enclosure()) ?> get_link()) { echo ''; } else echo''; ?>` – tom harrison Aug 02 '15 at 09:15
  • actually looks like this is working thanks alot! – tom harrison Aug 02 '15 at 09:16