2016-09-28 134 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 –

相关问题