Qwerty
Qwerty

Reputation: 433

Query database in tpl file for CSCart add on?

I have created an add on for cscart.

The template file tpl needs to show data from a table in the cscart database.

How/where would I query the mysql database?

I have read that it is bad practice to put the query in the TPL file. That is true as per following the MVC principle.

So when creating an add on, which file can I create to perform this query and retrieve necessary data to pass on to the tpl file? Not sure where to create an independent php file to do the data extraction and passing to tpl file. I don't prefer to edit the default cscart file.

Where can I get a guide/information on this?

This is the command I need to run:

db_get_array('SELECT column FROM ?:table WHERE user_id = ?s', $user_id)

But this won't work on tpl file...

Upvotes: 0

Views: 1549

Answers (1)

Hungryweb
Hungryweb

Reputation: 625

1. is an error on db_get_array, $user_id is an integer, the correct query is

db_get_array('SELECT column FROM ?:table WHERE user_id = ?i', $user_id)

2. depending of the cs-cart version you have different folders where you need to add php and tpl files

v2.x & v3.x

addons/[ADDON_NAME]/controllers/customer/[PHP].php
skins/[SKIN_NAME]/customer/addons/[ADDON_NAME]/views/[PHP_FILE]/[MODE].tpl

v4.x

app/addons/[ADDON_NAME]/controllers/customer/[PHP].php
design/themes/[THEME_NAME]/templates/addons/[ADDON_NAME]/views/[PHP]/[MODE].tpl

above is the case for the new controllers

When you are just hooking to the actual controllers

v2.x & v3.x

addons/[ADDON_NAME]/controllers/customer/[CONTROLLER].post.php
skins/[SKIN_NAME]/customer/addons/[ADDON_NAME]/views/[CONTROLLER]/[MODE].tpl

v4.x

app/addons/[ADDON_NAME]/controllers/customer/[CONTROLLER].post.php
design/themes/[THEME_NAME]/templates/addons/[ADDON_NAME]/views/[CONTROLLER]/[MODE].tpl

Upvotes: 1

Related Questions