Jump to content

[SOLVED] function output...(works) but i need to know why


tefuzz

Recommended Posts

Ok, so i created a function for my form to populate a list with years for use with birth dates. I added more functionality to it so that when validating and error checking, I could have the function still populate the list, but if a year was selected and there were other errors, not pertaining to the list it would still keep that year selected after reload. my function takes a start/end year and populates the list accordingly from highest to lowest, and checks to see which value is selected...That's where I cant figure out why it actually works.

 

Anwyho, so here is how it is right now. Works flawlessly (might not be the best way, but im a beginner, and it works  ;))

 

function getYears($name, $startYear, $endYear, $tabIndex, $selected) {
$year = $endYear;
echo ("\n\n<select name=\"$name\" id=\"$name\" tabindex=\"$tabIndex\">\n"); 
if(empty($selected)){
	    echo("\n\t<option selected=\"selected\" value=\"\"></option>");
   }
  while ($year >= $startYear) {
   if ($year == $selected) {
	 echo("\n\t<option selected=\"selected\" value=\"$selected\">$selected</option>");
	} else {
	 echo("\n\t<option value=\"$year\">$year</option>");
	}
  $year --;
  }
echo ("\n</select>");
} 

 

 

With it like that, it works 100% no problems, none ever...However, while trying to figure it out, I had coded it a little differently, and it would populate the list with only 2 values, both being blank. here is the only difference in code:

 

echo("\n\t<option selected=\"selected\" value=\"$year\">$year</option>");

 

instead of

echo("\n\t<option selected=\"selected\" value=\"$selected\">$selected</option>");

 

Considering the statement below, shouldnt $year AND $selected work in such a case since they are equal?

if ($year == $selected) {

Um... I don't think selected is supposed to be used in that way. I think it's supposed to be like: <option value="something" selected>Something</option>

 

Try your function like this instead:

 

function getYears($name, $startYear, $endYear, $tabIndex, $selected) {
$year = $endYear;
echo ("\n\n<select name=\"$name\" id=\"$name\" tabindex=\"$tabIndex\">\n"); 
if(empty($selected)){
	    echo("\n\t<option selected=\"selected\" value=\"\"></option>");
   }
  while ($year >= $startYear) {
   if ($year == $selected) {
	 echo("\n\t<option value=\"$year\" selected>$year</option>");
	} else {
	 echo("\n\t<option value=\"$year\">$year</option>");
	}
  $year --;
  }
echo ("\n</select>");
} 

 

Yes, because $year == $selected, it should be the same. However, == can mean equivalent values, too. So having said that, if $year = 0; & $selected = false; - I believe that would have the same translation. So instead of using this operator, perhaps try the identical comparison operator: === (just an extra =)

Um... I don't think selected is supposed to be used in that way. I think it's supposed to be like: <option value="something" selected>Something</option>

 

Yes, because $year == $selected, it should be the same. However, == can mean equivalent values, too. So having said that, if $year = 0; & $selected = false; - I believe that would have the same translation. So instead of using this operator, perhaps try the identical comparison operator: === (just an extra =)

 

I have always used selected="selected"  and it is validating according to the w3c...as for the === thanks, makes more sense to me.

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.