Welcome Guest, Not a member yet? Register   Sign In
Form drop_down problem
#1

[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
#2

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

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

[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?
#5

[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'); ?>
#6

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

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

[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'); ?>
#9

[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.
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB