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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)