Do/While confusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jdboyer
Forum Newbie
Posts: 9
Joined: Wed Jul 17, 2002 3:30 pm
Location: Calgary, Alberta, Canada

Do/While confusion

Post 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?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: Do/While confusion

Post 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.
jdboyer
Forum Newbie
Posts: 9
Joined: Wed Jul 17, 2002 3:30 pm
Location: Calgary, Alberta, Canada

Post by jdboyer »

I tried that with no luck. Anything else pop out at you?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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 ||
jdboyer
Forum Newbie
Posts: 9
Joined: Wed Jul 17, 2002 3:30 pm
Location: Calgary, Alberta, Canada

Post by jdboyer »

I think that did it...... tell me again what && means please? Thanks again for all your help.
jdboyer
Forum Newbie
Posts: 9
Joined: Wed Jul 17, 2002 3:30 pm
Location: Calgary, Alberta, Canada

Post 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??
jdboyer
Forum Newbie
Posts: 9
Joined: Wed Jul 17, 2002 3:30 pm
Location: Calgary, Alberta, Canada

Post 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....
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

jdboyer wrote:. . . tell me again what && means please?
The && is the same as ||, except it means and, instead of or.
jdboyer
Forum Newbie
Posts: 9
Joined: Wed Jul 17, 2002 3:30 pm
Location: Calgary, Alberta, Canada

Post 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.
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post by gnu2php »

Yes. That should work. It'll stop when the "medication" field is empty.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
galena
Forum Newbie
Posts: 7
Joined: Tue Jul 16, 2002 4:46 am
Location: Netherlands

Post 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
Post Reply