Help with dropdown - 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: Help with dropdown (/showthread.php?tid=26831) |
Help with dropdown - El Forum - 01-24-2010 [eluser]Bainzy[/eluser] Hi everyone I'm really stuck ATM with dropdown boxes all I am trying to do is have the dropdown box show the category name and have the category ID as the value but it's not working its just filling the dropdown with all the information from my categorys table how would I go about doing this ? Help with dropdown - El Forum - 01-24-2010 [eluser]JHackamack[/eluser] Do you have an example of your code that you're trying to create? This would help in debugging. Help with dropdown - El Forum - 01-24-2010 [eluser]Bainzy[/eluser] Right ok the code I have at the momen is as follows Foreach ($this->ForumModel->fetchAllRows('category') as $row) { $options[$row->CategoryID] = $row->Name; } $data['options'] = $options; then is send the data to the view file which is as follows <?php echo form_dropdown('category', $options); ?> Help with dropdown - El Forum - 01-24-2010 [eluser]JHackamack[/eluser] What does $this->ForumModel->fetchAllRows('category') return? Help with dropdown - El Forum - 01-25-2010 [eluser]Bainzy[/eluser] That line returns all the rows from the 'Categorys' database so that i can select what data i require from it. Chris Help with dropdown - El Forum - 01-25-2010 [eluser]JHackamack[/eluser] My Question was referring to How CI returns rows. In order to do a foreach loop it needs to read Code: foreach($rows->result() as $row) but your model has to be returning this: Code: $rows = $this->db->get('table'); Help with dropdown - El Forum - 01-25-2010 [eluser]danmontgomery[/eluser] I have a helper that I use to generate select fields from database tables. It will generate a select from the first two columns in a table, optionally you can pass it some SQL (which should return at least 2 columns, the first two will be the value and displayed option), or an array which it passes to the native form_dropdown() function. Code: echo make_select("my_field", "table_name", set_value("my_field")); Params: $name: Name of field $table: Table in DB (optionally, an assoc array as value => display) $selected_value: Option selected by default $options: Array of options: default_value: Default option at the top of the list of options, has no value ("Select One..." by default, FALSE to disable) params: Attributes to append to the <select> tag, given as an assoc. array sql: Optional SQL statement to construct the select from So, in your case, assuming there are only 2 columns (CategoryID and Name) in the categories table (or they are the first 2 columns), you could just use: Code: echo make_select("category", "categories", set_value("category")); otherwise, Code: echo make_select("category", false, set_value("category"), array("sql" => "SELECT `CategoryID`, `Name` FROM `categories` ORDER BY `Name` ASC")); Hope this helps. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |