note 42730 deleted from language.types.array by tomsommer
| From: | tomsommer@php.net | Date: | Sat, 03 Jul 2004 13:14:21 +0000 |
| Subject: | note 42730 deleted from language.types.array by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72414@lists.php.net to get a copy of this message | ||
Note Submitter: denisroy@istop.com
----
I've been working on this little lifesaver for some time now so since it's working good, I
thought I'd share it with he
mass, fairly giving back to the people I got help from.
This can be particularly useful if you need to make drop-down menus (or checkboxes, radio-buttons or
what not) in a form, but using values from MySQL's ENUM or SET fields instead of typing it all.
And combined with a couple clever foreach(), you won't have to update your php scripts every
time you add more fields to your table or more values to your ENUM fields.
I also tried to add as much comments as I could, even the obvious, just so anyone, even the most
neophyte, can understand what the hell's happening quite easily. And for those same neophyte I
say "put that code in a seperate file, add "require_once("<name of
file>");" to the script you wanna be able to call the function from, and follow the
commented exemple and you should'nt have any problems"
<?php
////////////////////////////////////////////////////////////////////
//
// Description: Populate a multidimensional array with arrays
// of a given MySQL table's ENUM or SET field's
// values, no matter how many fields or
// values/fields there is.
// Functions: import_mysql_enumset
// Arguments: Name of the MySQL table you want to import from.
// Returns: $enumset_return
// Example: $enumset = import_mysql_enumset(list_main)
// Assming you had one ENUM field named "categories"
// in the list_main table, you can now see what the
// categories array looks like by doing:
// print_r($enumset[categories]);
// or the third item from array categories:
// print $enumset[categories]['2'];
//
//
////////////////////////////////////////////////////////////////////
?>
<?php
function import_mysql_enumset ($table)
{
// Gather all rows resulting from the SQL command into $result
$sql = "SHOW COLUMNS FROM $table";
$result = mysql_query($sql, $GLOBALS[db]);
// and count the number of rows that were returned.
$rows = mysql_num_rows($result);
// Now, for each row of result, store the information in $data
for ($id = 0; $id < $rows; $id++) {
$data = mysql_fetch_row($result);
// and test if the 2nd array item of the SHOW COLUMNS result row
// ($data[1]) contains ENUM or SET and if it does,
if (ereg("(enum.*|set.*)",$data[1])) {
// since the results looks like
"enum('value1','value2')", we
// strip the enum(' or set(' from the begining and the ') from
// the end of $data[1] and also strip the ',' that seperates
// each values so that we end up with a clean $subarray that
// contains only the values of this particular row's ENUM or
// SET field.
$separator = "','";
$string =
preg_replace("/(enum|set)\('(.+?)'\)/","$2",$data[1]);
$subarray = explode($separator,$string);
// Now, store that particular $subarray within another array
// (multidimensional) nammed $enumset_return, using the Db
// field's name as array key ($data[0])
$enumset_return[$data[0]] = $subarray;
// and if the row doesn't contain ENUM or SET, continue to next row
} else {
continue;
}
}
// Self-explanatory...
mysql_free_result($result);
// Return multidimensional array to calling script
return $enumset_return;
}
?>