helloise Posted January 26, 2011 Share Posted January 26, 2011 i have the following code that produces a drop down list: <select name="services"> <?php $catcher_names = LpmCatcherPeer::getByAllNames(); foreach($catcher_names as $row) { ?> <option value="<?php echo $row->getName()."/".$row->getId();?>"<?php if($row->getName() === $catcher_name) echo 'selected="selected"';?>><?php echo $row->getName();?></option> <?php } ?> </select> can someone explain the echo 'selected="selected"' part to me please...i got this code off the net and it works perfect but dont know what that part does :( im trying to learn html and php please... thanks Quote Link to comment Share on other sites More sharing options...
haku Posted January 26, 2011 Share Posted January 26, 2011 That sets the option it is contained within as the selected option. So if you have: option 1 option 2 option 3 (with selected="selected") option 4 The third option will be selected on page load. The reason it is written that way is because XHTML (not HTML) requires that all attributes have a value. So the attribute is 'selected' and the value of that attribute is 'selected'. Quote Link to comment Share on other sites More sharing options...
©ode® Posted January 29, 2011 Share Posted January 29, 2011 It basically sets the default option that will be selected. This is becoming more and more important for User Centered Design. There is a UCD design pattern called "Good Defaults." The pattern basically calls for us designers to consider what the best defaults are as we design a form from the user's viewpoint. For example if the drop down was asking the person to select which country they were from and you knew that 90% of your customer base lived in the United States then you would want to set United States as your default option. Quote Link to comment Share on other sites More sharing options...
haku Posted January 29, 2011 Share Posted January 29, 2011 Somewhat alienating to the other 10% of us though. Better to check the user's IP address for location and try to go off that. But even then you will run into some issue with countries that may be close to each other, but have bad relationships, if you get the wrong country from the IP. Quote Link to comment Share on other sites More sharing options...
©ode® Posted January 29, 2011 Share Posted January 29, 2011 The country was just an example of using Good Defaults. There are probably better ways you can get the country like you said, but the design pattern is still essential to UCD. I was just trying to explain the proper application of using "selected." Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.