dousha1873 2016-09-28 12:45
浏览 17
已采纳

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

there are pre-defined fields and users can as well create new fields and insert values , so what I want to do is to get all the columns than remove only the pre-defined fields so that only the fields the users has created can remain in the query and i would like to get the sum of the remaining fields . The problem is that only the first row gets processed and discards the remaining rows since there are many rows that meet the where condition.

$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条回答 默认 最新

  • dooso0594 2016-09-28 12:48
    关注

    That is because it is fetching only the first row

    Put the lines

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

    inside a while loop like this

    $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;
     }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败