Custom Code to run on every page

where can i set a variable or a session and it should update on every page. I used to have it in global code but it doesn’t seem to work anymore.

i had this code in global code

$_SESSION[“Count20”]= ExecuteScalar(" SELECT COUNT(*) FROM mprce " );

and used it in menu item adding.
but now not working. any help plz.

ExecuteScalar won’t work in “Global Code”. Therefore, change that code to:

$_SESSION[“Count20”] = “0”;

Then, in “MenuItem_Adding” server event, put this following code:

$_SESSION[“Count20”] = ExecuteScalar(“SELECT COUNT(*) FROM mprce” );

Session is not started in global code yet so you cannot use session unless you start it yourself, and your code is actually executing the SQL every page, not for a PHP session (as you suggested by storing it in $_SESSION variable). If the record count of mprce changes between pages, keep your code. Othewise, you may use:

if (!isset($_SESSION[“Count20”])) $_SESSION[“Count20”] = ExecuteScalar(“SELECT COUNT(*) FROM mprce”);

ExecuteScalar actually works in Global Code, and at the start i did put it in the MenuItem_Adding server event but i have more than one query like this and it slows down the application so bad because i think it runs the query on every menu item. I am adding this for counting the number of records in these tables and putting it like number on menu.

i did add session_start(); in the global code and this fixed the problem. is this ok to have here? or does it affect anything?

what is strange is that my code worked perfectly before the new build update.

arbei wrote:

Othewise, you may use:
if (!isset($_SESSION[“Count20”])) $_SESSION[“Count20”] = ExecuteScalar(“SELECT COUNT(*) FROM mprce”);