Forum Moderators: coopster

Message Too Old, No Replies

for loops

         

JoJoJohnson

4:13 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



Ive not got an actual problem, just a simple query.

I am designing a form, and one of the fields is for date of birth.

I am having 3 select boxes. one for day, one for month and one for year.

im wanting to use a for loop to generate the options. so for the day i would need a for loop that goes from 1 to 31.

here is the current code i am using, which works fine. note this line (<?PHP for($day=1; $day<32; $day++) {?>)

<select name="dob_day" tabindex="4">
<option value="">00</option>
<?PHP for($day=1; $day<32; $day++) {?>
<option value="<?PHP echo sprintf("%02d",$day);?>"><?PHP echo sprintf("%02d",$day);?></option>
<?PHP }?>
</select>

however, when i ran this code. note this line (<?PHP for($day=1; $day=31; $day++) {?>)

<select name="dob_day" tabindex="4">
<option value="">00</option>
<?PHP for($day=1; $day=31; $day++) {?>
<option value="<?PHP echo sprintf("%02d",$day);?>"><?PHP echo sprintf("%02d",$day);?></option>
<?PHP }?>
</select>

the page took forever to load.

what is the reason for $day<32 being a lot quicker to complete than $day=31?

Salsa

5:27 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



One way is to make an array for how many days are in each month

$days_in_months = array('Jan' => 31, 'Feb' => 28, ...); 
if ($year == 2004 ¦¦ $year == 2008, ...) $days_in_months['Feb'] = 29;

(For the index, use whatever values you use for your $month variable.)

Then:

for($day=1; $day <= $days_in_months[$month]; $day++) { 
...
}

Salsa

5:33 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



In rereading, I misunderstood you question, but maybe my bit will help a little.

Gotta go.

I wish you well

dreamcatcher

6:43 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try:

for($day=1; $day<31; $day++)

Your second expression is contradicting the first.

:)