Skip to main content
edited tags
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Do not write "solved" in title
Link
Mołot
  • 21.9k
  • 8
  • 59
  • 112

SOLVED: How to populate a dropdown list from array in FORM API

Solved
Source Link
Tom
  • 233
  • 3
  • 13

SOLVED: How to populate a dropdown list from array in FORM API

Hopefully this is clearer, and my last question in a long time!

Using PDO I have connected to an external DB, I can query this and return an array of values, Using html/php I can populate a standard select box. However I am unsure how to do this in the Form API, taking the array from the second paragraph, or do I query within the form item that produces the drop down? I want to take the name and the id, display the name. So that I can store the ID later.

      $stmt = $conn->prepare("SELECT nameadmin_name, user_idadminuser_id FROM adminuser_tbl");
if ($stmt->execute()) {
$stmt->setFetchMode(PDO::FETCH_OBJ);
$user_list = array();
    whileforeach ($row$stmt =as $stmt->fetch()$row) {
    $user_list[$row->user_id]>adminuser_id] = $row->name;>admin_name;
     }
 
}


$form['user_list']=array(
    '#type'=>'select',
    '#title' => t('Users'),
    '#options' => $user_list,
    '#multiple' => false,
    '#attributes'=>array('size'=>4),
    '#weight'=>8,
);

How to populate a dropdown list from array in FORM API

Hopefully this is clearer, and my last question in a long time!

Using PDO I have connected to an external DB, I can query this and return an array of values, Using html/php I can populate a standard select box. However I am unsure how to do this in the Form API, taking the array from the second paragraph, or do I query within the form item that produces the drop down? I want to take the name and the id, display the name. So that I can store the ID later.

 $stmt = $conn->prepare("SELECT name, user_id FROM adminuser_tbl");
 $user_list = array();
    while ($row = $stmt->fetch()) {
    $user_list[$row->user_id] = $row->name;
     }
 



$form['user_list']=array(
    '#type'=>'select',
    '#title' => t('Users'),
    '#options' => $user_list,
    '#multiple' => false,
    '#attributes'=>array('size'=>4),
    '#weight'=>8,
);

SOLVED: How to populate a dropdown list from array in FORM API

Hopefully this is clearer, and my last question in a long time!

Using PDO I have connected to an external DB, I can query this and return an array of values, Using html/php I can populate a standard select box. However I am unsure how to do this in the Form API, taking the array from the second paragraph, or do I query within the form item that produces the drop down? I want to take the name and the id, display the name. So that I can store the ID later.

      $stmt = $conn->prepare("SELECT admin_name, adminuser_id FROM adminuser_tbl");
if ($stmt->execute()) {
$stmt->setFetchMode(PDO::FETCH_OBJ);
$user_list = array();
foreach ($stmt as $row) {
    $user_list[$row->adminuser_id] = $row->admin_name;
}
}


$form['user_list']=array(
    '#type'=>'select',
    '#title' => t('Users'),
    '#options' => $user_list,
    '#multiple' => false,
    '#attributes'=>array('size'=>4),
    '#weight'=>8,
);
Update answer tries
Source Link
Tom
  • 233
  • 3
  • 13
Loading
Source Link
Tom
  • 233
  • 3
  • 13
Loading