Assigning global variable based on menu selection

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
cslane
User
Posts: 38

Assigning global variable based on menu selection

Post by cslane »

I have a very large project I'm trying to design involving online sim baseball leagues. There are 16 leagues, and I need somewhere between 35-40 tables per league. So, I'd like to simplify as much as possible by creating an opening menu where the user selects the league he/she wants to work on. That way, I can perform all MySql SELECT commands using both a globally held $currentLg variable and user id, storing everything in 35-40 large tables.

How (and where) can I test for menu item selected? Obviously the menu id's are key, but there seems to be no guidance on retrieving the item selected.

Has anyone done something like this?


mobhar
User
Posts: 11736

Post by mobhar »

You may use "MenuItem_Adding" server event to check the Url of the menu item, and then compare to the current page.

function MenuItem_Adding(&$Item) {
if ($Item->Url == "yourpage.php" && ew_CurrentPage() == "yourpage.php") {
// put your code here ...
}
return TRUE;
}


cslane
User
Posts: 38

Post by cslane »

Thanks for trying to help with this. But, simply put, I cannot make the example you provided work either with .html or with .php. In the Menu Adding event, I use:

if ($Item->Url == "Campbell-Headliner.php" && ew_CurrentPage() == "Campbell-Headliner.php") {
global $lgID;
$lgID = 3;
}

if ($Item->Url == "Doubleday-Headliner.php" && ew_CurrentPage() == "Doubleday-Headliner.php") {
global $lgID;
$lgID = 8;
}

I have the Campbell-headliner.php in the same directory as my project. Execution flow never gets inside the if block. $lgID is always null. Now, please remember that Campbell-Headliner.php is a page I created even though I have placed in on the PHPMaker menu system.

In Menu Rendering, I am doing this:

if ($Menu->IsRoot) { // Root menu
$Menu->AddMenuItem(10000, "Arizona", "Arizona-Headliner.php", -1, "", IsLoggedIn());
$Menu->AddMenuItem(10005, "Ashburn", "Ashburn-Headliner.php", -1, "", IsLoggedIn());
$Menu->AddMenuItem(10010, "Campbell", "Campbell-Headliner.php", -1, "", IsLoggedIn());
$Menu->AddMenuItem(10015, "Doubleday", "Doubleday-Headliner.php", -1, "", IsLoggedIn());
$Menu->MoveItem("Logout", $Menu->Count() - 1); // Move to last
}

In the Recordset_Selecting Event for the table I am doing this:

global $lgID;
ew_AddFilter($filter, "lgID = '$lgID'");

Any ideas why I can't get the result you suggest I should get?


mobhar
User
Posts: 11736

Post by mobhar »

cslane wrote:
Now, please remember that Campbell-Headliner.php is a page I created even though
I have placed in on the PHPMaker menu system.

You should create your own .php file based on the "blankpage.php" file that generated by PHPMaker. Read "Generate a blank page" under "Generate Settings" in the help file.


cslane
User
Posts: 38

Post by cslane »

Ah, I see. That gives a much more uniform look. Thank you! And the code you provided now works. However, I still cannot figure out how to make a global variable work in PHP. This is surely my own programming limitation, as I am used to non-web coding environments. I can assign a global variable and have PHPMaker functions see it without a problem. But I can't seem to change the value of a global variable within a PHPMaker function and keep the new value in memory beyond the confines of that function. How does on change the vlaue of a global variable inside a PHPMaker function and make the new value subsequently available to another function? Your help on this last bit of difficulty would be much appreciated. I want a variable to hold the value of the league a member is currently working on, and this variable needs to change several times during a session. How do others do this?


mobhar
User
Posts: 11736

Post by mobhar »

As long as you have already put the code to declare the global variable in the "Global Code" server event, then it should can be used from any page by declaring it first using the "global" keyword.

// For example, put this following code in "Global Code":
$my_var = "123";

// Then you may use or even override the original value from any page, for example, put the following code in "Page_Render" that belongs to any List page:
global $my_var;
$this->setSuccessMessage($my_var); // <-- you should get "123"
$my_var = "456"; // <-- override the original value ("123") with "456" ...
$this->setSuccessMessage($my_var); // <-- now you should get "456"


cslane
User
Posts: 38

Post by cslane »

Thanks, mobhar. I'm getting a feel for things now. I do have a couple questions I can't seem to find an answer to yet: (1) I want to take a user to another .php page automatically depending on whether they select the checkbox next to a row (I'm using Page_Load and Row_Custom_action in combination, but I can't figure out how to trigger a menu event when needed - is it possible?). And, (2) can one show multiple tables on a single page? I have not been able to figure how to accomplish that either.


mobhar
User
Posts: 11736

Post by mobhar »

(1) Try to use "Page_Redirecting" server event.

