This topic is locked
[SOLVED]

While Loop in another While Loop not working

11/17/2023 4:39:14 AM
PHPRunner General questions
P
PK author

Is there a reason a while loop nested in another while loop wont work.
The idea is to select some employees to be evaluated (first_set) then click a button to a pop up with a list of all employees where I will select some employees (second_set) to evaluate the first set. So I loop through the second_set and for each record I loop through the second_set to create an sql string.

But for some reason, after it goes through the first while-loop and then goes through the second while-loop , it exits the whole loop. Whis is not right.
It should go back to the first while-loop to get the next record and repeat the process with the second while-loop and so on until the first while-loop is ended.

Is there something I'm doing wrong?
I have this code:

global $conn;
$count=0;
$sql = "insert into appraisal_360_data_header (Appraiser, Appraisee, GroupID)";
//get employees selected to be evaluated
$strSQL = "SELECT EmployeeNumber, EvaluationGroup FROM employees WHERE SelectedForEval = 1";
$rs = db_query($strSQL,$conn);
//loop through the employees selected to evaluate the employees selected for evaluation
while($data = $button->getNextSelectedRecord())
{
$appraisor = $data['EmployeeNumber'];
while ($dataAppraisees = db_fetch_array($rs))
{
$appraisee = $dataAppraisees['EmployeeNumber'];
$GroupID = $dataAppraisees['EvaluationGroup'];
if ($count==0) {
$sql.= "VALUES ('".$appraisor."', '".$appraisee."', ".$GroupID.")";
} else {
$sql.= ", ('".$appraisor."', '".$appraisee."', ". $GroupID.")";
}
$count++;
}
}

if ($count>0) {
db_exec($sql);
$result["txt"]=$sql; // I put this here to see the $sql string
//unselect employees selected for evaluation
$sql1 = "update employees set SelectedForEval = 0 WHERE SelectedForEval = 1";
db_exec($sql1);
}
unset($rs);
unset($_SESSION["eval_selected_name"]);
fhumanes 11/17/2023

img alt

In the first element the cycle of the "result set" of the Query ends.

To start it again, you have to return to the Query.

Regasrds,
fernando

C
cristi 11/18/2023

...you are fetching the array from the inner loop query only once since the outer loop executed. You're not running the db_fetch_array query every time though your $data loop - it's still at the end of the query results when you start every iteration after the first. So, take all the results from $data1, put them into an array, and iterate over that for each of your results.
You could do it like this (without nested while loops -I personnally don't like using them but it is only my preference) - just an general example so you get the idea:

//First lool
$results= array();
while ($data1=db_fetch_array($query))
{
$results[]=$data1;
}

//Second loop:
while ($data=$button->getNextSelectedRecord())
{
//do your thing
foreach ($results as $result)
{
//do your thing with $result
}
}

OR you could use mysqli_fetch_all() instead of db_fetch_array inner loop and have all the query results in an array, remove the inner while loop and do a foreach loop on the fetch_all() returned array inside the outer (single now)while loop....

P
PK author 11/19/2023

Thank you both.
I used cristi's 2 separate loops method and it works great!

Thanks again