I had an issue where I need to enter Temperature data either in Imperial unit (i.e. Fahrenheit degree (F) or Metric unit (i.e. Celsius for temperature (C) . I could create two separate fields one for Metric unit & another for Imperial Unit. And I can make those fields enable/disable based on unit type by using JavascriptOnLoad events. But I don’t want to use two different fields for two different unit rather only one field for efficient database performance.
I want user can enter temperature data in either format (Celsius or Fahrenheit) into a single field. After submit Fahrenheit temp data should be converted into Metric unit (Celsius) on the fly and entered into the database table and when these data has to retrieve it should be similarly converted into its native unit Fahrenheit. Because I have to build application that should be used by both European users (Metric system) as well as USA users (Imperial system).
Solution as follows:
Step 1: Create a separate “Unit Profile” table in the db and add “1 Metric” for European users & “2 Imperial” for USA users as unit profile.
Step 2: Add a separate field “Unit_Profile (INT)” into the Users Table. So that when a new user added into the users table Admin can align that new user either “1 Metric” or “2 Imperial” profile based on new user’s Country of residence.
Step 3: Set the “Unit_Profile” field in Users Add/Edit form as Lookup field from “Unit Profile” Table in Phprunner Editor.
Step 4: Set SESSION variable on After Successful Login event page as follows:
$_SESSION["user_id"]=$data["unit_profile"];
this will save unit_profile data into the SESSION what we can pass to the target page “Add/Edit_data.php”.
Step 5: write “before record added” event on “data_add” page as follows”
if ($_SESSION["user_id"]=="2" && $values["temp"]=='true')
$values["temp"]=($values['temp']-32)*0.556;
else
$values['temp']== 'true';
After submit new temp data from data_Add form the above event will call the “Unit_Profile_id (i.e.2 for Imperial)” from the SESSION and used in the formula to convert Fahrenheit temp into Celsius temp and inserted into the db table. If the user enters Celsius temp data then no action will be happened and the Celsius temp data by default inserted into the db table. Similarly we have to use in Edit_data page event and reverse calculation in query page for reporting.
That’s it.