(2) At the moment, you can only show one table on a single page. However, you may display multiple tables on a single page if it is a "Master/Multiple-Details"; one Master table with multiple Detail tables.


cslane
User
Posts: 38

Post by cslane »

The Page_Redirecting event is not what I want. That does not allow the list page top show at all but simply jumps to another URL. Depending on whether a user checks the row (checkbox) I want to send them to another page. In other words, ONLY UNDER THAT Condition.

I'll try to investigate the master/detail option. I have been unable to see how it works so far. Evidently, one must set up the tables in a certain way? I can't get the little dialogs to appear when I click 'Modify" in the Master/Detail panel, even when I follow the directions in the help file.


cslane
User
Posts: 38

Post by cslane »

I did finally figure out how to do Master/Detail. Why does the detail view have to show the selected record (like a view) above it? And also, is there no way to change the text on the master/detail button? I don't want it to show the table name.


mobhar
User
Posts: 11736

Post by mobhar »

To redirect to another page, then use "Page_Terminate" server event instead.

Here is the example of code in "Row_CustomAction" server event:

// Row Custom Action event
function Row_CustomAction($action, $row) {

// Return FALSE to abort
if ($action == "updatedesc") {
ew_Execute("UPDATE categories SET Description = CONCAT(Description, ' ',' - Testing') WHERE CategoryID=" . $row["CategoryID"]); // Assume the field ID is of integer type
}
$this->Page_Terminate("productslist.php"); // <-- this will redirect you to the desired page (adjust it with yours) after executing Row_CustomAction server event
return TRUE;
}


mobhar
User
Posts: 11736

Post by mobhar »

cslane wrote:
is there no way to change the text on the master/detail button? I
don't want it to show the table name.

Can you show us which button exactly you want to change its caption and in which page? An example would be better to comprehend.


cslane
User
Posts: 38

Post by cslane »

The following code doesn't work as intended because the Page_Terminate event fires BEFORE the function returns true, which, because of the way AUTOCOMMIT works, means that ew_Execute() does not perform the desired action. I need ew_Execute() to finish its task because it writes the id that I'm using to filter the table on the target page. Any other ideas?

// Row Custom Action event
function Row_CustomAction($action, $row) {
// Return FALSE to abort
if ($action == "id") {
$batID = $row["batID"];
$uid = $row["userid"];
ew_Execute("UPDATE customsettings SET batLive = '$batID' WHERE userid = '$uid'"); // Assume the field ID is of integer type
}
$this->Page_Terminate("Historical_Bat_Standlist.php"); // <-- this will redirect you to the desired page (adjust it with yours) after executing Row_CustomAction server event
return TRUE;
}


mobhar
User
Posts: 11736

Post by mobhar »

It should work as I have tried it from here. Double check your SQL, try this instead (assume userid field is integer type):

ew_Execute("UPDATE customsettings SET batLive = '".$batID."' WHERE userid = ".$uid.""); // Assume the field ID is of integer type


cslane
User
Posts: 38

Post by cslane »

I have things working well. Thanks for a great product. Is there anyway I can include my custom pages (pages made from the blankpage) in the breadcrumb trail? I've looked carefully at blank page and I don't see anywhere to add to the breadcrumb?


mobhar
User
Posts: 11736

Post by mobhar »

The breadcrumb link has not supported for the blankpage.php. It only supports for the generated pages based on the Database Tables/Views.


cslane
User
Posts: 38

Post by cslane »

Ok, I guess we can live with that. I have another coding challenge. I have a very complex application. I'm trying to use a lookup table. The trouble is, I can't filter a lookup table in the same way I do other tables, namely, by adding to the filter in the RecordSet_Selecting event. So, I'm stuck with the built-in table filter offered me in PHPMaker. So, I want to know whether I can use a FUNCTION in the filter string. Can I write my own function to retrieve the dynamic filter I want? I know we can use the global functions like CurrentUserID(), but this is a different question. Thanks for any suggestions you can give. It is imperative that I have a way to adjust my filters dynamically.


mobhar
User
Posts: 11736

Post by mobhar »

Have you tried "Lookup_Selecting" server event?

Read "Server Events and Client Scripts" in the help file for more information and example.


cslane
User
Posts: 38

Post by cslane »

Oops. I should have seen that. Sorry to barrage you with questions. My latest problem is knowing how to check a record that has been updated on the edit page. I can trap grid edit changes easily enough, but how do I check/verify user changes when I'm editing a record as an individual case (I'm using using tabs)? The Row_Updating and Row_Updated events don't seem to be called in this.


mobhar
User
Posts: 11736

Post by mobhar »

If I'm not mistaken about your question, you want to detect the conflict when more than one user are updating a record.

If so, then try to use "Row_UpdateConflict" server event. You have to enable "Check conflicts" under "Table" setup to make it working properly. Read also "Server Events and Client Scripts" in the help file.


Post Reply