Page 1 of 1
How do I create for loop for an array (month day year.....)
Posted: Wed Dec 09, 2009 12:19 am
by redrocket1855
I was wondering how to create a loop with in an array keeping my code more concise ....
Code: Select all
[color=#BF0000]$days = array(1 => 1, 2, 3, ... , 30, 31);[/color]
$years = array(2009 => 2009, 2010, 2011, ...);
$hours = array(1 => 1, 2, 3, ... , 23, 24);
$minutes = array(0 => '00', '01', '02', ... , '58', '59');
I know this code for the days will display the numbers 1-31. I do not understand how to put this in to the array of $days..
Code: Select all
<?php
$i=1;
while($i<=31)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
Thanks I am new to PHP and appreciate all the feedback..
Re: How do I create for loop for an array (month day year.....)
Posted: Wed Dec 09, 2009 12:57 am
by pbs
Below is code to display drop down box for year, month and day.
Code: Select all
<select name="dobyy">
<option value="">yyyy</option>
<?php
for($y=(date("Y"));$y>(date("Y")-10);$y--)
{
?>
<option value="<?=$y?>" <?php if (trim($_REQUEST['dobyy']) == $y) { echo " selected "; }?> ><?=$y?></option>
<?php
}
?>
</select>
<select name="dobmm">
<option value="">mm</option>
<?php
for($m=1;$m<13;$m++)
{
?>
<option value="<?=str_pad($m, 2, "0", STR_PAD_LEFT)?>" <?php if (trim($_REQUEST['dobmm']) == str_pad($m, 2, "0", STR_PAD_LEFT)) { echo " selected "; }?> ><?=str_pad($m, 2, "0", STR_PAD_LEFT)?></option>
<?php
}
?>
</select>
<select name="dobdd">
<option value="">dd</option>
<?php
for($d=1;$d<32;$d++)
{
?>
<option value="<?=str_pad($d, 2, "0", STR_PAD_LEFT)?>" <?php if (trim($_REQUEST['dobdd']) == str_pad($d, 2, "0", STR_PAD_LEFT)) { echo " selected "; }?> ><?=str_pad($d, 2, "0", STR_PAD_LEFT)?></option>
<?php
}
?>
</select>
Re: How do I create for loop for an array (month day year.....)
Posted: Wed Dec 09, 2009 1:04 am
by Christopher
redrocket1855 wrote:I know this code for the days will display the numbers 1-31. I do not understand how to put this in to the array of $days..
Code: Select all
$days = array();
$i=1;
while($i<=31)
{
$days[] = $i;
$i++;
}
Although if you checked the manual for
array functions, you would have found this: