开发者

Populate PHP Combo Box using MySQL Database Field Enum Values

开发者 https://www.devze.com 2023-03-30 12:46 出处:网络
I would like to create a combo box (eventually operating it using jQuery Ajax), which is populated with the enum values from a database table field.

I would like to create a combo box (eventually operating it using jQuery Ajax), which is populated with the enum values from a database table field.

My goal is to later use these options to filter mysql_query results on a page.

I'm thinking the best way to do this is to echo the entire form and combo box with the enum values as the option tags values. Where I fall short is knowing how to call the enum values in the first place.

If you have a better suggestion on how to do this, please let me know!

Thanks.


I found the following code, which appears to do what I need. However, it seems to grab all the enum arrays from my table, and convert each array into a combo box. This isn't a problem as I was hoping to create combo boxes for each of the separate arrays anyway. However, I wish to change the order of it. Is there any way I can edit this code to control each set? Or even have it specify which field I would like to echo, and then repeat as many times as I need? I know that doing that would mean more code, and would not be as efficient.

The PHP:

$table="images";
$describe=mysql_query("describe $table");
while ($ligne=mysql_fetch_array($describe)){
    extract($ligne);
    if (substr($Type,0,4)=='enum'){
        $liste=substr($Type,5,strlen($Type));
        $liste=substr($liste,0,(strlen($liste)-2));
        $enums=explode(',',$liste);
        if (sizeof($enums)>0){
            echo "<select name='enum'>\n";
            for ($i=0; $i<sizeof($enums);$i++){
                $elem=strtr($enums[$i],"'"," ");
        开发者_开发知识库        echo "<option value='".$elem."'>".$elem."</option>\n";
            }
            echo "</select>";
        }
    }
}

Thought I should mention, the three fields that contain enums are imgFamily, imgClass, and imgGender. I would like the combo boxes in that order preferably.


You can use:

SELECT column_type FROM information_schema.columns WHERE table_name = :myTable AND column_name = :myColumn

This will return something like this:

enum('one','two','three')

Which you could parse in php. However, I think it would perhaps be better to maintain the list in code or another table than as enum values.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号