Jump to content

Problem to make a mySQL query with getValue [SOLVED]


dr.pepper

Recommended Posts

Hi

 

I used the next thing in a .tpl file to get the value of a variable by GET method:

 

{$f=Tools::getValue('fo')}

 

and later making conditionals like {if $f == some value ] Do A {else} Do B {/if}

 

Well, works ok when tested in local mode (localhost) but doesn't when I upload it to a server, with debug on, I got this error:

 

Fatal error: Smarty error: [in /var/www/vhosts/*******.com/httpdocs/themes/mytheme/./product-list.tpl line 31]: syntax error: unrecognized tag: $f=Tools::getValue('fo') (Smarty_Compiler.class.php, line 446) in /var/www/vhosts/**********.com/httpdocs/tools/smarty_v2/Smarty.class.php on line 1095

 

I don't know why works ok in one and not in the other. Any tips? Both Prestashops are the same version.

 

And by other way, Is there a better solution to get the value of GET method in a .tpl file?

 

Many thanks.

Edited by dr.pepper (see edit history)
Link to comment
Share on other sites

below is how you are supposed to do it in smarty templates. what you are doing is not necessarily wrong, just not the preferred way

 

{* display value of page from URL ($_GET) http://www.example.com/index.php?page=foo *}
{$smarty.get.page}

{* display the variable "page" from a form ($_POST['page']) *}
{$smarty.post.page}

{* display the value of the cookie "username" ($_COOKIE['username']) *}
{$smarty.cookies.username}

{* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*}
{$smarty.server.SERVER_NAME}

{* display the system environment variable "PATH" *}
{$smarty.env.PATH}

{* display the php session variable "id" ($_SESSION['id']) *}
{$smarty.session.id}

{* display the variable "username" from merged get/post/cookies/server/env *}
{$smarty.request.username}

Link to comment
Share on other sites

Hi

 

I will explain the problem with an example:

 

From an url like mypage.php?f=3:

 

Get the f value and use it to do a MySQL query, and print something or not if there are results.

 

 

 

For now I used:

 

in controller.php

--------------------

 

self::$smarty->assign('f', (int)Tools::getValue('f'));

$f = (int)Tools::getValue('f');

self::$smarty->assign('result', Db::getInstance()->ExecuteS('SELECT * FROM mytable WHERE id_f = '.$f));

 

 

 

In .tpl file

-----------

 

{if $result}

Let's do A

{else}

Let's do B better

{/if}

 

 

 

 

I know It's not the better solution, but at least works for me.

The two lines:

self::$smarty->assign('f', (int)Tools::getValue('f'));

$f = (int)Tools::getValue('f');

are in fact reduntant, but like bellini13 described, maybe using $smarty.get.page it can be solved.

Link to comment
Share on other sites

what you have done in the controller is what you are expected to do. the controller should be used to produce the information to display, you use the template to display it.

 

the redundancy can be removed by doing this instead in the controller

$f = (int)Tools::getValue('f');
self::$smarty->assign('result', Db::getInstance()->ExecuteS('SELECT * FROM mytable WHERE id_f = '.$f));

 

I'm not sure why you do this...

self::$smarty->assign('f', (int)Tools::getValue('f'));

 

If you just want to display the value of f in the template, then do this

{$smarty.get.f}

Edited by bellini13 (see edit history)
Link to comment
Share on other sites

Hi

 

I used in the controller:

 

self::$smarty->assign('f', (int)Tools::getValue('f')); ---> To display value of f later in the tpl

$f = (int)Tools::getValue('f'); ---> Get again f to use in 'WHERE'

self::$smarty->assign('results', Db::getInstance()->ExecuteS('SELECT * FROM table WHERE id_f = '.$f));

 

 

So, {$smarty.get.f} is the way to get $_GET[] value directly in a tpl? So, could be instead:

 

(Controller)

$f = (int)Tools::getValue('f');

self::$smarty->assign('results', Db::getInstance()->ExecuteS('SELECT * FROM table WHERE id_f = '.$f));

 

(tpl)

{$smarty.get.f}

 

 

Sorry if I made so many troubles, I hope to not misunderstanding it.

Link to comment
Share on other sites

Hi, many thanks

 

I tried {$smarty.get.f} in tpl and prints directly the value from $_GET[]

 

If I want for example, get the value from $_GET[] and store it in a new variable (in tpl) , I tried something like:

{assign var = f value = $smarty.get.f}

and later when you wants to print the value: {$f}

 

Anyway, as you said before, It's better get values, variables, etc. in controller instead on tpl.

 

Finally I get to work make a sql query with filtering with a value getting from $_GET[] method, many thanks for your help.

Link to comment
Share on other sites

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...