Forum Moderators: coopster

Message Too Old, No Replies

Need help with select option from enum population funktion

populate options mysql enum

         

Gruessle

3:39 am on Mar 4, 2005 (gmt 0)

10+ Year Member




I picked this up somewhere and was wondering if someone could help me use it.

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>

Gruessle

4:29 am on Mar 4, 2005 (gmt 0)

10+ Year Member



Never mind - I found one which works:

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";
}

Gruessle

5:17 am on Mar 4, 2005 (gmt 0)

10+ Year Member



I was to fast again.
The values when I submit them don't register because there is no value=# field.
Either I need to find out how I can make them register in mysql without the value field or the funktion needs to return the value field as well.
Whatever is the better solution?!?

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')

Gruessle

5:42 am on Mar 4, 2005 (gmt 0)

10+ Year Member



This is embarrassing I wish there was a way to delete your own post.

It does work great.

The reason it wasn't working for me is:

I didn't chnage the NAME in SELECT.

<SELECT NAME="Select" SIZE='1'>

Sorry

jatar_k

6:13 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> This is embarrassing

I wouldn't worry about it, I can't even count how many times I have done this. The more scripts/sites you build the more annoyed you get with yourself for doing it. Eventually though it only merits a sigh or a slap upside the head from you. ;)

ergophobe

6:59 pm on Mar 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's like learning a foreign language - it's better make the occasional "stupid" error here and there than it is to say nothing and learn nothing!