$i=0;
while($i <= $countstud)
{
$j=0;
while($j < $counteval)
{
if($i < $countstud)
{
$connection=Yii::app()->db;
$sql='update exam_answers
set eval_id=:eval
where student_id=:sid
and exam_id=:eid';
$command1=$connection->createCommand($sql);
$command1->bindParam(":eval",$eval[$j],PDO::PARAM_STR);
$command1->bindParam(":sid",$studid[$i],PDO::PARAM_STR);
$command1->bindParam(":eid",$examid,PDO::PARAM_STR);
$command1->execute();
}
$j++;
$i++;
}
}
Here, my $countstud is 4 and $counteval is 3. I want to assign
$stud[0] = $eval[0],
$stud[1] = $eval[1],
$stud[2] = $eval[2],
$stud[3] = $eval[0],
$stud[4] = $eval[1]
and so on.. till the $stud
exhausts.
However, here, It doesnt work that way, it runs once and exits the loop.. Please help.
Maybe the $j++ has to go after the closing bracket of the second while loop? In the first loop you are checking against $countstud but you are incrementing i the second while loop.
I don't think you need to use nested loops at all. Simply reset $j
when appropriate:
<?php
$i=0;
$j=0;
while($i < $countstud)
{
$connection=Yii::app()->db;
$sql='update exam_answers
set eval_id=:eval
where student_id=:sid
and exam_id=:eid';
$command1=$connection->createCommand($sql);
$command1->bindParam(":eval",$eval[$j],PDO::PARAM_STR);
$command1->bindParam(":sid",$studid[$i],PDO::PARAM_STR);
$command1->bindParam(":eid",$examid,PDO::PARAM_STR);
$command1->execute();
$i++;
$j++;
if($j == $counteval)
{
$j=0;
}
}
?>
I would replace the first while with a foreach and the second while with a infinite iterator.
$connection=Yii::app()->db;
$sql='update exam_answers
set eval_id=:eval
where student_id=:sid
and exam_id=:eid';
$iterator = new InfiniteIterator(new ArrayIterator($eval));
foreach($studid as $sid)
{
$c=$connection->createCommand($sql);
$c->bindValue(":eval", $iterator->current(), PDO::PARAM_STR);
$c->bindValue(":sid", $sid, PDO::PARAM_STR);
$c->bindValue(":eid", $examid, PDO::PARAM_STR);
$c->execute();
$iterator->next();
}
}
I did a combo of all the answers and it worked! Thanks everyone.
$connection=Yii::app()->db;
$sql='update exam_answers
set eval_id=:eval
where student_id=:sid
and exam_id=:eid';
$command1=$connection->createCommand($sql);
$j=0;
foreach($studid as $sid)
{
$command1->bindParam(":eval",$eval[$j],PDO::PARAM_STR);
$command1->bindParam(":sid",$sid,PDO::PARAM_STR);
$command1->bindParam(":eid",$examid,PDO::PARAM_STR);
$command1->execute();
$j++;
if($j == $counteval)
{
$j=0;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.