2

i am trying below code to display list of enum values in select dropdown box. but its displaying only dropdown box, but values are not displaying....

tablename = tbl_users, column name = userStatus

<select>
<?
    $stmt = $user_home->runQuery('SHOW COLUMNS FROM '.tbl_users.' WHERE field="'.userStatus.'"');
       while($data = $stmt->fetch()) {
            foreach(explode("','",substr($row[1],6,-2)) as $option) {
                print("<option>$option</option>");
            }
        }
?>
<select>

enter image description here

Note : I really tried lot before posting question here & i am new to php coding, still learning....

1
  • your example code having problem, you get $data from $data = $stmt->fetch() and use $row in loop, how can I help with with a wrong question information? Commented Nov 10, 2016 at 7:11

2 Answers 2

2

To display list of enum values in select dropdown:

<select name="select">
<?php
  $sql = 'SHOW COLUMNS FROM table_name WHERE field="field_name"';
  $row = $dbh->query($sql)->fetch(PDO::FETCH_ASSOC);
  foreach(explode("','",substr($row['Type'],6,-2)) as $option) {
   print("<option value='$option'>$option</option>");
  }
?>
</select>
Sign up to request clarification or add additional context in comments.

Comments

-2

For display enum value as dropdown you can do something like this.

<?php $status = array('Y'=>'Approve','N'=>'unapprove');  ?>
<select>
<?php foreach($status as $key=>$state) { ?>
<option value="<?php echo $key;?>"><?php echo $state;?></option>
<?php } ?>
</select>

4 Comments

this answer is a fraud. it doesn't answer the question asked.
@YourCommonSense I am show the way to display enum value as dropdown at any place, then tell me why it fraud?
because the question was specifically about enum field stored in database
yes, but the enum value is always fixed, so we can used that value as key in array and Display label which we want in dropdown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.