0

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

2
  • your array is wrong in controller what you passing. Commented Aug 14, 2016 at 14:49
  • you mean the one i passed in dropdown ? i don't know how to parse it Commented Aug 14, 2016 at 14:52

1 Answer 1

1

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>'; 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.