0

How can fix error Message: Invalid argument supplied for foreach() - Line Number: 71 in following foreach?

The error is:

Warning: Invalid argument supplied for foreach() in /home/u712773673/public_html/components/com_cedphotofeed/parser/simplepie.php on line 71

the line 71 is:

71.                    foreach ($thumbnails as $thumbnail) {
72.                        $modelItem['thumbnails'][] = $thumbnail;
73.                    }

<?php
/**
* @version      2.6.3
* @package      cedPhotoFeed
* @copyright    Copyright (C) 2009-2013 Cedric Walter. All rights reserved.
* @copyright    www.cedricwalter.com / www.waltercedric.com
*
* @license        GNU/GPL v3.0, see LICENSE.php
*
* cedPhotoFeed is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// no direct access
defined('_JEXEC') or die('Restricted access');

require_once(JPATH_SITE . '/libraries/simplepie/simplepie.php');

class PhotoFeedSimplePieParser
{

    var $simplepie = null;

    function PhotoFeedSimplePieParser($config = array())
    {
    }

    public function getItems($params, $feedUrl, $startAtPhoto, $limit)
    {
        //Init SimplePie
        $this->simplepie = new SimplePie($feedUrl, null, $params->get('RssCachetime', 3600));

        // set sorting before parsing feed
        // Reorder feed by date descending
        $this->simplepie->enable_order_by_date(false);

        //do the parsing
        $this->simplepie->handle_content_type();
        //$feedEntries = $this->simplepie->get_item_quantity();

        $imageCount = ($startAtPhoto + $limit);
        if ($startAtPhoto == "") {
            $startAtPhoto = 0;
        }
        //get_items($startAtPhoto, $imageCount) cant be used
        $items = $this->simplepie->get_items($startAtPhoto, $imageCount);

        $model = array();
        foreach ($items as $item) {
            if ($enclosure = $item->get_enclosure()) {
                $modelItem = array();
                $modelItem['title'] = htmlspecialchars(strip_tags($item->get_title()));
                $modelItem['description'] = htmlspecialchars(strip_tags($item->get_description()));
                $modelItem['mediaContentUrl'] = $enclosure->get_link();

                $media_group = $item->get_item_tags('http://search.yahoo.com/mrss/', 'group');
                $media_content = $media_group[0]['child']['http://search.yahoo.com/mrss/']['content'];
                $attributes = $media_content[0]['attribs'][''];

                $modelItem['width'] = $attributes['width'];
                $modelItem['height'] = $attributes['height'];
                $modelItem['imageUrl'] = $attributes['url'];

                $modelItem['thumbnails'] = array();
                $modelItem['links'] = array();
                foreach ($item->get_enclosures() as $enclosure) {
                    $thumbnails = $enclosure->get_thumbnails();
                    $modelItem['links'][] = $enclosure->get_link();
                    foreach ($thumbnails as $thumbnail) {
                        $modelItem['thumbnails'][] = $thumbnail;
                    }
var_dump($thumbnails);
                }

                $model[] = $modelItem;
            }

        }


        return $model;
    }

    public function getGalleryLink()
    {
        return $this->simplepie->get_link();
    }

    public function getGalleryTitle()
    {
        return $this->simplepie->get_title();
    }

    public function getGalleryDescription()
    {
        return $this->simplepie->get_description();
    }

}
1
  • $thumbnails is clearly not an array. You need to check to see if it is an array before you try to loop through its contents.
    – John Conde
    Apr 8, 2014 at 13:05

2 Answers 2

0

Maybe your array can be empty. Also cannot an array. Check the both...

Change your first lines as exist :

//Check if the thumbnails aren't empty
if(count($thumbnails)>0)
{
    //Check if is array
    if(is_array($thumbnails))
    {
        foreach ($thumbnails as $thumbnail) 
        {
            $modelItem['thumbnails'][] = $thumbnail;
        }
    }
    //You can print a message here if aren't array
}
0

Change code to below:

if(is_array($thumbnails) && count($thumbnails)){
    foreach ($thumbnails as $thumbnail) {
         $modelItem['thumbnails'][] = $thumbnail;
     }
}
3
  • count($thumbnails) can be thrown away Apr 8, 2014 at 13:24
  • The function $enclosure->get_thumbnails(); may return blank array. in this case, the if condition will not work. Apr 8, 2014 at 13:28
  • Two cases "if() condition fails because count() == 0" and "foreach() skips because the argument is empty" give equal results. Moreover, the only responsibility of if() is: "prevent warnings == protect foreach() from incorrect arguments". Empty array isn't incorrect. So, count() condition is redundant. Apr 9, 2014 at 9:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.