Prestashop注意:未定义的偏移量:0

时间:2017-05-03 13:25:21

标签: php mysql prestashop prestashop-1.6 prestashop-1.5

我使用prestashop 1.5.6,我试图关注this tutorial

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

  

注意:未定义的偏移量:0英寸   第3157行/home/deveskemhu/www/classes/Category.php

我必须在最后一个大括号之前添加它:

   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行是上面代码中右括号之前的那一行。

我该怎么做才能解决它?

2 个答案:

答案 0 :(得分:1)

这是因为不是每个$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

}

答案 1 :(得分:0)

尝试:

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;

}

此致