Reputation: 18120
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]));
Upvotes: 1
Views: 1833
Reputation: 18120
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);
Upvotes: 0
Reputation: 22810
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);
Upvotes: 0
Reputation: 33163
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.
Upvotes: 1
Reputation: 56697
$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
Upvotes: 0