Posted By

clapfouine on 12/12/07


Tagged

mysql php array enum


Versions (?)

Enum to Array / SQL to PHP


 / Published in: PHP
 

adapted from http://www.gotocode.com/art.asp?art_id=275&

  1. function LoadTypeValues($table, $column)
  2. {
  3. global $db;
  4. // Create a SQL Query to get the Columns Type information,
  5. // Open a database connection, execute the query, and retrieve
  6. // the result.
  7.  
  8. $sql = "show columns from $table like '$column'";
  9.  
  10. // Get the Type information, Remove "xxx(" from the front
  11. // and ")" from the end. Split the comma delimited values
  12. // into an array.
  13. $requete = mysql_query($sql, $db);
  14. while($enum = mysql_fetch_array($requete)) {
  15. $enum = $db->f('Type');
  16. $off = strpos($enum,"(");
  17. $enum = substr($enum, $off+1, strlen($enum)-$off-2);
  18. $values = explode(",",$enum);
  19.  
  20. // For each value in the array, remove the leading and trailing
  21. // single quotes, convert two single quotes to one. Put the result
  22. // back in the array in the same form as CodeCharge needs.
  23.  
  24. for( $n = 0; $n < Count($values); $n++) {
  25. $val = substr( $values[$n], 1,strlen($values[$n])-2);
  26. $val = str_replace("''","'",$val);
  27. $values[$n] = array( $val, $val );
  28. }
  29. }
  30. // return the values array to the caller
  31. return $values;
  32. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: maietta on July 30, 2010

I am a big fan of jQuery and have actually been able to grasp the concepts behind JavaScript... so when it came time to implement the jQuery plugin called Jeditables for my new php framework Commnetivity, i needed a way to dynamically generate the forms and also validate them.... using the concept of this snipplr entry, i was able to finally parse that dreaded enum value for my selectors. Thank you clapfouine for the contribution and Thank you snipplr.com for hosting it!

You need to login to post a comment.