Foreach仅计算第一行并丢弃其余行

时间:2016-09-28 12:45:01

标签: php mysql

有预定义的字段,用户也可以创建新字段和插入值,所以我想要的是获取所有列而不是只删除预定义的字段,以便只有用户创建的字段可以保留在查询中,我想得到剩余字段的总和。问题是只有第一行被处理并丢弃剩余的行,因为有许多行符合where条件。

$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;
}

1 个答案:

答案 0 :(得分: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;
 }
}
相关问题