p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

php_databases thread: populating a drop down list with values that occur in a column

Message #1 by "Kevin Price" <Creature_of_the_id@y...> on Thu, 9 Jan 2003 10:29:37
Well the snippet of code provided is actually a function - so to make it 
work you need to call it from elsewhere.  The method below returns an array 
containing the enum values associated with the field $field.  So for 
example if you have a field called 'title' in table 'user' (where title is 
described by enum('Mr','Mrs','Ms','Dr'...etc...)), you would use the code 
as follows:

$title_values = getEnumVals("user","title");

And then I would step through the $title_values array and pull out each 
possible value for 'title' and do whatever you want with it.


At 04:26 PM 9/01/2003 +0000, you wrote:
>Thanks Vinny,
>I'm pretty new to PHP and I'm teaching myself as I go along and have to
>overcome each problem.
>When I run the script (after having changed the field and table to the
>appropriate names) nothing happens. I dont get any error messages, just a
>nice blank page.
>I'm at work right now, but I'll take a good look at it tonight when I get
>home and see what I am missing.
>Thanks again for your help
> > Try this snippet of code which I adapted from Beginning PHP4 by Wrox.
>can ignore the two lines immediately after "//Debugging".
>//This function looks at the enum values of a given field in a given table
>//  and returns an array of those values
>//Code adapted from p.465 & 467 of WROX - Beginning PHP4 (Choi et al.)
>function getEnumVals($table,$field) {
>          $sql = "SHOW COLUMNS FROM $table LIKE '$field'";
>          $query_result = mysql_query($sql, $this->link_id);
>          $result = mysql_fetch_array($query_result);
>          if(eregi("('.*')", $result["Type"], $match)) {
>                  $enum_str = ereg_replace("'", "", $match[1]);
>                  $enum_options = explode(',', $enum_str);
>                  //Debugging
>                  echo "DB - getEnumVals - enum get successful ... values
>follows: <br>";
>                  foreach ($enum_options as $opt) { echo $opt." <br>"; }
>                  return $enum_options;
>          } else {
>                  return 0;
>          }
>Hope this helps...

  Return to Index