Tags
Asked 3 years ago
7 Jul 2021
Views 758
Dallin

Dallin posted

Prestashop 1.7 How to Display data from database to tpl

Prestashop 1.7 How to Display data from database to tpl
jaydeep

jaydeep
answered Oct 8 '21 00:00


in Prestashop 1.7, put code at the controller for getting data first.
suppose you want to get display data on the cart page. so put the code at initContent() function at CartController.php

public function initContent()
  {
....
....

 $sql = " select * from `"._DB_PREFIX_."cart_extra`  where id_cart='".$this->context->cart->id."'";
 $cart_extra_data = Db::getInstance()->executeS($sql);

//and now pass data to template 
 $this->context->smarty->assign([
            'cart' => $presented_cart,
             'cart_extra_data' => $cart_extra_data, // passing data to template 
             ]);
            $this->setTemplate('checkout/cart');
      ....
      ....


as you see above code we run a query in the controller which is not the right way to do it, you should use the model to do it so but for easy explanation I am using data fetcher code at the controller.
so Db::getInstance()->executeS ($sql); will run given MySQL query and return data from table.

after that we passed on the data to the template by $this->context->smarty->assign() function.
now at template file , checkout/cart.tpl use $cart_extra_data as per need.

Post Answer