0
votes

I'm Just start working in CodeIgniter framework.

now i have initiate an array in my controller

 public function index() {

   $data = array(
                        'option0'       =>  'Select Option...',
                        'option1'       =>  'Option 1',
                        'option2'       =>  'Option 2',
                        'option3'       =>  'Option 3',);

    $this->load->view('news/index',$data);

now, when i create dropmenu in view , this the code

     <?php echo form_dropdown('options',$data,'','Options') ?>

        <?php echo form_close(); ?>

so, when i browse in the net ... this errors came :

==================================================================================

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: news/index.php

Line Number: 11

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 331

hint..

when i but my Array the view file, its work perfect.

i want to keep my code in controller & models, and views only for output

please help

1

1 Answers

0
votes

Do this in your controller:

$data['dropdown'] = array(
  'option0'       =>  'Select Option...',
  'option1'       =>  'Option 1',
  'option2'       =>  'Option 2',
  'option3'       =>  'Option 3');

Then in view:

<?php echo form_dropdown('options',$dropdown, 'option0') ?>

<?php echo form_close(); ?>

If you want to learn more about form_dropdown, then look at the docs here.