duanmianhong4893 2018-08-27 06:52
浏览 6
已采纳

代码仅输出第1行

I want to get employee name from $projSDE_1 that consists of emp ID for each row in the for loop below from function func_GetEmpName($empID).For the code below it should display 10 rows with $projSDE_1's namebut the problem is the code outputs only the 1st row .Why the other 9 rows could not be display?Im already stuck with this problem for 1 week.If i remove the function it will display 10 rows .All help appreciated.

<?php
for($i=0; $i<db_rowcount();$i++){
  //loop through every result set
  $projID=db_get($i,0);
  $projNo=db_get($i,1);
  $projDesc=db_get($i,2);
  $projSDE_1=db_get($i,8);//1st Services Development Engineer

  $projSDE_1_name = func_GetEmpName($projSDE_1);//get name from function


}//endfor

  function func_GetEmpName($empID) {
  $sqlEmp="select EmpID,LastName2_c from empbasic WHERE EmpID= '".$empID."'";
  db_select($sqlEmp);
  $rowcount=db_rowcount();
  if(db_rowcount()>0){
    for($f=0;$f<count($empID);$f++){
      $empID=db_get($f,0);
      $empName=db_get($f,1);
    }
  }
  return $empName;
} // function
?>

this the output from the code

need to display like this

removed function

  • 写回答

1条回答 默认 最新

  • doulangyu9636 2018-08-27 09:42
    关注

    As CBroe said, you're overwritting $empName in each loop. Put the variable in an array to avoid that.

    <?php
    
    $projSDE_1_IDs = [];
    
    for($i=0; $i<db_rowcount();$i++){
        //loop through every result set
        $projID=db_get($i,0);
        $projNo=db_get($i,1);    
        $projDesc=db_get($i,2);
        $projSDE_1=db_get($i,8);//1st Services Development Engineer
    
        array_push($projSDE_1_IDs, $projSDE_1) // Store all `$projSDE_1` in an array
    
    }//endfor
    
    $projSDE_1_names = func_GetEmpName($projSDE_1_IDs); // Pass the IDs' array to the function
    
    function func_GetEmpName($empIDs) {
    
        $names = [];
    
        foreach($empIDs as $empId){
    
            $sqlEmp="select EmpID,LastName2_c from empbasic WHERE EmpID= '".$empID."'";
    
            db_select($sqlEmp);
    
            $rowcount=db_rowcount();
    
            if(db_rowcount()>0){
    
                for($f=0;$f<count($empID);$f++){
                    $empID=db_get($f,0);
                    $empName=db_get($f,1);
    
                    array_push($names, $empName)
                }
            }
    
        }
    
        return $names;
    
    } // function
    
    var_dump($projSDE_1_names) // Display the array to see if you get all the correct data 
    

    Not very sure for the foreach part in the function but yb modifying little thing by your own if it's not working, you should be able to do what you want. I think the logic is here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算