Do/While confusion
Moderator: General Moderators
Do/While confusion
Hey everyone,
I am very new to PHP so I appologize for this question, but I have been butting my head againts the wall for days now. I need to stop my loop before it runs through with a value of 3 for $i. Here is the code so you can see what I am doing.
$Med[0] = $Medication0;
$Med[1] = $Medication1;
$Med[2] = $Medication2;
$Age[0] = $AgewhenStarted0;
$Age[1] = $AgewhenStarted1;
$Age[2] = $AgewhenStarted2;
$Dur[0] = $Duration0;
$Dur[1] = $Duration1;
$Dur[2] = $Duration2;
if ($BirthMeds == 1)
{
$i = 0;
$Medication = $Med[$i];
$AgewhenStarted = $Age[$i];
$Duration = $Dur[$i];
do
{
$validate_query = "INSERT INTO medication (IndividualID, Medication, AgewhenStarted, Duration)
VALUES ('" .$IndividualId. "','" .$Medication. "'," .$AgewhenStarted. ",'" .$Duration. "')";
$result = mysql_query($validate_query);
if (!$result)
{
echo "There was a problem connecting to the database. Please contact an ACGP representative about this issue.";
exit;
}
$i++;
$Medication = $Med[$i];
$AgewhenStarted = $Age[$i];
$Duration = $Dur[$i];
//echo $i;
}while (($Medication != '') || ($i <= 2));
}
I need it to run three times or until $Medication equals "". The error I am getting is an Undefined offset: 3. I know this is because It runs through the loop with a value of 3 for $i which refers to nothing. Can anyone help?
I am very new to PHP so I appologize for this question, but I have been butting my head againts the wall for days now. I need to stop my loop before it runs through with a value of 3 for $i. Here is the code so you can see what I am doing.
$Med[0] = $Medication0;
$Med[1] = $Medication1;
$Med[2] = $Medication2;
$Age[0] = $AgewhenStarted0;
$Age[1] = $AgewhenStarted1;
$Age[2] = $AgewhenStarted2;
$Dur[0] = $Duration0;
$Dur[1] = $Duration1;
$Dur[2] = $Duration2;
if ($BirthMeds == 1)
{
$i = 0;
$Medication = $Med[$i];
$AgewhenStarted = $Age[$i];
$Duration = $Dur[$i];
do
{
$validate_query = "INSERT INTO medication (IndividualID, Medication, AgewhenStarted, Duration)
VALUES ('" .$IndividualId. "','" .$Medication. "'," .$AgewhenStarted. ",'" .$Duration. "')";
$result = mysql_query($validate_query);
if (!$result)
{
echo "There was a problem connecting to the database. Please contact an ACGP representative about this issue.";
exit;
}
$i++;
$Medication = $Med[$i];
$AgewhenStarted = $Age[$i];
$Duration = $Dur[$i];
//echo $i;
}while (($Medication != '') || ($i <= 2));
}
I need it to run three times or until $Medication equals "". The error I am getting is an Undefined offset: 3. I know this is because It runs through the loop with a value of 3 for $i which refers to nothing. Can anyone help?
- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
Re: Do/While confusion
I believe your problem lies within the fact that you do i++ before printing the values of the arrays at that location. If $i is == 2, then when it gets to that line, it will say $i++, so now $i is 3. And since there is no index 3 in the $Med, $Age, $Dur arrays, you will get this error.jdboyer wrote:Hey everyone,
I am very new to PHP so I appologize for this question, but I have been butting my head againts the wall for days now. I need to stop my loop before it runs through with a value of 3 for $i. Here is the code so you can see what I am doing.
Code: Select all
do { //blah blah blah $i++; $Medication = $Medї$i]; $AgewhenStarted = $Ageї$i]; $Duration = $Durї$i]; //echo $i; }while (($Medication != '') || ($i <= 2));
Try this:
Code: Select all
do {
// blah blah blah
$Medication = $Medї$i];
$AgewhenStarted = $Ageї$i];
$Duration = $Durї$i];
$i++; // last thing done in the loop
} while ($Medication != '' || $i <= 2);I worked around it for now. I don't think this is the best way to do it but it works. I just added a dummy value to each array in the [3] position. Then I changed the look to run until $i<=3. This way it runs through the last line but jumps out before it tries to enter the dummy values.
I would love to figure out a better way to do this but it will have to do until then.
Thanks again....
Thanks again....
- Bill H
- DevNet Resident
- Posts: 1136
- Joined: Sat Jun 01, 2002 10:16 am
- Location: San Diego CA
- Contact:
It might work better with the condition test first:
That should run three times, or until it encounters an empty $Med array element.
If the first $Med element is empty the loop won't run at all.
A "do/while" loop will always run at least once.
A "while/endwhile" loop may not run at all, depending on initial conditions.
Code: Select all
$i = 0;
while ($i < 3 && strlen($Medїi]));
{
//blah blah blah
$Medication = $Medї$i];
$AgewhenStarted = $Ageї$i];
$Duration = $Durї$i];
$i++;
//echo $i;
}If the first $Med element is empty the loop won't run at all.
A "do/while" loop will always run at least once.
A "while/endwhile" loop may not run at all, depending on initial conditions.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Because of what you are trying to do it might be better just to use a for loop and cut out a couple of lines of code:
Mac
Code: Select all
for ($i=0; $i < 3 && !empty($Medї$i]); ++$i) {
$Medication = $Medї$i];
$AgewhenStarted = $Ageї$i];
$Duration = $Durї$i];
}Thanks for all your help guys, one last question. With that for loop.... I don't want it to run three times every time. If the user only fills in two out of the three lines then I need it to stop. Will it do this with: for ($i=0; $i < 3 && !empty($Med[$i]); ++$i). I just wasn't aware we could do this in a for loop.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
The best way to find these things out is to test them yourself... But, bsically the loop will run while $i is less than 3 AND $Med[$i] is not empty (equal to an empty string or 0) so it stops as soon as one of those conditions is not satisfied.jdboyer wrote:Will it do this with: for ($i=0; $i < 3 && !empty($Med[$i]); ++$i).
Mac
I always try to keep the testing in a for loop simple. This will do exactly the same, but is more clear to read:
Remind that 'continue' will take the execution to the beginning of the next loop, while 'break' will stop the iteration completely.
Michiel
Code: Select all
for ($i=0; $i<3; ++$i) {
if(empty($Medї$i])) continue;
//....etc......
}Michiel