Page 1 of 1

Do/While confusion

Posted: Wed Jul 17, 2002 3:30 pm
by jdboyer
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?

Re: Do/While confusion

Posted: Wed Jul 17, 2002 3:39 pm
by protokol
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 &#123;
//blah blah blah
		$i++;
		$Medication = $Med&#1111;$i];
		$AgewhenStarted = $Age&#1111;$i];
		$Duration = $Dur&#1111;$i];
		//echo $i;
		
	&#125;while (($Medication != '') || ($i <= 2));
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.

Try this:

Code: Select all

do &#123;
   // blah blah blah
   $Medication = $Med&#1111;$i];
   $AgewhenStarted = $Age&#1111;$i];
   $Duration = $Dur&#1111;$i];
   $i++; // last thing done in the loop
&#125; while ($Medication != '' || $i <= 2);
Again, this will all depend on what you are trying to do with the $i variable. Just remember that once $i++ is > 2, then the loop stops.

Posted: Wed Jul 17, 2002 4:30 pm
by jdboyer
I tried that with no luck. Anything else pop out at you?

Posted: Wed Jul 17, 2002 4:48 pm
by protokol
Yes, still using my code with the $i++ at the end of the loop, change the while condition to this:

while ($Medication != '' && $i <= 2); // && instead of ||

Posted: Wed Jul 17, 2002 6:14 pm
by jdboyer
I think that did it...... tell me again what && means please? Thanks again for all your help.

Posted: Wed Jul 17, 2002 6:28 pm
by jdboyer
The only problem now is that for some reason it is only running the loop twice. So I only get the first two rows of data and then it stops??

Posted: Wed Jul 17, 2002 6:42 pm
by jdboyer
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. :roll: I would love to figure out a better way to do this but it will have to do until then.

Thanks again....

Posted: Wed Jul 17, 2002 8:13 pm
by Bill H
It might work better with the condition test first:

Code: Select all

$i = 0;
while ($i < 3 && strlen($Med&#1111;i])); 
&#123; 
     //blah blah blah 
      $Medication = $Med&#1111;$i]; 
      $AgewhenStarted = $Age&#1111;$i]; 
      $Duration = $Dur&#1111;$i]; 
      $i++; 
     //echo $i; 
&#125;
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.

Posted: Thu Jul 18, 2002 2:07 am
by twigletmac
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:

Code: Select all

for ($i=0; $i < 3 && !empty($Med&#1111;$i]); ++$i) &#123;
      $Medication = $Med&#1111;$i]; 
      $AgewhenStarted = $Age&#1111;$i]; 
      $Duration = $Dur&#1111;$i]; 
&#125;
Mac

Posted: Thu Jul 18, 2002 4:05 am
by gnu2php
jdboyer wrote:. . . tell me again what && means please?
The && is the same as ||, except it means and, instead of or.

Posted: Thu Jul 18, 2002 8:37 am
by jdboyer
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.

Posted: Fri Jul 19, 2002 7:00 pm
by gnu2php
Yes. That should work. It'll stop when the "medication" field is empty.

Posted: Sat Jul 20, 2002 5:14 am
by twigletmac
jdboyer wrote:Will it do this with: for ($i=0; $i < 3 && !empty($Med[$i]); ++$i).
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.

Mac

Posted: Sun Jul 21, 2002 6:26 pm
by galena
I always try to keep the testing in a for loop simple. This will do exactly the same, but is more clear to read:

Code: Select all

for ($i=0; $i<3; ++$i) &#123;
    if(empty($Med&#1111;$i])) continue; 
    //....etc......
&#125;
Remind that 'continue' will take the execution to the beginning of the next loop, while 'break' will stop the iteration completely.

Michiel