Posted By

brownrl on 07/28/09


Tagged

select php forms max min


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


PHP getNumberSelect


 / Published in: PHP
 

URL: http://www.goingson.be

Quick little function to give you a select box for numbers ranging from min to max. Perfect for birthday date selectors.

  1. // FUNCTION: getNumberSelect
  2. //
  3. // create a select box for selecting number from min - max.
  4. //
  5. // $name -> name of the form element, also the id for css selecting.
  6. // $min -> the starting number
  7. // $max -> the end number
  8. // $default -> the default that you want to be selected when shown.
  9. //
  10. // ex, getNumberSelect( "birthdate_day" , 1 , 31 , 1 )
  11. //
  12. function getNumberSelect( $name , $min , $max , $default = null )
  13. {
  14. $temp = "";
  15.  
  16. // if it is not set from the form then show this one as selcted...
  17. if( ! isset( $_POST[$name] ) )
  18. {
  19. $_POST[$name] = intval( $default );
  20. }
  21.  
  22. // the id is set for styling feel free to mod
  23. $temp .= "<select name=\"".$name."\" id=\"".$name."\" >\n";
  24.  
  25. // we go from min to max, yes yes yes min should be < max
  26. // what fool would call it switched?
  27. while( $min <= $max )
  28. {
  29. $temp .= "<option value=\"".$min."\"";
  30.  
  31. if( intval( $_POST[$name] ) == intval( $min ) )
  32. {
  33. $temp .= " SELECTED ";
  34. }
  35.  
  36. // I always put the spaces there cause all the browsers do weird things
  37. // with select boxes and this seemed to make things ok.
  38. $temp .= ">".$min."&nbsp;&nbsp;</option>\n";
  39. $min++;
  40. }
  41. $temp .= "</select>\n";
  42. return $temp;
  43. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: moussin on July 28, 2009

各国国歌

You need to login to post a comment.