|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Sentro Smarty n00b
Joined: 12 Aug 2013 Posts: 4
|
Posted: Mon Aug 12, 2013 3:12 pm Post subject: PHP Function in smarty |
|
|
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
I was hoping someone could help me on how to display the result in the smarty file.
Thank you in advance |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Aug 12, 2013 8:25 pm Post subject: |
|
|
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 |
|
Sentro Smarty n00b
Joined: 12 Aug 2013 Posts: 4
|
Posted: Tue Aug 13, 2013 7:52 am Post subject: |
|
|
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 |
|
Sentro Smarty n00b
Joined: 12 Aug 2013 Posts: 4
|
Posted: Tue Aug 13, 2013 1:16 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Aug 13, 2013 1:37 pm Post subject: |
|
|
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 |
|
Sentro Smarty n00b
Joined: 12 Aug 2013 Posts: 4
|
Posted: Tue Aug 13, 2013 1:58 pm Post subject: |
|
|
EDIT: Figured it out eventually.
Thank you very much for your help. You saved me. |
|
Back to top |
|
|
|
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
|