担心我的模块后台 undefined index prestashop

Worry in the backoffice of my module undefined index prestashop

我开发了一个prestashop模块,当我在我的模块后台时,我有很多错误:

Notice à la ligne 30 du fichier C:\xampp2\htdocs\projet_presta\app\cache\dev\smarty\compilecc1c1cb1906b13002a1460e54203e8a370598366.file.displayContent.tpl.php [8] Undefined index: avisnote Notice à la ligne 30 du fichier C:\xampp2\htdocs\projet_presta\app\cache\dev\smarty\compilecc1c1cb1906b13002a1460e54203e8a370598366.file.displayContent.tpl.php [8] Trying to get property of non-object

在我的模块 avisnote.php 文件中:

public function displayContent()
    {
        $avisnote = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'avisnote` LIMIT 1');
        print_r($avisnote);
        foreach ($avisnote as $row)
        {
            $file = $row['url'];
            $state = $row['etat'];
        }

        if(isset($fileContent) && !empty($fileContent))
        {
            $fileContent = file_get_contents($file, NULL, NULL);

            $var_sep = explode(";", $fileContent);
            $nb_avis = $var_sep[0]; 
            $note = $var_sep[1];
            $this->context->$smarty->assign('avisnote',
                    array('nb_avis' => $nb_avis,
                          'note'=>$note,
                          'etat'=>$state));
        }
        return $this->display(__FILE__,'views/templates/hook/displayContent.tpl');

    }

我的文件路径:

displayContent.tpl:

<p></p>
<div itemprop="itemreviewed" itemscope="" itemtype="http://data-vocabulary.org/Review-aggregate">
    <span itemprop="itemreviewed">Mywebsite</span> <span itemprop="rating" itemscope="" itemtype="http://data-vocabulary.org/Rating"> <span itemprop="average">{$avisnote.note}</span> sur <span itemprop="best">5</span> </span>
    <br />basé sur <span itemprop="votes">{$avisnote.nb_avis}</span> avis Avis Vérifiés</div>
</div>

您的代码中至少有 2 个错误。

首先检查 isset($fileContent),然后使用变量 $file。其次,你有 $this->context->$smarty ...你的代码固定应该更像:

public function displayContent()
{
    $avisnote = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'avisnote` LIMIT 1');
    print_r($avisnote);
    foreach ($avisnote as $row)
    {
        $file = $row['url'];
        $state = $row['etat'];
    }

    $nb_avis = $note = $state = '';
    if(isset($file) && !empty($file))
    {
        $fileContent = file_get_contents($file, NULL, NULL);

        $var_sep = explode(";", $fileContent);
        $nb_avis = $var_sep[0]; 
        $note = $var_sep[1];
    }
    $this->context->smarty->assign('avisnote',
                array('nb_avis' => $nb_avis,
                      'note'=>$note,
                      'etat'=>$state));
    return $this->display(__FILE__,'views/templates/hook/displayContent.tpl');

}