2016-09-28 82 views
-3

有預定義的字段,用戶也可以創建新的字段並插入值,所以我想要做的是獲取所有列而不是隻刪除預定義的字段所以只有用戶創建的字段可以保留在查詢中,我希望得到剩餘字段的總和。問題是隻有第一行得到處理並丟棄剩餘的行,因爲有許多行滿足where條件。Foreach只計算第一行並丟棄其餘的部分

$added_income = 0; 
$added_income1 = 0; 

// total Salary 
$result868 = mysqli_query($link, "SELECT * FROM $income WHERE Company='".$company."' AND (Month='".$March."' OR Month='".$April."' OR Month='".$May."' OR Month='".$June."' OR Month='".$July."' OR Month='".$August."' OR Month='".$September."' OR Month='".$October."' OR Month='".$November."' OR Month='".$December."') AND Year='".$Year1."'");            

$rows54 = mysqli_fetch_assoc($result868); 

$removeKeys = array('ID', 'Employee_Number', 'Month', 'Year', 'Company', 'Status', 'payment_cycle', 'Payslip_Number', 'House1', 'closing', 'generated', 'Salary','Bonus','Commission','Housing_Allowance','House1','Transport_Allowance','Travel_Allowance','Vehicle_Allowance','Vehicle1','Cellphone_Allowance','Entertainment_Allowance','Company_Car','Medical_Allowance','Leave_payout','Overtime_Hours','Overtime','Cost_to_company'); 

foreach($removeKeys as $key) { 
    unset($rows54[$key]); 
} 

foreach($rows54 as $x => $x_value) {  
    $added_income = $added_income + $x_value; 
} 
+5

好 - 你只取第一行,所以只有第一個*可以*被處理 –

+0

'mysqli_fetch_assoc'獲取__one row__。 –

+4

什麼是'removekeys'垃圾。如果你只想要一個特定的列,然後做一個'SELECT col1,col2,col3'並且查詢也會更快運行 – RiggsFolly

回答

2

那是因爲它是隻讀取第一行

把線

$rows54 = mysqli_fetch_assoc($result868); 

$removeKeys = array('ID', 'Employee_Number', 'Month', 'Year', 'Company', 'Status', 'payment_cycle', 'Payslip_Number', 'House1', 'closing', 'generated', 'Salary','Bonus','Commission','Housing_Allowance','House1','Transport_Allowance','Travel_Allowance','Vehicle_Allowance','Vehicle1','Cellphone_Allowance','Entertainment_Allowance','Company_Car','Medical_Allowance','Leave_payout','Overtime_Hours','Overtime','Cost_to_company'); 

foreach($removeKeys as $key) { 
    unset($rows54[$key]); 
} 

foreach($rows54 as $x => $x_value) {  
    $added_income = $added_income + $x_value; 
} 

while循環中這樣

$added_income = 0; 
$added_income1 = 0; 

// total Salary 
$result868 = mysqli_query($link, "SELECT * FROM $income WHERE Company='".$company."' AND (Month='".$March."' OR Month='".$April."' OR Month='".$May."' OR Month='".$June."' OR Month='".$July."' OR Month='".$August."' OR Month='".$September."' OR Month='".$October."' OR Month='".$November."' OR Month='".$December."') AND Year='".$Year1."'");            

while($rows54 = mysqli_fetch_assoc($result868)){ 

$removeKeys = array('ID', 'Employee_Number', 'Month', 'Year', 'Company', 'Status', 'payment_cycle', 'Payslip_Number', 'House1', 'closing', 'generated', 'Salary','Bonus','Commission','Housing_Allowance','House1','Transport_Allowance','Travel_Allowance','Vehicle_Allowance','Vehicle1','Cellphone_Allowance','Entertainment_Allowance','Company_Car','Medical_Allowance','Leave_payout','Overtime_Hours','Overtime','Cost_to_company'); 

foreach($removeKeys as $key) { 
    unset($rows54[$key]); 
} 

foreach($rows54 as $x => $x_value) {  
    $added_income = $added_income + $x_value; 
} 
} 
+0

不太清楚你在說什麼。向OP顯示完整的重新編碼部分。否則,OP將在一個while循環內執行取指操作 – RiggsFolly

+0

我已編輯我的答案 –

+0

足夠好用於+1嗎? :P –

相關問題