CodeIgniter Forums
Form drop_down problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Form drop_down problem (/thread-44056.html)



Form drop_down problem - El Forum - 08-01-2011

[eluser]Michal1[/eluser]
Hello guys I trying to use dropdowns in a simple way and I am having problems with them.

I have a controller site and inside of function index I have:

Code:
$shirts_on_sale = array('small', 'large');
                    $this->load->view('admin_view',$shirts_on_sale);

then in admin_view I have

Code:
<?php echo form_dropdown('shirts', $shirts_on_sale, 'large'); ?>

But I receive an error

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: shirts_on_sale

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: helpers/form_helper.php

Line Number: 310

why is that? Thanks for any input


Form drop_down problem - El Forum - 08-01-2011

[eluser]toymachiner62[/eluser]
Did you load the form helper?


Form drop_down problem - El Forum - 08-01-2011

[eluser]danmontgomery[/eluser]
Code:
$data = array('shirts_on_sale' => array('small', 'large'));
$this->load->view('admin_view',$data);



Form drop_down problem - El Forum - 08-01-2011

[eluser]Michal1[/eluser]
[quote author="noctrum" date="1312241160"]
Code:
$data = array('shirts_on_sale' => array('small', 'large'));
$this->load->view('admin_view',$data);
[/quote]

Thank you but I dont understand why it has to be in this way?


Form drop_down problem - El Forum - 08-01-2011

[eluser]Michal1[/eluser]
Because even if I have this it does not work

Code:
$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );
                    $this->load->view('admin_view',$options);

and then in admin_view:

Code:
<?php echo form_dropdown('shirts', $options, 'large'); ?>



Form drop_down problem - El Forum - 08-01-2011

[eluser]Michal1[/eluser]
huh anyone? :/


Form drop_down problem - El Forum - 08-01-2011

[eluser]Aken[/eluser]
You need to read more about how dynamic data is passed to views: http://ellislab.com/codeigniter/user-guide/general/views.html


Form drop_down problem - El Forum - 08-02-2011

[eluser]Michal1[/eluser]
huh,I know how dynamic data are passed to views. I passed as an array of course here:

Code:
$this->load->view('admin_view',$options);

So there must problem in a view in this line right?

Code:
<?php echo form_dropdown('shirts', $options, 'large'); ?>



Form drop_down problem - El Forum - 08-02-2011

[eluser]LuckyFella73[/eluser]
Quote:Because even if I have this it does not work

$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
$this->load->view('admin_view',$options);
and then in admin_view:

<?php echo form_dropdown('shirts', $options, 'large'); ?>

Should look like:
Code:
$data['options'] = array(
    'small'  => 'Small Shirt',
    'med'    => 'Medium Shirt',
    'large'   => 'Large Shirt',
    'xlarge' => 'Extra Large Shirt'
    );
$this->load->view('admin_view',$data);
//and then in admin_view:

echo form_dropdown('shirts', $options, 'large');

It's basically the same noctrum told you - I just tried to make it
easier (visually)

It's important for you to understand that you send an array to the view
(in my example "$data") which keys are usable as variables/arrays in your view.

Veery basic example:
Code:
// controller:
$data['variable_1'] = 'Value 1';
$data['variable_2'] = 'Value 2';
$data['test_array'] = array(0 => 'array value 1', 1 => 'array value 2');
$this->load->view('admin_view',$data);

//view file:
echo $variable_1;
echo $variable_2;
foreach ($test_array ...)
// do something

Hope that makes it a bit clearer.


Form drop_down problem - El Forum - 08-02-2011

[eluser]Michal1[/eluser]
Ah thanks, I got it know.