[英]PHP do-while loop not working
So I've been stuck on this do-while loop not working for about two hours. 因此,我被困在这个do-while循环中,大约两个小时都无法正常工作。 I really don't understand why it doesn't work.
我真的不明白为什么它不起作用。 I'm getting this error:
我收到此错误:
Notice: Undefined offset: 9 in /public_html/me/yes.php on line 60
The only problem I think of is that it doesn't accept while loops in a do-while. 我想到的唯一问题是,它不接受do-while中的while循环。
Here is my working code for just the inner while loop: 这是我的内部while循环的工作代码:
$maxcols = $numofcols-1; //=9
$maxrow = count($myarray)-1; //=44
$currentcol=0;
$currentrow=1;
//do {
$collection->insert(array($title[$currentcol] => $myarray[$currentrow][$currentcol]));
$currentcol++;
while ($currentcol<=$maxcols){
$newdata = array('$set' => array($title[$currentcol] => $myarray[$currentrow][$currentcol]));
$currentcol--;
$collection->update(array($title[$currentcol] => $myarray[$currentrow][$currentcol]), $newdata);
$currentcol++;
$currentcol++;
}
$currentrow++;
//} while ($currentrow<=$maxrow);
If I uncomment the two line's "//do {" and "//} while ($currentrow<=$maxrow);" 如果我取消注释两行的“ // do {”和“ //},而($ currentrow <= $ maxrow);” my program dies with the error I mentioned above.
我的程序死于上面提到的错误。 Is there something dead simple as to why it's breaking my code?
关于它为什么破坏我的代码,有没有简单的东西? Thanks in advance
提前致谢
UPDATE: 更新:
Line 60 is: 60行是:
$collection->insert(array($title[$currentcol] => $myarray[$currentrow][$currentcol]));
The inner while loop ends with $currentcol
set to $maxcols
. 内部while循环以
$currentcol
设置为$maxcols
。 The next time you get there $currentcol
has been increased by one (in the previous line) so it's $maxcols+1
, so the while loop doesn't run. 下次到达那里时,
$currentcol
已增加一(在上一行中),因此它是$maxcols+1
,因此while循环不会运行。 The next loop in the outer do loop now tries to access $title[ $maxcols+1 ]
which is undefined. 现在,外部do循环中的下一个循环尝试访问未定义的
$title[ $maxcols+1 ]
。
$currentrow
is initialized to the value 1
. $currentrow
初始化为值1
。 If your array only contains one element, this has index 0
, so you're getting an "index out of bounds" error. 如果您的数组仅包含一个元素,则其索引为
0
,因此您将收到“索引超出范围”错误。
Also, you're determining the number of columns once globally (outside the while
loop for the rows). 另外,您要全局确定一次列数(在
while
循环之外的行数)。 Maybe you should determine the number of columns for each row just before you loop over the columns. 也许您应该在循环浏览列之前确定每一行的列数。
Also, you're not resetting the column index variable to 0, you just keep incrementing. 另外,您不会将列索引变量重置为0,而只是不断增加。 Which causes an error in the second row, as you're starting out with a column index that doesn't exist.
由于您从不存在的列索引开始,因此在第二行中导致错误。
Suspecting that PHP does not allow a while loop within a while loop is nonsense. 怀疑PHP不允许在while循环内进行while循环是胡说八道。 Nested loops are a used very frequently - you just have to get the condition right
嵌套循环经常使用-您只需要使条件正确即可
Try this : 尝试这个 :
$maxcols = $numofcols-1; //=9
$maxrow = count($myarray)-1; //=44
$currentcol=0;
$currentrow=1;
do {
if ($currentcol<9)
{
$collection->insert(array($title[$currentcol] => $myarray[$currentrow][$currentcol]));
$currentcol++;
while ($currentcol<=$maxcols){
$newdata = array('$set' => array($title[$currentcol] => $myarray[$currentrow][$currentcol]));
$currentcol--;
$collection->update(array($title[$currentcol] => $myarray[$currentrow][$currentcol]), $newdata);
$currentcol++;
$currentcol++;
}
$currentrow++;
}
} while ($currentrow<=$maxrow);
Such a simple little mistake, but there was no reset on the $currentcol
这么简单的小错误,但是
$currentcol
上没有重置
Moving the initialization down right below the do statement made it work correctly.Can you tell it's getting late? 将初始化向下移到do语句下方使其可以正常工作。您能知道它来晚了吗? hahaha Thanks all.
哈哈哈谢谢。
$currentrow=1;
do {
$currentcol=0;
$collection->insert(array($title[$currentcol] => $myarray[$currentrow][$currentcol]));
$currentcol++;
while ($currentcol<=$maxcols){
$newdata = array('$set' => array($title[$currentcol] => $myarray[$currentrow][$currentcol]));
$currentcol--;
$collection->update(array($title[$currentcol] => $myarray[$currentrow][$currentcol]), $newdata);
$currentcol++;
$currentcol++;
}
$currentrow++;
} while ($currentrow<=$maxrow);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.