Newbie: Question regarding Arrays
Moderator: General Moderators
Newbie: Question regarding Arrays
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>
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
I just ran this, and it's working perfectly.
Try it again.
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
I think i may have figured it out. Could it be because my host doesn't support PHP6?
Re: Newbie: Question regarding Arrays
I'm not running PHP 6 either. I just copied that straight in to a file and ran it.dkawas wrote:I think i may have figured it out. Could it be because my host doesn't support PHP6?
Re: Newbie: Question regarding Arrays
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
Just tested it, Mine works too.
Re: Newbie: Question regarding Arrays
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
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.
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
I got it. Not sure how, but it must've been a typo.
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
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