Welcome Guest, Not a member yet? Register   Sign In
form_dropdown() Selected value
#1

[eluser]mdcode[/eluser]
I have a form with a few select boxes pulling options from the database. Basically when the for is loaded, I want these select boxes to be blank, however they are always populated with the result for id equal to 1. How can I get them to display blank without having blank rows in my db?

VIEW:
Seemingly the selected attribute on the form_dropdown() function in the view doesn't do anything... I have tried it with nothing:
Code:
<?php echo form_dropdown('customer', $customers, '', $dds); ?>
Blank:
Code:
<?php echo form_dropdown('customer', $customers, ' ', $dds); ?>
An id value:
Code:
<?php echo form_dropdown('customer', $customers, '2', $dds); ?>
And the result text:
Code:
<?php echo form_dropdown('customer', $customers, 'Customer One', $dds); ?>

CONTROLLER:
Code:
/* grab the customer list */
            if ($customers = $this->projects_model->get_all_customers())
            {
                foreach ($customers as $key=>$value)
                $data['customers'][$value['id']] = $value['name'];
            }

MODEL:
Code:
/* get all customers */
            function get_all_customers()
            {
                $this->db->select('*');
                $this->db->from('customers');
                $this->db->order_by('name', 'ASC');
                
                $query = $this->db->get();
                return ($query->num_rows) ? $query->result_array() : FALSE;
            }
Any ideas peeps? Mucho appreciated.
#2

[eluser]pistolPete[/eluser]
Could't you add a blank entry to your dropdown when you generate the array?
Code:
$data = array();
// I assume you have no entry using id=0
$data['customers'][0] = '';
foreach ($customers as $value)
{
   $data['customers'][$value['id']] = $value['name'];
}
#3

[eluser]mdcode[/eluser]
Unfortunately not -- while your code doesn't generate any errors, it also does nothing to the results of the form even though id 0 is not being used.

Following on your advice I have also tried to formulate it the same as the rest of the query:

Code:
$data['customers'][$value['0'] = $value[''];
which brings up these errors (x2):
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: 0
Filename: controllers/projects.php
Line Number: 56
and
Code:
$data['customers'][0] = $value[''];
which brings up this error:
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined index:
Filename: controllers/projects.php
Line Number: 56
#4

[eluser]mdcode[/eluser]
Ok, playing around with this and with ideas running through my head using your code I have got it to work like this:
Code:
/* grab the division list */
            if ($divisions = $this->projects_model->get_all_divisions())
            $data['divisions'][$value['id']=0] = '';
            {
                foreach ($divisions as $key=>$value)
                $data['divisions'][$value['id']] = $value['name'];
            }
Thanks for the prompting pistolpete Smile




Theme © iAndrew 2016 - Forum software by © MyBB