Jump to content

Display db data on a new module page


Recommended Posts

Hi everyone!

 I've created a simple module to record some data in the db and it works well. Right now I'm trying to show some of these data in a specific user page. I've created a my-account.tpl file that link to a custom page (it works) and a file located file in controller --> front like this: 

public function initContent()

{

$caed = CassyTestModel::getCaedData($id_customer);

parent::initContent();

$this->setTemplate('module:myFirstModule/views/front/c-module.tpl');

}

 

that call a function that is:

public function getCaedData($id_customer)

{

$sql = new DbQuery();

$sql->select('caed_number');

$sql->from('caed_table');

$sql->where('id_customer = ' . (int)$id_customer);

return Db::getInstance()->getValue($sql);

}

And I need to show in in my tpl something like:

{extends file='page.tpl'}

{block name='page_content'}

<div class="col-md-12">

<h2>{l s='Caed models' mod='caedmodels'}</h2>

{if isset($caed)}<p>"{$caed}"</p>

{else}

<p>NO DATA</p>

{/if}

</div>

{/block}

 

The tpl is really basic as I just need it to work well but I don't understand where I'm getting wrong. Sorry if the questions could be silly, it is my first approach on PS

Thank you ^^ 

CC

 

Link to comment
Share on other sites

1 hour ago, endriu107 said:

You need to pass data to smarty.

Check in other modules there is something like: $this->context->smarty->assign()

Hello and thank you!

I changed my controller file like this:

$id_customer=(int)$this->context->customer->id;
$caed = CassyTestModel::getCaedData($id_customer);
//var_dump($caed);
$this->context->smarty->assign([
'caed' => $caed,
]);
return $this->setTemplate('module:myFirstModule/views/front/c-module.tpl');

and it seems working if on DB I have a text or integer value. As I need to display a date i got empty value on .tpl files and on var_dump a bool(false) results. The db value is a simple date like 2021-10-04 00:00:00 that I want to display like D - M - Y 00:00 

 

Thank you!

Edited by Chariel
Updated the code (see edit history)
Link to comment
Share on other sites

As endriu107 wrote.

In order to have access to your DB data that you fetched with getCaedData($id_customer),  you need to pass it to the template using:

$this->context->smarty->assign('caed', --return from function--);

Or if you'd like to assign multiple variables:

$this->context->smarty->assign(array('variable_name' => $variable, 'variable_name_2' => $variable_2));

 

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...