Jump to content

[SOLVED] no august in my drop down, what the...?


Lodius2000

Recommended Posts

so just noticed that my dropdown menu on my website does not contain the moth of august, it defaults to january

 

heres my code adapted from several include files but i managed to to recreate the problem

 

<?php

$months = array(01 => 'January', 02 => 'February', 03 => 'March', 04 => 'April', 05 => 'May', 06 => 'June', 07 => 'July', 08 => 'August', 09 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');

//defaults is an array because in my real script it contains the rest of the parts of the date: hours, mins, year...
$defaults = array('month'  => date('n'));


function input_select($element_name, $selected, $options, $multiple = false){
//print out the <select> tag
print '<select name="' . $element_name;
// if multiple choices are permitted, add the multiple attribute
// and add a [] to the end of the tag name
if ($multiple){ print '[]" multiple="multiple'; }
print '">';

//set up the list of things to be selected
$selected_options = array();
if ($multiple) {
	foreach ($selected[$element_name] as $val){
		$selected_options[$val] = true;
	}
} else {
	$selected_options[ $selected[$element_name] ] = true;
}

//print out the <option> tags
foreach ($options as $option=>$label) {
	print '<option value="' . htmlentities($option) . '"';
	if ($selected_options[$option]){
		print ' selected="selected"';
		}
	print '>' . htmlentities($label) . '</option>';
}
print '</select>';
}


print '<form>';
input_select('month',$defaults, $months);
print "</form>";
?>

 

so why no august?

The leading "0" in your $months array causes the indexes to be treated as octal.

 

Also, your code is generating undefined index messages (possible due to the octal treatment of the indexes.)

 

Edit: Once you remove the leading "0" in the $months array, you will get an August value. The undefined index messages are because you are referencing $selected_options[$option] for each index value, but only the one(s) that you have set will exist. You need to add an isset() to avoid generation of any error messages (even if your error_reporting level or display_errors setting won't cause the undefined index messages to be output or logged, php must still go through the error response code every time it encounters an index that does not exist, thereby slowing down your loop by about 20x what it should execute at.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.