Forum Moderators: coopster
category_select($what, $where, $multiple);
I am thinking that
$what = table name
$where = table field
$multiple = visible options is select menu
If that is correct shouldn't be
this
$table_def = mysql_query("SHOW FIELDS FROM knowledge");
more like this?
$table_def = mysql_query("SHOW FIELDS FROM $what");
Anyway I don't get it maybe you can you help?
<pre>
//////////////////////////////////////////////////
// create a select box to select a category
//////////////////////////////////////////////////
function category_select($what, $where, $multiple ) {
// Category field is an ENUM, so we have to parse it out
$table_def = mysql_query("SHOW FIELDS FROM knowledge");
if (!$table_def) { die ("SQL category selection error: ".mysql_error
()); }
// create a selectbox
// if 'multiple' is greater than 1, it's a multiple-selectbox
echo "<SELECT NAME=\"$what\" size=$multiple"; if ($multiple>1) {
echo " MULTIPLE"; } echo ">\n";
// grab the relevant record
for($i=0;$i<mysql_num_rows($table_def);$i++) {
$row_table_def = mysql_fetch_array($table_def);
if( (strstr($row_table_def["Type"], "enum")) && ($row_table_def
["Field"]== "knowledge_category") ) {
// replace all extraneous characters with ""
$set = str_replace("enum(", "", $row_table_def["Type"]);
$set = str_replace("'", "", $set);
$set = str_replace(")", "", $set);
$set = str_replace("\\", "", $set);
$set = stripcslashes($set);
echo $set;
// blow it up
$set = explode(",", $set);
// for each category, write an HTML option
for($j=0; $j<count($set); $j++) {
echo ' <OPTION VALUE="'.$set[$j].'"';
// selecting the one that matches
if ($set[$j]==$where) {
echo " SELECTED";
}
echo ">".$set[$j]."</OPTION>\n";
}
}
}
echo "</SELECT>\n";
}
</pre>
function mysql_enum_values($tableName,$fieldName)
{
$result = mysql_query("DESCRIBE $tableName");
//then loop:
while($row = mysql_fetch_array($result))
{
//# row is mysql type, in format "int(11) unsigned zerofill"
//# or "enum('cheese','salmon')" etc.
ereg('^([^ (]+)(\((.+)\))?([ ](.+))?$',$row['Type'],$fieldTypeSplit);
//# split type up into array
$ret_fieldName = $row['Field'];
$fieldType = $fieldTypeSplit[1];// eg 'int' for integer.
$fieldFlags = $fieldTypeSplit[5]; // eg 'binary' or 'unsigned zerofill'.
$fieldLen = $fieldTypeSplit[3]; // eg 11, or 'cheese','salmon' for enum.
if (($fieldType=='enum' ¦¦ $fieldType=='set') && ($ret_fieldName==$fieldName) )
{
$fieldOptions = split("','",substr($fieldLen,1,-1));
return $fieldOptions;
}
}
//if the funciton makes it this far, then it either
//did not find an enum/set field type, or it
//failed to find the the fieldname, so exit FALSE!
return FALSE;
}
mysql_enum_values($tableName,$fieldName);
echo "<SELECT NAME=\"Select\" SIZE='1'>";
foreach($fieldOptions as $tmp)
{
echo "<OPTION>$tmp";
}
Output is:
<SELECT NAME="Select" SIZE='1'>
<OPTION>
<OPTION>EnumValue1
<OPTION>EnumValue2
<OPTION>EnumValue3
What it should be:
<SELECT NAME="Select" SIZE='1'>
<OPTION value=1></option>
<OPTION value=2>EnumValue2</option>
<OPTION value=3>EnumValue3</option>
<OPTION value=4>EnumValue4</option>
The first optin is always blank in my enum fields.
Like this:
enum('', 'EnumValue2', 'EnumValue3', 'EnumValue4')