Jump to content

Get data from DB in TPl


sanl

Recommended Posts

May u help me with such thing? I have SQL query for example get customer firstname and lastname who has newsleter and print it in TPL file.

 

$sql = 'SELECT firstname, lastname, id_customer FROM ps_customer  WHERE newsletter = "1" ';
 
while($row=mysql_fetch_array($sql))
{
  $custdata[] = array(
    "firstname" => $row[0],
    "lastname" => $row[1],
  "id_customer" => $row[2]
  );
}
$this->context->smarty->assign("custdata", $custdata);
 

 

AND this is my TPL FILE:
 
{foreach $custdata as $item}
  {$item.firstname}:{$item.lastname}
{/foreach}

Error:

( ! ) Notice: Undefined variable: custdata in C:\wamp\www\b\modules\mymodule\controllers\front\display.php on line 32

 

( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\wamp\www\b\modules\mymodule\controllers\front\display.php on line 24

 

If u can give me a clear example which is working with PS16.

Link to comment
Share on other sites

1) first use DB class instead of using mysql_fetch_array

$sql = 'SELECT firstname, lastname, id_customer FROM ps_customer  WHERE newsletter = "1" ';
$result = Db::getInstance()->ExecuteS($sql);

2) initialize the $custdata array outside of the loop and use a foreach instead of while.

$custdata=array();
foreach ($result as $row)
{
  $custdata[] = array(
    "firstname" => $row['firstname'],
    "lastname" => $row['lastname'],
    "id_customer" => $row['id_customer']
  );
}
$this->context->smarty->assign("custdata", $custdata);

your template code looks fine

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

Hi!

With you I can see some light thru the tunnel..

 

My need is to print some data in the shopping-cart.tpl

 

If the user_gorup is < 4 then I need to display the related delivery costs (for example from 0€ to 50€ delivery is 8 euro and over 50$ delivery is free)

 

If user_group is 4 then other data.

 

These data should be taken by SQL form database. That's why I need to do a query and then output...

Link to comment
Share on other sites

{if $cart->id_customer}
    {foreach Customer::getGroupsStatic($cart->id_customer) as $group}
        {if $group==4}
        {l s='Im customer frop group 4'}
        {/if}
    {/foreach}
{/if}

code above is is an basic example of code to use in shopping-cart.tpl

no core modifications needed.

Link to comment
Share on other sites

  • 1 year later...

H

On 6/1/2016 at 7:42 PM, vekia said:

{if $cart->id_customer}
    {foreach Customer::getGroupsStatic($cart->id_customer) as $group}
        {if $group==4}
        {l s='Im customer frop group 4'}
        {/if}
    {/foreach}
{/if}

code above is is an basic example of code to use in shopping-cart.tpl

no core modifications needed.

Hello, I want to do this in the backOffice. I have created an Admin Controller and successfully added a tpl file which is running there. Now I want to get the data from the db. I know the db commands which will be used but I don't know where to put those commands. Any help or guidance?

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