Prestashop 通知:未定义的偏移量:0

Prestashop Notice: Undefined offset: 0

我使用 prestashop 1.5.6,我尝试遵循 this tutorial

它有效,但它也在页面顶部给我这个错误 24 次:

Notice: Undefined offset: 0 in /home/deveskemhu/www/classes/Category.php on line 3157

我不得不在最后一个大括号之前添加这个:

   public static function getProductsImgSupp($product_id)

   {

    $sql = '

    SELECT id_image, id_product from `'._DB_PREFIX_.'image`

    WHERE id_product="'.$product_id.'"

    ORDER BY `position` ASC

    LIMIT 1,1

    ';

    $result = Db::getInstance()->ExecuteS($sql);

    return $result[0]['id_product'].'-'.$result[0]['id_image'];

    }

第 3157 行是上面代码中右大括号之前的行。

我该怎么做才能解决它?

尝试:

public static function getProductsImgSupp($product_id) {

$sql = '

SELECT id_image FROM `'._DB_PREFIX_.'image`

WHERE id_product="'.$product_id.'"

ORDER BY `position`';

$id_image = Db::getInstance()->getValue($sql);

return $product_id.'-'.$id_image;

}

此致

发生这种情况是因为并非每个 $result 都有索引 [0] - 意味着它是空的

先添加if语句行,判断是否为空:

public static function getProductsImgSupp($product_id)

{

    $sql = '

    SELECT id_image, id_product from `'._DB_PREFIX_.'image`

    WHERE id_product="'.$product_id.'"

    ORDER BY `position` ASC

    LIMIT 1,1

    ';

    $result = Db::getInstance()->ExecuteS($sql);

    if(isset($result[0])){

        return $result[0]['id_product'].'-'.$result[0]['id_image'];

    }

    // Here you could put an else in case it's empty

}