Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

PHP Function in smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
Sentro
Smarty n00b


Joined: 12 Aug 2013
Posts: 4

PostPosted: Mon Aug 12, 2013 3:12 pm    Post subject: PHP Function in smarty Reply with quote

Hello, i'm new to smarty.
I'm trying to use a PHP function to display the result in a smarty file.

Below is the function code

"<?php

function getcommission($inputid)
{
$database="xxxxxxxxx";
mysql_connect ("xxxxxxxxxx");
@mysql_select_db($database) or die( "Unable to select database");

$data = mysql_query("SELECT * FROM hb_aff WHERE id = $inputid")
or die(mysql_error());
while($infocom = mysql_fetch_array( $data ))
{
return $infocom['total_commissions'];
}
}
?> "


I did some research, read somewhere i need to register the plugin, i also need to update the smarty.class.php file, i also need to register an object and i'm a little confused right now Smile

I was hoping someone could help me on how to display the result in the smarty file.

Thank you in advance
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Aug 12, 2013 8:25 pm    Post subject: Reply with quote

You can call any PHP function directly in the template. To display the result in your case it would be
Code:
{getcommission($inputid)}

The function parameter ($inputid) must be an assigned Smarty template variable.

The question is why you whant to call the function in the template?
Other solution would be to execute the function in the PHP script and assign the result as Smarty template variable.
Back to top
View user's profile Send private message
Sentro
Smarty n00b


Joined: 12 Aug 2013
Posts: 4

PostPosted: Tue Aug 13, 2013 7:52 am    Post subject: Reply with quote

U.Tews wrote:
You can call any PHP function directly in the template. To display the result in your case it would be
Code:
{getcommission($inputid)}

The function parameter ($inputid) must be an assigned Smarty template variable.

The question is why you whant to call the function in the template?
Other solution would be to execute the function in the PHP script and assign the result as Smarty template variable.


Ok the function parameter is a smarty variable.

I have this issue with an online platform.
There's a list of $affiliate.XXX smarty variables.
THing is in all the pages except from 1 no variable is visible except the $affiliate.id. That one shows up everywhere just fine.

Every else variable like $affiliate.firstname,lastname etc etc not appearing. I know it's not "empty" since it does show up in that 1 page but i can also double check on the database.


What i'm trying to do is run PHP function in order to show them all in my page but get those from the database instead of getting them from the smarty variables.

So in the first case is there a specific location for the php file?

thanks
Back to top
View user's profile Send private message
Sentro
Smarty n00b


Joined: 12 Aug 2013
Posts: 4

PostPosted: Tue Aug 13, 2013 1:16 pm    Post subject: Reply with quote

Ok i have finally worked out the issue about running php functions in smarty. But now how can i call that function with a smarty variable as a parameter? Here's the code.

This is in the plugins folder.

Code:
function smarty_function_getcommission( $params, &$smarty )
{
    include 'getcommission.php';
    getcommission({$affiliate.id});
}

?>


And the php is the following one

Code:
<?php
function getCommission({$affiliate.id})
{
    $database="XXXXXXXXXXX";
    mysql_connect ("XXXXXXXXXXXXX");
    @mysql_select_db($database) or die( "Unable to select database");

 $data = mysql_query("SELECT * FROM hb_aff WHERE id = {$affiliate.id}")
 or die(mysql_error());
 while($info = mysql_fetch_array( $data ))
 {
 Print $info['total_commissions'];
 }
}
 ?>


I call the function using {getcommission}. However when i try to add the {$affiliate.id} as a parameter for the function i get an error. Any ideas what im doing wrong?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Aug 13, 2013 1:37 pm    Post subject: Reply with quote

Code:
{getcommission id=$affiliate.id}


Code:
function smarty_function_getcommission( $params, &$smarty )
{
    include 'getcommission.php';
    getcommission($params['id']);
}


Ideally your plugin should *return* the value and not print it out, then it is safer to use in caching and whatnot.

Code:
function smarty_function_getcommission( $params, &$smarty )
{
    include 'getcommission.php';
    return getcommission($params['id']);
}


Be sure getcommission() accumulates the output and returns it instead of printing it to stdout.


Code:
while($info = mysql_fetch_array( $data ))
 {
    $output .= $info['total_commissions'];
 }
 return $output;
Back to top
View user's profile Send private message Visit poster's website
Sentro
Smarty n00b


Joined: 12 Aug 2013
Posts: 4

PostPosted: Tue Aug 13, 2013 1:58 pm    Post subject: Reply with quote

EDIT: Figured it out eventually.

Thank you very much for your help. You saved me.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP