Dropdown from DB Alphabetized - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Dropdown from DB Alphabetized (/showthread.php?tid=3122) |
Dropdown from DB Alphabetized - El Forum - 09-11-2007 [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. Dropdown from DB Alphabetized - El Forum - 09-11-2007 [eluser]Michael Wales[/eluser] ORDER BY last_name ASC or ORDER BY last_name DESC Dropdown from DB Alphabetized - El Forum - 09-11-2007 [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'); Code: $formAtt = array('id'=>'selClient'); Dropdown from DB Alphabetized - El Forum - 09-11-2007 [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'); or Code: $query = $this->db->query('SELECT last FROM Names ORDER BY last ASC'); Not both. :-) Dropdown from DB Alphabetized - El Forum - 09-11-2007 [eluser]I666I[/eluser] Derr. Thank you very much. My feeble brain very much appreciates your help Dropdown from DB Alphabetized - El Forum - 09-12-2007 [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. Dropdown from DB Alphabetized - El Forum - 09-12-2007 [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. Dropdown from DB Alphabetized - El Forum - 09-12-2007 [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. Dropdown from DB Alphabetized - El Forum - 09-12-2007 [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--'; Code: $formAtt = array('id'=>'selClient'); Dropdown from DB Alphabetized - El Forum - 09-12-2007 [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', The dropdown form that is generated will look like this: Code: <select size="1"> 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"> 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. |