Create dropdown in cakephp by using option values from database

Cakephp

Posted on 10 Oct 15 by Vickey
  538



By using html helper we can create form elements in cakephp.here i am creating a dropdown in which all option coming from database table

First use html helper in your controller

public $helper = array('Html');

now i am fetching data from database for use this in dropdown

$Countries=$this->Country->find('all',array('fields'=>array('Country.id','Country.name')));
foreach ($Countries as $country) {
 $countrylist[$country['Country']['id']]=$country['Country']['name'];
}
$this->set('countrylist', $countrylist);

In  view

<?php echo $this->Form->input('User.country_id',array('type'=>'select','options'=>$countrylist)); ?>

 


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com