Coders Opinion

Dropdowns in Kohana

Introduction

user

Robert-Jan de Dreu

Hey, I'm Robert-Jan de Dreu. A programmer mostly focusing on the web. While using PHP and MVC I will make sure the program I'm building is well documented and structured. I use this blog to show what I made and keep reminding myself of little tasks that I have done and maybe need to do again.

I live in Utrecht, The Netherlands and can be found in Ibuildings where my job is at. I'm available for freelancing if you want me to do a project for you. Just send me a message.


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://google.com

Hey, I'm Robert-Jan de Dreu. A programmer mostly focusing on the web. While using PHP and MVC I will make sure the program I'm building is well documented and structured. I use this blog to show what I made and keep reminding myself of little tasks that I have done and maybe need to do again.

I live in Utrecht, The Netherlands and can be found in Ibuildings where my job is at. I'm available for freelancing if you want me to do a project for you. Just send me a message.