lain Posted June 3, 2013 Share Posted June 3, 2013 Hello, I am working in "AdminCustomers.php" trying to save a value in a variable from one function and use this variable/value in another function but unsuccessfully I tried using global $variable; public $variable; $smarty->assign('variable', $firstFunctionVar); $this->context->smarty->assign('variable',$firstFunctionVar); I am sure its a dummy thing. thank you Link to comment Share on other sites More sharing options...
Paul C Posted June 4, 2013 Share Posted June 4, 2013 Iain, I'm really not sure from the example what you're trying to do as you're declaring a global variable and then not using it.. If you think of $this->context->smarty as a globally accessible variable then you can do the following: public function first() { $variable = 'Some value'; $this->context->smarty->assign('variable', $variable); } public function second() { // Get the value stored on the "first" function $variable = $this->context->smarty->tpl_vars['variable']->value // Change the value and then.... $variable = 'Modified value'; // Save it in the template again $this->context->smarty->assign('variable', $variable); } Note that you don't have to assign the same smarty variable in every function unless you want to change its value - it will persist across functions. 1 Link to comment Share on other sites More sharing options...
lain Posted June 4, 2013 Author Share Posted June 4, 2013 Thank you Paul but the problem using $this->context->smarty->assign('variable', $variable); Is that I get an error: Fatal error: Call to a member function assign() on a non-object in W:\var\www\server\administrator\tabs\AdminCustomers.php on line 874 Some times I have used $smarty->assign('variable', $firstvariable); in "FrontController.php" to use $variable in tpl files, but in that case, from "AdminCustomers.php" I cant use $smarty->assign or $this->contest->smarty->assign. Anyway, as I said, I want to use $variable from function one() in function two() Link to comment Share on other sites More sharing options...
vekia Posted June 4, 2013 Share Posted June 4, 2013 for the first you should give us information what PS version you use, everything depends on it regards Link to comment Share on other sites More sharing options...
lain Posted June 4, 2013 Author Share Posted June 4, 2013 You are right. Sorry I am using 1.4.5.1 Link to comment Share on other sites More sharing options...
vekia Posted June 4, 2013 Share Posted June 4, 2013 so, in 1.4.5.1 context object doesnt exist. you should try with simple $this->variable="value"; you can define value of this variable in one function - then you will be able to use it in other functions Link to comment Share on other sites More sharing options...
lain Posted June 4, 2013 Author Share Posted June 4, 2013 Hello vekia, thanks for answering I have tried $this->variableSample="Sample text!"; and just used $variableSample in the same function or another function but unsuccessfully. $variableSample keeps empty Link to comment Share on other sites More sharing options...
vekia Posted June 4, 2013 Share Posted June 4, 2013 but you have to use $this->variableSample not $variableSample Link to comment Share on other sites More sharing options...
lain Posted June 4, 2013 Author Share Posted June 4, 2013 (edited) anyway it doesnt work function one() { $this->variableSample = "asdf"; echo $this->variableSample; } function two() { $secondvar = $this->variableSample; echo $this->variableSample; echo $secondvar; } echo from first function is showed but echo´s from second function arent Edited June 4, 2013 by lain (see edit history) Link to comment Share on other sites More sharing options...
vekia Posted June 4, 2013 Share Posted June 4, 2013 hope you first call: function one(); (you're defining variable here) then: function two(); you can also define $this ->variableSample in the __construct function Link to comment Share on other sites More sharing options...
lain Posted June 4, 2013 Author Share Posted June 4, 2013 Hello again vekia, first of all thank you for your help defining the variable in function __construct allows me to use variable in function one and function two, thats ok BUT variable can not be modified in function one and get this modified value from function two. If function one modifies variable value, when function two call this variable, it has the original value from function __construct, not the value from function one. I hope you can understand me Link to comment Share on other sites More sharing options...
vekia Posted June 4, 2013 Share Posted June 4, 2013 another question, sounds dumb but i have to ask you use two these funcions in one call ? or maybe you using this in separated calls ? Link to comment Share on other sites More sharing options...
lain Posted June 4, 2013 Author Share Posted June 4, 2013 It doesnt sound dumb, functions are called separately, I am the dumb I will try to explain what I am doing: I want to use mail::send when a customer is changed from inactive to active when you edit a customer, function one is executed to print in screen customer fields, I try to save customer status into a variable. When you save from editing customer its executed function two, here I want to compare old customer status(variable from function one) and actual customer status and if it changes... { .. mail::send .. } Link to comment Share on other sites More sharing options...
lain Posted June 6, 2013 Author Share Posted June 6, 2013 Anyway, someone knows how to do that? call a function, modify a variable and use this variable in another function Link to comment Share on other sites More sharing options...
vekia Posted June 6, 2013 Share Posted June 6, 2013 hello sorry for late response, i didn't receive notification that you replied here. Maybe try to use Configuration::updatevalue('your_variable',$value); and Configuration::get('your_variable'); Link to comment Share on other sites More sharing options...
lain Posted June 7, 2013 Author Share Posted June 7, 2013 Thank you vekia, your idea fixed my problem, using shop database and PS_CONFIGURATION table to save variable value temporarily was the solution. Thank you again Link to comment Share on other sites More sharing options...
vekia Posted June 7, 2013 Share Posted June 7, 2013 you're welcome i marked this thread as [solved] if you've got another questions related to this case - feel free to ask and continue discussion regards! Link to comment Share on other sites More sharing options...
mani_dev Posted December 8, 2015 Share Posted December 8, 2015 (edited) Hello, I used a custom function to export with custom fields, I can't get $this->arrayVariable inside that function and also can't pass array from .tpl to that function. public function processMyfunctionName(){ $this->arrayVariableName; } By Using Configuration::updatevalue('your_variable',$value); and Configuration::get('your_variable'); method I can't get array varible. How to solve this issue ?. Thanks. Edited December 8, 2015 by mani_dev (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now