Community & Support

How to display list of options dynamically from DB using PHP in PHP Code box

Hello Community,

I created a form named downloads by creating a content type. Now I need to give access to download items based on role.

I am trying to create a Download Permissions dropdown to associate with every download item. For this I need to fetch the roles from the DB. I am trying to write code to fetch roles from the db.

When I write some example code to populate the dropdown, the code is approved by drupal, but it is not populating my dropdown. Can anyone give me an idea Where I am going wrong.

The example code I tried is as this:

$test_array = array(
  0 => array('1' => 'value for nid'),
  1 => array('2' => 'value for nid')
);

return $test_array;

Any help is really appreciated.

Thanks,
Manish Aneja

Comments

I also want to mention that

I also want to mention that I am writing this in Default value PHP Code block. When I search through various posts there are two types ov boxes:

Default Value
and
Allowed Values.

But I don't know Where do we set Allowed Values list.

plz try this in your select options

$test_array = array(
'1' => 'value for nid',
'2' => 'value for nid'
);

return $test_array;

Not accepting

Hi Kuldip,

Thanks for your reply.

I tried it the way you mentioned, but Drupal does not accept it that way and shows an error:

The default value PHP code returned an incorrect value
Expected format :

array(
  0 => array('nid' => value for nid),
  // You'll usually want to stop here. Provide more values
  // if you want your 'default value' to be multi-valued :
  1 => array('nid' => value for nid),
  2 => ...
);

Returned value : Array ( [1] => value for nid [2] => value for nid )

Done

Hello Community,

Finally I am able to dynamically populate the dropdown from the database.

I was checking Select List from Node Reference while I has to check out the select list from the of type text.

I got an idea from here:
http://blue.live4all.co.il/~mooffie/cms/node/15

Thanks,
Manish Aneja