Sikander
Sikander

Reputation: 2835

Codeigniter : passing array from controller to view dropdown

I am fetching data from database and have to pass it to dropdown in view. This is what i've in controller

 public function  register(){
        foreach ($this->user_model->getallcountries() as $key => $value) {
             $countries[] = array('id'  =>  $value['id'] ,'name'  => $value['name'] );
        }

 $array= array( 
                array(
                    'type'          =>       'countryname',
                    'name'          =>        'Country',
                    'options'       =>        $countries
                ),

       );

   $this->load->view('authentication',$output);

i can parse the array and it works like this

<?php 
     foreach ($data as $key => $value) {
        if($value['type']=='countryname'){
            foreach ($value['options'] as $subvalue){
                echo $subvalue['id'].' =  '.$subvalue['name'].'<br/>';

            }
        }
    } 
?>

and this is what i get

1 = United Arab Emirates
2 = Saudi Arabia 
3 = Oman
4 = Qatar
5 = Bahrain
6 = Kuwait

this is what i need to pass to form_dropdown that i dont know how i can do this

i tried something like this

 <?php 
      if($value['type']=='countryname'){
       echo '<div class="form-group">';
        echo form_dropdown('countryname', $value['options'],'','class="form-control"');
         echo '</div>'; 

    }?>

but don't know how to process inner array in form_dropdown

this is what i have

Upvotes: 0

Views: 86

Answers (1)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

In Controller

$array = array(     
    'type'          =>       'countryname',
    'name'          =>        'Country',
    'options'       =>        $countries
);

$data['options'] = $array;

In View

if($options['type']=='countryname'){
    echo '<div class="form-group">';
    echo form_dropdown('countryname', $options['options'],'','class="form-control"');
    echo '</div>'; 
}

Upvotes: 1

Related Questions