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 name
but 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
?>
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.