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) {

Link to comment
Share on other sites

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 =)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.