This topic is locked

SQL Query Inquiry

7/17/2016 3:20:13 AM
PHPRunner General questions
J
jefflau author

All,

Really hope someone able gave me a solution where I stuck around few week. I do using query withour getting any error. But it was not calculating at all




Hope someone able solved my problem
SELECT

ID,

apply_date,

applicant_name,

emp_no,

emp_email,

position,

emp_dept,

emp_date,

leave_type,

start_date,

end_date,

(TIMESTAMPDIFF(Day, start_date, end_date)+1) AS total_day,

leave_reason,

DATE_ADD(end_date, INTERVAL 1 DAY) AS return_date,

apply_by,

paid_type,

approval_status,

approval_by,

approval_position,

approval_remark,

ROUND((DATEDIFF(NOW(), emp_date)-186)/365) TIMESTAMPDIFF(YEAR,emp_date,Now()) AS annual_avail,

medical_avail,

ROUND(DATEDIFF(NOW(), emp_date)/7) AS off_avail,

CASE WHEN approval_status = "Approved" AND leave_type = "Annual Leave" THEN (total_day + annual_used) END AS annual_used,

CASE WHEN approval_status = "Approved" AND leave_type = "Medical Leave" THEN (total_day + medical_used) END AS medical_used,

CASE WHEN approval_status = "Approved" AND leave_type = "Off Day" THEN (total_day + off_used) END AS off_used,

(ROUND((DATEDIFF(NOW(), emp_date)-186)/365)
TIMESTAMPDIFF(YEAR,emp_date,Now()) - annual_used) AS annual_balance,

(medical_avail - medical_used) AS medical_balance,

(ROUND(DATEDIFF(NOW(), emp_date)/7) - off_used) AS off_balance

FROM leave_application

WHERE (approval_status is not null) AND (approval_by is not null) AND (approval_position is not null)
I do also try before put the below code to the leave_approval_panel but it also not work the annual_balance, medical_balance and off_balance still empty. May you all advise me how may I solve this issue
global $conn;

$strSQLExists = "select from leave_application where approval_status='Approved' AND leave_type='Annual Leave'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

$values["annual_used"] = $data["total_day"]+$data["annual_used"];

$values["annual_balance"] = $data["annual_avail"]-$values["annual_used"];

}

global $conn;

$strSQLExists = "select
from leave_application where approval_status='Approved' AND leave_type='Medical Leave'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

$values["medical_used"] = $data["total_day"]+$data["medical_used"];

$values["medical_balance"] = $data["medical_avail"]-$values["medical_used"];

}

global $conn;

$strSQLExists = "select * from leave_application where approval_status='Approved' AND leave_type='Off Day'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

$values["off_used"] = $data["total_day"]+$data["off_used"];

$values["off_balance"] = $data["off_avail"]-$values["off_used"];

}

romaldus 7/17/2016

in add page before record added event or edit pagebefore record updated, use something like this:



if($values['field1'] == "blablaba" and $values['field2'] == "blliblibli")
//perform calculation

{

$values['field5'] = $values['field3'] + $values['field4'];

}
elseif($values['field1'] == "blablaba" and $values['field2'] == "blobloblo")
//perform different calculation

{

$values['field5'] = $values['field3'] * $values['field4'];

}
J
jefflau author 7/17/2016

Sir really thank for your help and reply. I do follow you advise but it still not solved my issue. it still no value appear likw the below screenshot


This the problem I facing, I try many way but still remain the same
The below is my before record edited event code. Hope you able gave me some advise
// Use "Add Action" button to add code snippets.

//Pull employee Login information Information and stamped into approval_by field

$values["approval_by"] = $_SESSION["UserName"];
//Pull employee_information Information and autofill in to leave_eform

$rstmp = CustomQuery("select position from employee_information");

$datatmp = db_fetch_array($rstmp);

$values["approval_position"] = $datatmp["position"];
//Pull employee_information Table Information where according login account and autofill in to leave_application

$rstmp = CustomQuery("select position, emp_no, emp_dept, emp_date, annual_avail, medical_avail, off_avail, annual_used, medical_used, off_used, annual_balance, medical_balance, off_balance from employee_information where UserName='".$_SESSION["UserID"]."'");

$datatmp = db_fetch_array($rstmp);

$values["position"] = $datatmp["position"];

$values["emp_no"] = $datatmp["emp_no"];

$values["emp_dept"] = $datatmp["emp_dept"];

$values["emp_date"] = $datatmp["emp_date"];
if($values['approval_status'] == "Approved" and $values['leave_type'] == "Annual Leave")

//perform calculation

{

$values['annual_used'] = ($values['annual_used'] + $values['total_day']);

$values["annual_balance"] = $values["annual_avail"]-$values["annual_used"];

}

elseif($values['approval_status'] == "Approved" and $values['leave_type'] == "Medical Leave")

//perform different calculation

{

$values['medical_used'] = ($values['medical_used'] + $values['total_day']);

$values["medical_balance"] = $values["medical_avail"]-$values["medical_used"];

}

elseif($values['approval_status'] == "Approved" and $values['leave_type'] == "Off Day")

//perform different calculation

{

$values['off_used'] = ($values['off_used'] + $values['off_day']);

$values["off_balance"] = $values["off_avail"]-$values["off_used"];

}

return true;



in add page before record added event or edit pagebefore record updated, use something like this:



if($values['field1'] == "blablaba" and $values['field2'] == "blliblibli")
//perform calculation

{

$values['field5'] = $values['field3'] + $values['field4'];

}
elseif($values['field1'] == "blablaba" and $values['field2'] == "blobloblo")
//perform different calculation

{

$values['field5'] = $values['field3'] * $values['field4'];

}


Sergey Kornilov admin 7/18/2016

I just wanted to say there is a bit of confusion here. According to the first post fields like 'annual_used' are calculated in SQL query. This means do not physically exist in the database and cannot be calculated in events like BeforeAdd or BeforeEdit.
You need to decide if you need those values calculated in SQL query on the fly or to be stored in the database. These are two different approaches.