Page 1 of 1

Newbie: Question regarding Arrays

Posted: Wed Jul 23, 2008 10:55 am
by dkawas
I am brand new to PHP (after just learning html and CSS) and am following along in a book on writing php.

The code below should produce a Month, Day and Year drop down box. While the Day and Year are fine, the Months are displayed without a dropdown. I've been going over this for over an hour and can't figure it out. Please help.



<?php


// This script makes three pull-down menus

// For and HTML form: months, days, years.

// Make the Months array:

$months = array (1 => 'January' , 'February' , 'March' , 'April' , 'May' , 'June' , 'July' , 'August' , 'September' , 'October' , 'November' , 'December');



// Make the days and years arrays:

$days = range (1, 31);

$years = range (2008, 2018);


// Make the Months pull-down menu:

echo '<select name="month">';

foreach ($months as $key => $value) {

echo "<option value=\"$key\"> $value</option>\n";

}

echo '</select>';

// Make the Days pull-down menu:


echo '<select name="day">';

foreach ($days as $value) {

echo "<option value=\"$value\"> $value</option>\n";
}


echo '</select>';



// Make the Years pull-down menu:


echo '<select name="year">';

foreach ($years as $value) {

echo "<option value=\"$value\"> $value</option>\n";
}

echo '</select>';


?>

</form>

</body>
</html>

Re: Newbie: Question regarding Arrays

Posted: Wed Jul 23, 2008 11:45 am
by mabwi
I just ran this, and it's working perfectly.

Try it again.

Code: Select all

 
<?php
 
 
// This script makes three pull-down menus
 
// For and HTML form: months, days, years.
 
// Make the Months array:
 
$months = array (1 => 'January' , 'February' , 'March' , 'April' , 'May' , 'June' , 'July' , 'August' , 'September' , 'October' , 'November' , 'December');
 
 
 
// Make the days and years arrays:
 
$days = range (1, 31);
 
$years = range (2008, 2018);
 
 
// Make the Months pull-down menu:
 
echo '<select name="month">';
 
foreach ($months as $key => $value) {
 
echo "<option value=\"$key\"> $value</option>\n";
 
}
 
echo '</select>';
 
// Make the Days pull-down menu:
 
 
echo '<select name="day">';
 
foreach ($days as $value) {
 
echo "<option value=\"$value\"> $value</option>\n";
}
 
 
echo '</select>';
 
 
 
// Make the Years pull-down menu:
 
 
echo '<select name="year">';
 
foreach ($years as $value) {
 
echo "<option value=\"$value\"> $value</option>\n";
}
 
echo '</select>';
 
 
?>
 
</form>
 
</body>
</html>
 

Re: Newbie: Question regarding Arrays

Posted: Wed Jul 23, 2008 1:48 pm
by dkawas
I think i may have figured it out. Could it be because my host doesn't support PHP6?

Re: Newbie: Question regarding Arrays

Posted: Wed Jul 23, 2008 3:28 pm
by mabwi
dkawas wrote:I think i may have figured it out. Could it be because my host doesn't support PHP6?
I'm not running PHP 6 either. I just copied that straight in to a file and ran it.

Re: Newbie: Question regarding Arrays

Posted: Wed Jul 23, 2008 3:56 pm
by dkawas
Not sure what's up. My months come up as: January February March April May June July August September October November December and not as a drop-down.

Re: Newbie: Question regarding Arrays

Posted: Wed Jul 23, 2008 4:00 pm
by JB4
Just tested it, Mine works too.

Re: Newbie: Question regarding Arrays

Posted: Wed Jul 23, 2008 4:39 pm
by Apollo
What kinda browser do you use? Depending on its nitpickyness about correct html, you may want to echo "<html><body><form>"; at the top of your php file (or just put it before the <?php opening tag).

Re: Newbie: Question regarding Arrays

Posted: Wed Jul 23, 2008 4:50 pm
by manixrock
View the page in the browser then view the page's source and paste it here.

In Internet Explorer you can go to the menu View > Source.
In Firefox you can go to the menu View > Page Source.

Re: Newbie: Question regarding Arrays

Posted: Wed Jul 23, 2008 4:59 pm
by dkawas
I got it. Not sure how, but it must've been a typo. :banghead:

Before the php code I had, or at least think I had "<form action="calendar.php" method="post">"

Thinking it had something to do with the form tag I deleted all but "<form>". And it worked. Then I typed back in the attributes and values and it still works.

Not sure what the deal was, but thanks to all :D