Welcome Guest, Not a member yet? Register   Sign In
form_dropdown
#1

[eluser]louisc[/eluser]
Hi,

I am passing an array to the form_dropdown helper, please see:

Model:
Code:
public function get_locations()
    {
        return array(9 => 'Gauteng', 1 => 'Eastern Cape', 2 => 'Free State', 3 => 'KwaZulu-Natal', 4 => 'Limpopo', 5 => 'Mpumalanga', 6 => 'North West');
    }

View:
Code:
$db_results = $this->lookup_model->get_locations();
$pr_options = array('' => 'Please select');
$options = array_merge($pr_options, $db_results);

$data = 'id="location" class="required" title="Select your location."';
echo form_dropdown('location', $options, set_value('location'), $data);

Even though I have given the array a key say for example in the case of Gauteng which is 9, it starts counting from 0.

Resulting HTML:
Code:
<select class="required" id="location" name="location">
<option selected="selected" value="">Please select</option>
<option value="0">Gauteng</option>
<option value="1">Eastern Cape</option>
<option value="2">Free State</option>
<option value="3">KwaZulu-Natal</option>
<option value="4">Limpopo</option>
<option value="5">Mpumalanga</option>
<option value="6">North West</option>
</select>

I want it to be the same as the value of the key specified in the array that I am passing.
Please HELP!!!
#2

[eluser]InsiteFX[/eluser]
Arrays start at 0 so you need to do something like this:
Code:
public function get_locations()
{
    return array(0 => 'Select etc', 9 => 'Gauteng', 1 => 'Eastern Cape', 2 => 'Free State', 3 => 'KwaZulu-Natal', 4 => 'Limpopo', 5 => 'Mpumalanga', 6 => 'North West');
}

InsiteFX
#3

[eluser]louisc[/eluser]
The reason I did the following was to be able to use the required CI validation rule, so it is not possible to do that.

$db_results = $this->lookup_model->get_locations();
$pr_options = array('' => 'Please select');
$options = array_merge($pr_options, $db_results);
#4

[eluser]defectivereject[/eluser]
With mine i use something like this

Code:
$data = array();
        $this->db->select('value, id');
        $this->db->order_by('value','asc');
        $Q = $this->db->get('table');
         if ($Q->num_rows() > 0){
           foreach ($Q->result_array() as $row){
             $data[$row['id']] = $row['value'];
           }
        }
        $Q->free_result();  
        return $data;
     }
this always gives me a dropdowns with the id as "value" and the value as the display
in the order i specified in the model query

then just use
Code:
echo form_dropdown('location', $options, 9, $data);
and it loads the dropdown with 9 as the displayed value........?
#5

[eluser]CodeIgniteMe[/eluser]
When you do a
Code:
print_r($options);
before you output the form / after you fetched the array, what does it produce?
#6

[eluser]louisc[/eluser]
[quote author="CodeIgniteMe" date="1309950372"]When you do a
Code:
print_r($options);
before you output the form / after you fetched the array, what does it produce?[/quote]

That I have done, and it produces the key and value as I put it in the array...so that works fine.

Code:
Array ( [9] => Gauteng [1] => Eastern Cape [2] => Free State [3] => KwaZulu-Natal [4] => Limpopo [5] => Mpumalanga [6] => North West )
#7

[eluser]jmadsen[/eluser]
Oh, for shame on all of you!

array_merge() renumbers the keys, with no switch or option to maintain them.


If you need to keep the keys, use "+":

Code:
$new = $pr_options + $db_results;
        print_r($new);
#8

[eluser]brucebat[/eluser]
Im having a problem posting from a dropdown to insert into my database.

When I try and post it does not give a value and ends up having a 0 value.


This is my array

Code:
$department = array
                (
                    '1' => 'Cardiology',
                    '2' => 'Radiology'
                );

My function in the view
Code:
echo form_dropdown ('procedure_deparment', $department,set_value('procedure_department'));

The result HTML

Code:
<select name="procedure_deparment">

<option value="1">Cardiology</option>
<option value="2">Radiology</option>
</select>


The post function in my Model:

Code:
//sets the foreign key from dropdown form
$departmentfk = $this->input->post('procedure_department');


So it goes View --&gt; Controller --&gt; Model

I don't understand why its not receiving a value, any ideas?

Thanks
#9

[eluser]jmadsen[/eluser]
is your form using a POST or a GET?
#10

[eluser]brucebat[/eluser]
Its using the form helper library from Codeigniter.

The other post functions work but this one doesnt for some reason, is it my array?




Theme © iAndrew 2016 - Forum software by © MyBB