CodeIgniter Forums
form_dropdown() always returns 0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: form_dropdown() always returns 0 (/showthread.php?tid=8727)



form_dropdown() always returns 0 - El Forum - 05-29-2008

[eluser]EthanSP[/eluser]
Why is it that this form_dropdown() always returns 0?

form_dropdown($fby,$fy_values,$fy_default) // in view

where,

$fby is $data['fby'] = array('name'=>'birth_year'); // in model

$fy_values is $data['fy_values'] = $datelib->show_years(); // in model

function show_years() {
for ($x=date('Y')-150; $x<=date('Y'); $x++)
$years[] = $x;
return $years;
}

$fy_default is $data['fy_default'] = array_search(date('Y'),$data['fy_values']); // in model

In the controller:

if($this->input->post('mysubmit')) {
$this->mdl_demographics->save();
}

In the model:

$db_array = array('last_name' => $this->input->post('birth_year'));

Result in db:

last_name = 0

Why?


form_dropdown() always returns 0 - El Forum - 05-29-2008

[eluser]xwero[/eluser]
You are using an array for the dropdown name, the first argument only takes strings. I guess if you look at the source of your page you will see something like
Code:
<select name="Array">



form_dropdown() always returns 0 - El Forum - 05-29-2008

[eluser]EthanSP[/eluser]
[quote author="xwero" date="1212071956"]You are using an array for the dropdown name, the first argument only takes strings. I guess if you look at the source of your page you will see something like
Code:
<select name="Array">
[/quote]

I patterned my work with that of the CI User Guide's, which is:
form_dropdown('shirts', $options, 'large');

where,

I replaced:
'shirts' w/ a variable ($fby),
$options w/ another variable/array ($fby_values), and
'large' w/ yet another variable ($fby_default).

And looking at my source code, I didn't use <select name="Array">, instead:

function show_years() {
for ($x=date('Y')-150; $x<=date('Y'); $x++)
$years[] = $x;
return $years;
}


form_dropdown() always returns 0 - El Forum - 05-29-2008

[eluser]xwero[/eluser]
Quote:$fby is $data[’fby’] = array(’name’=>’birth_year’); // in model
the $fby variable is an array but it should be a string. Or you would have to do
Code:
form_dropdown($fby['name'])

If you look to the source of your rendered page in your browser i think you will see the snippet i posted in my previous response.


form_dropdown() always returns 0 - El Forum - 05-29-2008

[eluser]EthanSP[/eluser]
xwero,

I see. So, that's the way to do it. May I just make a follow-up question: How can I display its value instead of index only? Because $fby['name'], where 'name' = 'birth_year,' when used here: 'birthday' => $this->input->post('birth_year'), outputs the index only and not its value. How can I display the value?

Also, if I display the source of the rendered page, all I see are HTML frame tags and other elements.

BTW, thanks for your time & for imparting your knowledge.


Ethan


form_dropdown() always returns 0 - El Forum - 05-30-2008

[eluser]poji23[/eluser]
Hi,

Just a small fix in your show_years method and it's done:

Code:
function show_years() {
for ($x=date('Y')-150; $x<=date('Y'); $x++)
$years[$x] = $x;
return $years;
}



form_dropdown() always returns 0 - El Forum - 06-03-2008

[eluser]EthanSP[/eluser]
It worked like magic! Thank you very much.