duanbi2760 2012-04-02 09:16
浏览 237
已采纳

PHP do-while循环不起作用

So I've been stuck on this do-while loop not working for about two hours. 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.

Here is my working code for just the inner while loop:

$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);" 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:

$collection->insert(array($title[$currentcol] => $myarray[$currentrow][$currentcol]));

  • 写回答

4条回答 默认 最新

  • duanhe6799 2012-04-02 09:28
    关注

    The answer:

    Such a simple little mistake, but there was no reset on the $currentcol

    Moving the initialization down right below the do statement made it work correctly.Can you tell it's getting late? 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