Throw error: how to use variable instead of text?

Any tips and tricks that has to with the ADDT that doesn't fit into one of the other categories
antonio
Posts: 77
Joined: 2010-09-28 11:48

Throw error: how to use variable instead of text?

Post by antonio » 2012-05-23 15:48

Hi all,
I trying to make a multiple language system with ADDT.
I set a session variable and include the related language file.
The language files (it.php, en.php..) contain all kind of text strings.
All works perfectly but I have problem trying to translate the error messages triggered by the Throw error server behaviour.
I set the variable $myerror['text'] and insert it where I should insert the error message text (in code view).
But it doesn't work: I get no error message.

Here is the trigger code:

Code: Select all

//start errore_no_qta trigger
//remove this line if you want to edit the code by hand
function errore_no_qta(&$tNG) {
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg($myerror['text']);
  $myThrowError->setField("");
  $myThrowError->setFieldErrorMsg("");
  return $myThrowError->Execute();
}
//end errore_no_qta trigger
Is there a way to use variable with Throw error server behaviour? How can I customize the error message in a multilanguage website?

TIA all.

tony

User avatar
Fred
Site Admin
Posts: 491
Joined: 2010-02-15 12:10
Location: Armagh, Northern Ireland
Contact:

Re: Throw error: how to use variable instead of text?

Post by Fred » 2012-05-26 11:22

Hi Tony,
I think you are loosing scope of some variable inside the function.

What happens if you echo the language session variable inside the function?

Code: Select all

function errore_no_qta(&$tNG) {
  echo $_SESSION['language'];
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg($myerror['text']);
  $myThrowError->setField("");
It might also loose focus on the function that does the lookup to find the correct translation for $myerror['text']
Does it make sense?
I did not had any sleep for the last 24 hours so I might be completely of the mark here.

antonio
Posts: 77
Joined: 2010-09-28 11:48

Re: Throw error: how to use variable instead of text?

Post by antonio » 2012-05-28 09:03

Fred wrote:Hi Tony,
I think you are loosing scope of some variable inside the function.

What happens if you echo the language session variable inside the function?

Code: Select all

function errore_no_qta(&$tNG) {
  echo $_SESSION['language'];
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg($myerror['text']);
  $myThrowError->setField("");
I can see the correct language output of the session on top of the page: it.
I tried to echo the error variable instead of the session variable but it outputs... nothing.

Code: Select all

function errore_no_qta(&$tNG) {
  echo $myerror['text'];
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg($myerror['text']);
  $myThrowError->setField("");
I tried even to output the session var instead of the error message and it works: the error message gets the lang session string, see below:

Code: Select all

function errore_no_qta(&$tNG) {
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg($_SESSION['lang']);
  $myThrowError->setField("");
The function that loads the language file is set on the very top of the page, so, all variables it contains should be available as soon as the page loads.
Keep in mind that all other areas in the page will display all variables correctly, even in other ADDT server behaviours; it is only the Throw Error behaviour that returns blanks when using variables other than session variable.

Thanks for any help, Fred.

Tony

User avatar
Fred
Site Admin
Posts: 491
Joined: 2010-02-15 12:10
Location: Armagh, Northern Ireland
Contact:

Re: Throw error: how to use variable instead of text?

Post by Fred » 2012-05-29 10:50

What happens if you try this?

Code: Select all

function errore_no_qta(&$tNG) {
  $myerror = "Test";
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg($myerror);
  $myThrowError->setField("");

antonio
Posts: 77
Joined: 2010-09-28 11:48

Re: Throw error: how to use variable instead of text?

Post by antonio » 2012-05-30 11:19

Fred wrote:What happens if you try this?

Code: Select all

function errore_no_qta(&$tNG) {
  $myerror = "Test";
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg($myerror);
  $myThrowError->setField("");
Hi Fred,
this way it works: I get the error "Test".
But how can I read my strings from the lang file this way?
Thanks again, Fred.

Tony

User avatar
Fred
Site Admin
Posts: 491
Joined: 2010-02-15 12:10
Location: Armagh, Northern Ireland
Contact:

Re: Throw error: how to use variable instead of text?

Post by Fred » 2012-05-30 12:55

Good, that just proves my point in my original reply.

Try this

Code: Select all

function errore_no_qta(&$tNG) {
  global $myerror;
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg($myerror['text']);
  $myThrowError->setField("");
Read more about "Variable Scope" in the php manual here http://php.net/manual/en/language.variables.scope.php

antonio
Posts: 77
Joined: 2010-09-28 11:48

Re: Throw error: how to use variable instead of text?

Post by antonio » 2012-05-30 15:10

Fred wrote:Good, that just proves my point in my original reply.
Try this

Code: Select all

function errore_no_qta(&$tNG) {
  global $myerror;
  $myThrowError = new tNG_ThrowError($tNG);
  $myThrowError->setErrorMsg($myerror['text']);
  $myThrowError->setField("");
Read more about "Variable Scope" in the php manual here http://php.net/manual/en/language.variables.scope.php
WoW! Fred,
I'm not a pure programmer (as you are) and I have lot's to learn.
It works.
Thanks-thanks-thanks.
I loose the editing capability of the server behaviour but I can rebuilt it easily before doing any change.

Thanks for the link too.

tony

User avatar
Fred
Site Admin
Posts: 491
Joined: 2010-02-15 12:10
Location: Armagh, Northern Ireland
Contact:

Re: Throw error: how to use variable instead of text?

Post by Fred » 2012-05-30 16:48

"pure" programmer? Not at all.

My mindset was from the day I started using the extensions that it is a good tool for "routine" work. Anything else needs to be researched and coded.
Make sure to comment your code so that you can remember why you did what you did a year from now.

Two important tips when you run into a problem like this is to never assume the value gets assigned and to manually try and do what you expect the application to do.
i.e. echo the variable in the position in your code where the variable needs to be assigned or manually assign a value and work your way back.
Once you pinpoint where you loose scope or the error occur you can start looking for a solution.

Never be afraid to ask a question. But always show the code that you have a problem with and most importantly if you got it working show your solution. Don't just disappear. Other people might learn from your mistake or solution.

Post Reply