dsaaqdz6223 2017-07-10 13:59
浏览 57
已采纳

错误TPL PHP Prestashop

I am working on Prestashop and I would like to retrieve the data from a table I created in my database. I've been on it for 2 days and nothing works, I'll go crazy.

Here is the code I put in my ProductController.php file:

    private function GetAvailableAttributes($id_produit, $id_compte, $note)
    {      
    global $smarty;  
$sql = 'SELECT id_produit, id_compte, note
        FROM `'.'ps_product_avis';
    $ess=mysql_result($sql,0);
$smarty->assign('contact', $ess);
    }

Here is the code put in the product.tpl file:

<li>test :{$contact}</li>

Here's the mistake it gives:

Error message

Could one of you save me and explain where the problem comes from please?

I thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongxiong1941 2017-07-10 14:41
    关注

    In PrestaShop 1.7 (and also 1.5 and 1.6) the global var $smarty was deprecated. You have made various errors, I'll try to explain with the corrections :)

    Change your code in this:

    private function GetAvailableAttributes($id_produit, $id_compte, $note)
    {
        /* global $smarty; DEPRECATED */ 
        $sql = 'SELECT id_produit, id_compte, note
            FROM `'._DB_PREFIX_.'product_avis`';
        /* Use always _DB_PREFIX_ constant var */
    
        /* $ess=mysql_result($sql,0); Use Db class instead of this old method */
        $ess = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
    
        /* $smarty->assign('contact', $ess); Deprecated use */
        $this->context->smarty->assign('contact', $ess);
    }
    

    Remember that the executeS method returns a set of array, also if there is one element to return.

    So your array will be like this:

    array(
        [0] => array(
            'id_produit' => 1,
            'id_compte' => 1,
            'note' => 'my note'
        )
    );
    

    In smarty to print single element you should do something like this:

    {$contact[0]['note']}
    

    It will print my note.

    That's all :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里