Welcome Guest, Not a member yet? Register   Sign In
Dropdown from DB Alphabetized
#1

[eluser]I666I[/eluser]
Hi,

I have a dropdown being populated from the db, and It's working except that I'd like the list to be alphabetized.

I tried SELECT id, last_name FROM Names ORDER BY last_name and it didn't throw an error, but it also didn't order the list.

Is there a way to get this working?

Much thanks.
#2

[eluser]Michael Wales[/eluser]
ORDER BY last_name ASC

or

ORDER BY last_name DESC
#3

[eluser]I666I[/eluser]
Thanks, but unfortunately that's not doing it. It is still ordering the list by row id. Here is what my code looks like:
Code:
$this->db->query('SELECT last FROM Names ORDER BY last ASC');
                $query = $this->db->get('Names');
                foreach ($query->result() as $row){
                    $data['Names'] [$row->last] = $row->last;
                }
                
                //LOAD  VIEW AND DATA FOOL
                $this->load->view('index_view', $data);
view:
Code:
$formAtt = array('id'=>'selClient');
                echo form_open('selectClient', $formAtt);
                    $js = 'onChange = alert(document.selClient.clients.options[document.selClient.clients.selectedIndex].value)';
                    echo form_dropdown('clients', $Names, 'Select Client', $js);
                    echo form_close();
                ?>
No matter what I try in the sql statement, it still is ordering the list by the id of the row. Much thanks for the help.
#4

[eluser]Michael Wales[/eluser]
Ah - it's because $this->db->get() is creating your query - you could remove your first line and it would perform exactly the same (basically, you are duplicating your efforts).

Code:
$this->db->orderby('last', 'ASC');
$query = $this->db->get('Names');

or

Code:
$query = $this->db->query('SELECT last FROM Names ORDER BY last ASC');

Not both. :-)
#5

[eluser]I666I[/eluser]
Derr. Thank you very much. My feeble brain very much appreciates your help Smile
#6

[eluser]I666I[/eluser]
A small problem I'm still having with the dropdown is the default value. According to the user_guide, in the code above the default value should be 'Select Client', but it is not showing up and the first in the list is selected by default. This is a problem because the onChange doesn't register the default value.

I've tried throwing everything in an array, tried setting the default as a variable, but nothing seems to work.

Is there something I'm doing wrong?

Much thanks for any help.
#7

[eluser]Michael Wales[/eluser]
Open your page and View -> Source - see what the value="" attribute is for the option you would like selected. Then set that value as the parameter in the form_dropddown function.
#8

[eluser]I666I[/eluser]
Hmmm. So it has to be one of the options in the list then? That still wont work. The whole list is of names that lead to a page for each name, so the default name can't be selected with the onchange function (onselect isn't working for some reason -- probably cause I'm noob). I guess I can just add a row to the db with --- or something.

Thank you.
#9

[eluser]Michael Wales[/eluser]
Ah - I think what you are wanting is it to load, by default - "Select Client" then people click that to select whatever client they want? That's easy.

Code:
$data['Names']['default'] = '--Select Client--';
$this->db->query('SELECT last FROM Names ORDER BY last ASC');
$query = $this->db->get('Names');
foreach ($query->result() as $row){
     $data['Names'] [$row->last] = $row->last;
}

//LOAD  VIEW AND DATA FOOL
$this->load->view('index_view', $data);

Code:
$formAtt = array('id'=>'selClient');
echo form_open('selectClient', $formAtt);
$js = 'onChange = alert(document.selClient.clients.options[document.selClient.clients.selectedIndex].value)';
echo form_dropdown('clients', $Names, 'default', $js);
echo form_close();
#10

[eluser]Michael Wales[/eluser]
Just an explanation here:

The form_dropdown() function wants a name, an array of options, the initially selected option, and then any javascript. You had that pretty much down - what's confusing you was the array of options.

Let's say we have the following array:
Code:
$array = array('l' => 'Large',
              ('xl' => 'Extra Large',
              ('xxl' => 'Extra Extra Large');

The dropdown form that is generated will look like this:
Code:
<select size="1">
  <option value="l">Large</option>
  <option value="xl">Extra Large</option>
  <option value="xxl">Extra Extra Large</option>
</select>

To make one of these items the initial selection - we have to give CodeIgniter the value attribute of the option you want selected - so, if we pass 'xl' to the helper function, our dropdown now looks like:

Code:
<select size="1">
  <option value="l">Large</option>
  <option value="xl" selected>Extra Large</option>
  <option value="xxl">Extra Extra Large</option>
</select>

Note the selected in xl now.

In your case - you wanted an instructional option, so we're just going to tack on another value within our array - at the beginning, so it's at the top when people click the dropdown.
Code:
$data['Names']['default'] = '--Select Client--';

The helper function will generate an option statement for this array variable that looks like:
Code:
<option value="default">--Select Client--</option>

And when you pass the value 'default' to the helper function, it of course - adds the selected attribute to this option tag.




Theme © iAndrew 2016 - Forum software by © MyBB