Coders Opinion

Dropdowns in Kohana

Introduction

user

Robert-Jan de Dreu

Hey, I’m Robert-Jan de Dreu. A freelance software engineer, developing software since 2009. While using PHP and Javascript I build well structured and tested software.

About Me Hire Me
Featured

Kohana 2

Dropdowns in Kohana

Posted by Robert-Jan de Dreu on .

I began to get irritated that I always had write a foreach for the Dropdowns after the query. So I was thinking isn’t there a more easier way to do it. First I wrote a helper to do it for me but It was clumsy in my opinion. Since I like nice clean applications I extended the Database_Driver class. The problem is that you need too add it directly to system because you can’t write it in app.

Therefore you will need to add the following code in “\system\libraries\drivers\Database.php”:

public function select_list($id, $descriptor)
{ 
	$array = array();
	$result = $this;

	foreach ($result as $key=>$value)
	{ 
		$array[$value->$id] = $value->$descriptor;
	}

	return $array; 
}

After you did this you can use it like this:

$user_model = new User_Model;
$users = $user_model->read_users()->select_list('user_id', 'user_name'); 
print form::dropdown('users', $users);
user

Robert-Jan de Dreu

http://codersopinion.com

Hey, I’m Robert-Jan de Dreu. A freelance software engineer, developing software since 2009. While using PHP and Javascript I build well structured and tested software.