I'm having a problem updating fields when making changes. Let's assume I have a database with different types of members, call them bronze, silver and gold members. I also have a table that has counts for the different user types so I can bring up a browse screen that shows how many members of each type there are. When a new member is created, I read the membership level from the edit screen. I then run a query on the database to find all the members with that membership level, extract the count and update the relevant table. This is done in the 'after save' section.
Code: Select all
$client_membership_level = $_POST["client-membership_level"];
$sc_query = "SELECT * FROM members WHERE client_membership_level = '$client_membership_level'";
$result = nuRunQuery($sc_query);
$num_rows = mysql_num_rows($result);
$sc_query = "UPDATE membership_levels SET membership_count = '$num_rows' WHERE membership_level = '$client_membership_level'";
nuRunQuery($sc_query);
What I'm currently doing is to create a text field (and table entry) that I use to store a copy of the membership level before update, and then run a similar query for the previous level. This works, but it seems rather cumbersome, so I'm wondering if there is a more elegant solution.
Personally I can think of two solutions:
The first (which I don't think is possible) is to read the current value in the 'before open' section and somehow pass it to the 'after save' section.
The second would be to not bother with reading the relevant value, but to simply iterate over all values of the (finite) list of membership levels. This seems the most elegant solution, as I could simply do this once when I call up the browse screen that shows the counts.
Presumaby, in the sql code of the browse screen, I should be able to select everything from the membership levels, join the members on the membership levels and then update the counts in the membership table based on how many members are in each category, but I'm stumped as to how I would do it. Basically, I need to iterate over the rows in the membership_levels table, but I don't know how to do it. I can successfully do it in a report where I can use a listbox to create the list to iterate over, but I'd really like to be able to generate the counts for a browse screen...
Any help would be greatly appreciated, if all this makes sense.
Dan.