Zen Cart Logo
Forums / All Other Contributions/Addons / Automatic Category Images Error

Automatic Category Images Error

Views: 667

Results 1 to 3 of 3
16 Dec 2020, 2:39 AM
#1
brian70809 avatar

brian70809

New Zenner

Join Date:
Jun 2011
Posts:
85
Plugin Contributions:
0

Automatic Category Images Error

Using Zen Cart 1.5.6c

I get this error if there is no image for an item.. I have 20k items and a ton of categories.. I do try and correct the problem, but I'd rather it just put "NoImage.jpg" instead of throwing an error.

Any help would be appreciated.

This is error

PHP Warning: array_rand() expects parameter 1 to be array .... httpdocs/includes/functions/extra_functions/functions_auto_category_images.php on line 13

This is line 13:

$image = auto_category_images( $sub_categories[array_rand( $sub_categories, 1 )] );

<?php

    function auto_category_images( $catID ) {
        global $db;

        if ( $catID > 0 ) {

            $cat_has_products = $db->Execute("SELECT p.products_image FROM " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c WHERE p.products_id = p2c.products_id AND p2c.categories_id = " . $catID . " ORDER BY RAND() LIMIT 1");
            if($cat_has_products->RecordCount() > 0){
                $image = $cat_has_products->fields['products_image'];
            } else {
                zen_get_subcategories( $sub_categories, $catID );
                $image = auto_category_images( $sub_categories[array_rand( $sub_categories, 1 )] ); 
              }
        } else {
            // This category contains no products!
            $image = 'pixel_trans.gif';
        }

        return $image;
    }

?>
16 Dec 2020, 6:19 AM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Automatic Category Images Error

Where did this function come from? It's not part of Zen Cart core.

I suspect the following will stop the error by virtue of setting the variable to an array before it is accessed. But it feels like your function is doing a lot of extra queries.

                $image = $cat_has_products->fields['products_image']; 
            } else { 
                $sub_categories = array();
                zen_get_subcategories( $sub_categories, $catID ); 
4 Aug 2021, 4:57 PM
#3
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,689
Plugin Contributions:
56

Re: Automatic Category Images Error

This bug is fixed in the latest version of Automatic Category Images.