PHP function and "0 USING $THIS WHEN NOT IN OBJECT CONTEXT" error

riccardoc 15 Oct, 2020
Hi,

I've read other questions and replies about this error but I'm not able to find the right solution to me.

in actions>load I've added a PHP module with this function:
function test($field) {
if ($this->data("language")=="it-IT") {
echo $this->get("read_data1.db_test_it.', $field,'_it");}
else {
echo $this->get("read_data1.db_test_en.', $field,'_en");}
}
in my view I call it like:
test("MyField");
The idea was to read and print the field "read_data1.db_test_it.MyField_it" if the interface is in Italian and "read_data1.db_test_en.MyField_en" in any other case but when I load my form I have the error "0 USING $THIS WHEN NOT IN OBJECT CONTEXT" and I really can figure out what do I have to do to have it working.

Thanks in advance for any help.
Max_admin 16 Oct, 2020
using $this should be fine, but your code has a quotes problem, please try to fix this first ?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
riccardoc 17 Oct, 2020
Thanks for reply Max, you are right, I made some mess with quotes but I tried this just to take it easy
function test() {
$text = $this->get("read_data1.db_test_it.ID");
echo $text;
}
then I call it like
test();
and the same errore appear, If I write
<?php
$text = $this->get("read_data1.db_test_it.ID");
echo $text;
?>
in views it works, what am I missing?
Max_admin 17 Oct, 2020
what if you test the 2 lines of code without the PHP tags ?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
riccardoc 17 Oct, 2020
If I add a PHP module in actions>load with those two lines (so not creating a function) I have no error but no data too, I also add some text to the echo like
echo $text,'abc';
and then I see abc and nothing more, if I copy the same code in a html module in views (with PHP tags) ID field is correctly displayed.
Max_admin 17 Oct, 2020
I added a PHP action to the first page and used this code:
function test(){
echo 123;
}

test();
that showed 123 on the top of the form which is the expected behavior!
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
riccardoc 18 Oct, 2020
Sorry for my English Max, I couldn't explain what I did.
In Actions>load>PHP module I wrote
function test() {
$text = $this->get("read_data1.db_test_it.ID");
echo $text,'abc';
}
Then in Views>HTML module I wrote
<?php
test();
echo '<br>';
$text = $this->get("read_data1.db_test_it.ID");
echo $text,'abc';
?>
The result when I load the form is
abc
25abc
So ID wasn't read in the function but only in the view code, no error reported.
Thanks again for the support.
Max_admin 18 Oct, 2020
the PHP is above or below the "read_data1" ?
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
riccardoc 18 Oct, 2020
1 Likes
You are right, it was before read_data1, now I moved it below but I got again the "0 USING $THIS WHEN NOT IN OBJECT CONTEXT" error when loading the form.
Max_admin 20 Oct, 2020
Answer
well, it looks like $this will not work inside a function defined in the PHP code, you will need to make the function take $this as a parameter (do not name it $this), and pass $this in the function call!
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Max_admin 20 Oct, 2020
No problem!
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
You need to login to be able to post a reply.