How Use $Var in table->General->Filter

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
jpozo
User
Posts: 10

How Use $Var in table->General->Filter

Post by jpozo »

Hello everyone:
How can I filter a table with a variable?
I have in code-> Server Events-> Global-> All Pages-> Global Code:
$MyVar = 1;

and in Table-> General-> Filter:
"MyField = ". $MyVar

but I get an error
Notice: Undefined variable: MyField in C:\xampp\htdocs\ app ...

Please help.


arbei
User
Posts: 9292

Post by arbei »


jpozo
User
Posts: 10

Post by jpozo »

jpozo wrote:
Hello everyone:
How can I filter a table with a variable?
I have in code-> Server Events-> Global-> All Pages-> Global Code:
$MyVar = 1;

and in Table-> General-> Filter:
"MyField = ". $MyVar

but I get an error
Notice: Undefined variable: MyVar in C:\xampp\htdocs\ app ...

Please help.


kirondedshem
User
Posts: 642

Post by kirondedshem »

How can I filter a table with a variable?
It depends, on when you wnat to filter as well as how and when the value is supposed to be determined beofre its used in the filter. BUT Basically it would be something like

Global code is meant ot house fucntions that can be called anywhere scuh that they can provide a predefined fucntionality, so if your value can always be derived from a fixed set of steps then you can create a fuctiion for it in global and call it later in you filter for example.

  1. got to global code and put
    function GetMyValue()
    {
    //put code to detremine your value
    $MyVar = 1;
    return $MyVar;
    }

  2. then go the filter fucntion, am guesing you want to filter records that show on list page sor record_Selectiong event
    function Recordset_Selecting(&$filter) {
    $my_value = GetMyValue();
    ew_AddFilter($filter, "Field1 = $my_value"); // Add your own filter expression
    }

BUT BUT If your value can not be predefined using specifc steps ie it can be affected overtime as php codes run from time to tiem, kind of like whne you define a variable at the top then its incremented and decremented maybe due to some loop that runs somehwre and then by some other code etc etc, in such a way that you would want to declare it as a global variable. Then its better you use a session variable instead, coz session variable can be accessed from any scope in the code, for example if one of the code that updates it is in page_load

  1. during any of your processes assing your value to a session
    function Page_Load() {
    //just kepp on assigning a value to this session variable to update it
    $_SESSION["my_var"] = 1;
    }

  2. then after access it to use in your filter as follows

function Recordset_Selecting(&$filter) {
$my_value = $_SESSION["my_var"];
ew_AddFilter($filter, "Field1 = $my_value"); // Add your own filter expression
}

ALTERNATIVELY, you can also declare the variable as a normal global php variable, like GLOBAL $my_value;, then you should be able to call it almost anywhere as well, google how to declare php variables globally, then rest is the same

NOTE: record_Selecting runs abit early when a list page is running, so ensure whatever code you have to run to determine the filter value is put maybe in page_load so you can set your value earlier, coz all events after that are run after record_selecting, yet you want by the time you use the variable it has laready been initialised.

NOTE: read about server events in help file to find the right event to use to do your filter as well as right events to use to populate your varibales


jpozo
User
Posts: 10

Post by jpozo »

Dear kirondedshem;

Excellent. Thank you very much.
I have used the information you gave me and it worked perfect for me.


Post Reply