Populate Dropdown from database

I am trying to include a dropdown in the accounts-module, which displays a selection of values from the databaseā€¦ I canā€™t figure out where to put the sql query to do so. Any help is appreciated!

Quick google search answer here

Hi,

I will explain here using Accounts module:

a) Create a drop down field using studio in editview of accounts, let the name of variable be ass_c.
b) Place this variable in edit layout view of accounts and click save and deploy button.
c) copy modules/Accounts/metadata/editviewdefs.php to custom/modules/Accounts/metadata/editviewdefs.php

In case editviewdefs.php exists at modules/Accounts/metadata/Editviewdefs.php do the following:

Replace the variable name ass_c in editviewdefs.php with

array (
ā€˜nameā€™ => ā€˜ass_cā€™,
ā€˜labelā€™ => ā€˜LBL_ASSā€™,
ā€˜customCodeā€™ => '{html_options options=$userdatadropdown selected=$mySelect} ā€™

      )

d) copy modules/Accounts/views/view.edit.php to custom/modules/Accounts/views/view.edit.php , in case the same does not exists at

custom/modules/Accounts/views/

e) Open custom/modules/Accounts/views/view.edit.php , go to display function and out the following code:

global $db;
//$MCArray = array(0=>ā€œtestā€,1=>ā€œtestssā€);

    $MCArray = array();
    $selval = '';
    $ri = '';
    $ridata=  '';
   
    
    $rss = $db->query("SELECT first_name,last_name,id from MYTABLE WHERE deleted = 0");
    while ($row = $db->fetchByAssoc($rss)){
		//$ri++;
		if($ri==''){
			$ri = $row['id'];
			$ridata = $row['first_name'] . " " . $row['last_name'] ;
			
		}
		
		$MCArray[$row['id']] = $row['first_name'] . " " . $row['last_name'] ;
		 
	}	
     $this->ss->assign('userdatadropdown', $MCArray );
    if($this->bean->ass_c){
		$this->ss->assign('mySelect', $ri);
		$this->ss->assign('sel', $ridata );
	}
    
    ////////////// code end for assigned to drop down///////////////// 

Then clear the cache. And go to the edit view of Accounts. You will see a drop down populated

Cheers! :slight_smile:

Hello,

I followed your topic. And did as mentioned. But its not working for me.

The dropdown is showing values in array form. Something is misplaced in your code as using this code showing error

Warning: Illegal offset type in /var/www

What I did to remove this errorā€¦ I changed $MCArray[$row] to $MCArray[] = $row;

Can you help me out here?

Thanks in advance.

The forum is not showing the correct format for the php code here. Ok u have to do two things

a) assign id of row to $rid

b) assign concatenated $rowā€™s first_name and last_name to $ridata

c) if i say id of $row as x and first_name of $row as y and last_name of $row as z then:

do assign

$MCArray[x] = y . one blank space . z;

Thanks for the replyā€¦

I did it and got values in the dropdown. But after saving the record, the field is not displaying that value because it is not saved in database.

How to save it in db?

In your message you wrote:

replace below lines with
$ri = $row;
$ridata = $row . " " . $row ;

$ri = $row;
$ridata = $row . " " . $row ;

This is same what to replace hereā€¦

The forum is not showing the correct format for the php code here. Ok u have to do two things
a) assign id of row to $rid
b) assign concatenated $rowā€™
c) if i say id of $row as x and first_name of $row as y and last_name of $row as z then:
do assign
$MCArray[x] = y . one blank space . z;

Thanks

Yupā€¦ working nowā€¦ but the values are getting saved in Numeric formatā€¦ like 0,1,2 etc for the dropdowns.

I need the same selected dropdown value to be saved in db. How it can be achieved?

Thanks

Do create dropdown using the studio and put the values there .

Hello Brother,
I need to create new customized dropdownlist. And the record into dropdown must be populated from the database.

Please help me.
Thanks in advanced.

Thats not wokring brother.

Hey, i created Custom Module for my requirement and i want to implement Dropdown Menu From Database but I could not found View Folder In My custom Module, can you Help me For that?