Posted By

brownrl on 07/30/09


Tagged

select php forms box


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jseltzer
umang_nine


PHP getSelectFromTable


 / Published in: PHP
 

URL: http://www.goingson.be

This is one of my favs to use and reuse. Essentially, we make a select box on what is in the table filled values/labels.

Download the ISO countries list to make a country table and Bam! with this function you got a country select box... Also great for foreign keys...

Other Examples:

auto_maker, auto_maker_id color_name, color_id

  1. // FUNCTION getSelectFromTable
  2. //
  3. // Make a select box based on value and labels in a table.
  4. //
  5. // $name -> the name of the form element
  6. // $table -> the table to base off of
  7. // $labels -> Which field has the labels: Ford, GM, Fiat
  8. // $values -> field has the values: 1,37,99
  9. // $where -> option where clause that you can put in
  10. // $orderby -> default ordering is on the label field but you can override
  11. // $default -> What should the first option be if needed.
  12. // array x['label'] = "Make a Choice" x['value'] = ""
  13. // leave blank for blank...
  14.  
  15. function getSelectFromTable( $name , $table , $labels , $values , $where = -1 , $orderby = -1 , $default = null )
  16. {
  17.  
  18. // if we have a where clause use it if not nevermind
  19. if( $where == -1 || $where == null )
  20. {
  21. $where = "";
  22. }
  23. else
  24. {
  25. $where = " WHERE ".$where;
  26. }
  27.  
  28. // if we have an order by use it other wise order by label field
  29. if( $orderby == -1 || $orderby == null )
  30. {
  31. $orderby = " ORDER BY ".$labels." ASC";
  32. }
  33. else
  34. {
  35. $orderby = " ORDER BY ".$orderby;
  36. }
  37.  
  38.  
  39. $temp = "";
  40.  
  41. // get the labels and the values from the table
  42. $q = "SELECT ".$labels." as label, ".$values." as value FROM ".$table."".$where.$orderby;
  43.  
  44. $qr = dbquery( $q );
  45.  
  46. // begin building the select box
  47.  
  48. // you can css on the #id if you want
  49. $temp .= "<select class=\"form_element\" name=\"".$name."\" id=\"".$name."\">";
  50.  
  51. // build the first option if this is null then we just a get a blank
  52. // one
  53. $temp .= "<option value=\"".$default['value']."\">".$default['label']."</option>\n";
  54.  
  55.  
  56. // roll through the result set building the options
  57. while( ! $qr->EOF )
  58. {
  59. $temp .= "<option value=\"".$qr->fields['value']."\"";
  60.  
  61. // lets make this one selected if it was selected before
  62. if( $_POST[$name] == $qr->fields['value'] )
  63. {
  64. $temp .= " selected";
  65. }
  66.  
  67. // sorry for the entities and such but when you live
  68. // amongst french speaking folks they tend be anal about
  69. // their precious special charecters théy rêàlly suck còck!
  70.  
  71. $temp .= " >".htmlentities( utf8_decode($qr->fields['label']) )."</option>\n";
  72.  
  73. $qr->MoveNext();
  74. }
  75.  
  76. $temp .= "</select>\n";
  77.  
  78. return $temp;
  79. }

Report this snippet  

You need to login to post a comment.