4

I have the following setup:

class.staff.php

This defines many variables, the one I'm working with right now is $thisuser->getStaffLang();

class.language.php

(Only a function, not a class) This file runs a sql query based on the one variable I pass it from header.inc.php as well as it should pull the staff members unique language ID.

The function is:

function translate($TRANSLATION){

$sql="SELECT $TRANSLATION FROM ".LANGUAGE_TABLE." WHERE LANGUAGE_ID=".$thisuser->getStaffLang;
 $query = mysql_query($sql);
 $translation = mysql_result($query,0);
 print $translation;
 }

header.inc.php

First file I'm working with using this function

example translation entry is:

translate('TEXT_WELCOME_BACK_STAFF');

My problem is that when I'm outside the function $thisuser->getStaffLang; is populated but inside the function it is empty. I really don't want to have to pass the same variable to the function over and over as some files can have upwards of 20 translations in them and that seems like alot of redundant coding. Can someone tell me how in the heck I can get that variable to be recognized by the function without have to pass it to it every single time when calling the function? Hope this wasn't clear as mud. :\

Note: Both class.language.php (where the function is and doesn't work) and header.inc.php (where the variable alone works) have required class.staff.php. So they both should be able to utilize that code/variable.

3 Answers 3

3

add global $thisuser; at the beggining of translate()

Sign up to request clarification or add additional context in comments.

2 Comments

Sigh, so simple! Thank you! I've been banging my head against the wall for this the last 2 hours. :P
hehe, tried that myself but I don't have the rep for it yet :P
1

You need to mark $thisuser as global. See below:

function translate($TRANSLATION)
{
   global $thisuser;     //<----  MUST MARK global

   $sql="SELECT $TRANSLATION FROM ".LANGUAGE_TABLE." WHERE LANGUAGE_ID=".$thisuser->getStaffLang;
   $query = mysql_query($sql);
   $translation = mysql_result($query,0);
   print $translation;
 }

Comments

0

$thisuser->getStaffLang is probably not a global. You should either make it global, by adding global $thisuser to the first line of the function,

or better, pass this variable also in the function scope. So something like this:

function translate($TRANSLATION, $stafflang){
// function here
}

translate('TEXT_WELCOME_BACK_STAFF',$thisuser->getStaffLang);

5 Comments

Giving Alon the credit as he answered first (though within seconds) and because I don't want to use the "pass the variable" method you described as there alot of entries I need to do this on and i don't want to have all that quasi-redundant code. Great answer though too as I know (now) that both ways you mentioned would work. Thanks very much!
you should not depend on the variable being there outside the function. Maybe somewhere in the future you change the variable name, or make it function or something, and then it suddenly fails to work, and will give you a lot of trouble then. Whithin functions/classes always have everything local
btw, it's not about being first, it's about the best answer
Thank you for the advice and I will definitely keep it in mind. On this particular variable though I know that it will never change, $thisuser is one of the most important variables in the entire structure. I'll be sure and remember what you've said though. thanks again.
"btw, it's not about being first, it's about the best answer" granted, but I specifically mentioned I did not want the way that you described in my original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.