Jump to content

[SOLVED] How to SQL query in a tpl file? n00b inside


Benimaru

Recommended Posts

Hi,

First of all, I have to say that I'm a n00b PS developer, and I need some advice. I know this might seem a stupid question, but it's really hard to find developer info or tutorials about PS (any links?), just bits of info here and there.

My problem is: I just need to show the content of a new table (ps_do) I've created in main DB, in my product.tpl.

So I've created the following method in classes/Product.php

   public function getDO($id_product){

       $result = Db::getInstance()->ExecuteS('
       SELECT `do`
       FROM `ps_do`
       WHERE `id` = '.intval($id_product));

       return $result;
   }



But I can't call this method (or doesn't work) in product.tpl. If I try for example in product.tpl

$something = Product::getDO($val)



$something is always unset.

I'm doing this possibly uber wrong, but right now I'm trying to develop looking at code parts and trying to deduce how all works, the awesomely slow way :/

Any help will be much appreciated. Thanks!

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
Yes, right after doing this:
$something = Product::getDO($val) 


do this:

$smarty->assign('do_records', $something);


Then in your template, you have access to it like this:

{foreach $do_records as $items}
 {items}
{/foreach}



Hi people, thanks for your replies!

... but I still can't get it working after lots of changes, I don't know what I'm doing wrong and I'm feeling stupid :(. It's not an SQL problem, it's about php function and smarty assign.

Please, can somebody point me how to do a new function (and exactly where to put it, in which file) if I want to show the result in a tpl file?

I can't get a simple var to get passed to a tpl file, its very sad >_
Link to comment
Share on other sites

First of all, enable error reporting and debugging in the config.inc.php file.
And you need to add "static" to your function definition if you want to call it as a static function.


Thankyou very much! No need to add static to that function to get it working, the key has been to enable error report (I knew I was missing something from pure php coding, hahaha).

I let it all pasted here, maybe somebody will find it useful :D

Accessing an unique product feature, having stored it's contents in a separate table

in classes/Product.php
   public function getDO($id_product){

       $result = Db::getInstance()->ExecuteS('
       SELECT *
       FROM `ps_do`
       WHERE `id` = '.intval($id_product));            

       return $result;
   }


Code added at the end of Product class. ps_do is a new table with an unique id and 2 more columns of info.

Add in product.php (between /* Features / Values */ and /* Category */ )

//id_feature 19 it's the feature I'm trying to get info from
$do = Product::getDO($features['id_feature' == 19]['value']);
$smarty->assign('do_records', $do); 



in product.tpl (inside a foreach bucle of printing features)

{if $feature.name == 'DO'}
{$feature.name|escape:'htmlall':'UTF-8'|cat:": "}

<!-- Getting more info of this feature, from an external table-->
{if (isset ($do_records))}
{foreach from=$do_records item=foo}
{$foo.region}
{/foreach}
{/if}
{/if}

'region' is one column of extra do info from 'ps_do' table

That's all, problem solved, thanks for your cooperation :D
  • Like 1
Link to comment
Share on other sites

  • 2 years later...
  • 7 years later...

What I should change if I using Prestashop 1.7?

I want to show array from db table.

I was try add to ProductController this code

<?php
public function getHSH($id_product)
{
		$id_product = (int)Tools::getValue('id_product');   
        
$sql = 'SELECT kolumna1, kolumna2, id_product FROM '._DB_PREFIX_.'product_hsh where id_product='. $id_product . '' ;
$result = Db::getInstance()->executeS($sql);
$hsha=array();
foreach ($result as $row)
{
  $hsha[] = array(
    "hsh1" => $row['kolumna1'],
    "hsh2" => $row['kolumna2'],
  );
}
$this->context->smarty->assign("hsh", $hsha);

and to product-details.tpl

<section class="product-features">
        <h3 class="h6">{l s='HSH' d='Shop.Theme.Catalog'}</h3>
        		<dl class="data-sheet">
				{foreach $hsh as $hsha}
            <dt class="name">{$hsha.hsh1}</dt>
            <dd class="value">{$hsha.hsh2}</dd>
			{/foreach}
        </dl>
      </section>

and it not working.

 

Can anybody help me ?

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