错误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:
Could one of you save me and explain where the problem comes from please?
I thank you in advance.
我正在使用Prestashop,我想从我在数据库中创建的表中检索数据。 我已经使用了2天没有任何效果,我会发疯的。 p>
这是我放在ProductController.php文件中的代码: p> \ n
私有函数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-&gt; assign('contact',$ ess );
}
code> pre>
以下是放在product.tpl文件中的代码: p>
&lt; li&gt;测试:{$ contact}&lt; / li&gt;
code> pre>
这是它给出的错误: p>
你们中有人能救我并解释问题的来源吗? p>
我提前感谢你。 p>
div>
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 :)