1

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]));

7
  • 2
    What is line #60 in the above code? And the notice you posted above should not cause the program to die. Commented Apr 2, 2012 at 9:17
  • It's the very first line after the "do{" statement. Sorry! Commented Apr 2, 2012 at 9:18
  • @SalmanA Obvious question... That's what I was about to ask myself... Commented Apr 2, 2012 at 9:18
  • 1
    Well what the error message says is that you're trying to get $title[9], which as you just said does NOT exist... What about trying to see what's wrong with your currentCol variable.... Maybe just add a check if ($currentCol<9) { then and only then, do... what's following... Commented Apr 2, 2012 at 9:22
  • 1
    Have you ever heard about foreach? This seems a C++ way of handling arrays. Take a look at that. Commented Apr 2, 2012 at 11:04

4 Answers 4

1

The inner while loop ends with $currentcol set to $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. The next loop in the outer do loop now tries to access $title[ $maxcols+1 ] which is undefined.

Sign up to request clarification or add additional context in comments.

Comments

0

$currentrow is initialized to the value 1. If your array only contains one element, this has index 0, so you're getting an "index out of bounds" error.

Also, you're determining the number of columns once globally (outside the while loop for the rows). 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. 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. Nested loops are a used very frequently - you just have to get the condition right

Comments

0

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);

2 Comments

It does get rid of the error message but I very much doubt the script will work the way OP wants it to.
@Juhana I'm not sure about that too, coz it seems there's simply some logical error with the code. But let's just see...
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.