![]() |
Form dropdown from db data - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Form dropdown from db data (/showthread.php?tid=13236) |
Form dropdown from db data - El Forum - 11-15-2008 [eluser]abmcr[/eluser] Code: /** Put it into a MY_Form_helper... into the view use Code: echo form_dropdown_from_db('prova', "SELECT id,c_txtitaliano FROM categorie"); Form dropdown from db data - El Forum - 11-15-2008 [eluser]Référencement Google[/eluser] Your idea of having a form dropdown helper from DB results is good, but I don't like much that it needs to have SQL commands in a view. What better solution can we think of? Form dropdown from db data - El Forum - 11-17-2008 [eluser]Iverson[/eluser] [quote author="Too Pixel" date="1226822978"]Your idea of having a form dropdown helper from DB results is good, but I don't like much that it needs to have SQL commands in a view. What better solution can we think of?[/quote] I agree. I was thinking the same thing. I too like the idea though so this might be a little better implementation. I separated the actual data manipulation from the display part. In your controller (I would probably run this in a model), you would run... Code: $form_data = form_dropdown_data('prova', "SELECT id,c_txtitaliano FROM categorie") Then you could send your form to your view... Code: $form_display = form_dropdown_display($form_data); Helper File Code: function form_dropdown_data($name = '', $sql, $selected = array()) Form dropdown from db data - El Forum - 11-17-2008 [eluser]Référencement Google[/eluser] That make sense now, thanks for sharing. Form dropdown from db data - El Forum - 03-30-2009 [eluser]Nicholai[/eluser] I'm pretty new to CodeIgniter (and PHP in general) and receive multiple errors trying to use this helper. However, the drop-down appears below the errors and is pulling data normally, though the "name" parameter of the option list is blank. From what I can tell, my controller follows the function as it should: Code: function addnew() And my view calls it correctly: Code: <?php echo form_open('tasks/insert_task'); ?> When loading the view, I get these errors. Undefined variable: extra Undefined variable: name Undefined variable: multiple Undefined variable: selected in_array() [function.in-array]: Wrong datatype for second argument Do you have any advice for what I may be doing wrong? I love this helper! Form dropdown from db data - El Forum - 12-02-2010 [eluser]xavieremerson[/eluser] hi.. I'm very new to codeigniter... This article is very much helped me. But I am changed the sql statements in this. Instead of that I used an array variable.. Here is the code .... Any way thankz.... for giving idea... function form_dropdown_from_db($name = '', $array,$data,$selected = array(), $extra = '') { //$CI =& get_instance(); if ( ! is_array($selected)) { $selected = array($selected); } // If no selected state was submitted we will attempt to set it automatically if (count($selected) === 0) { // If the form name appears in the $_POST array we have a winner! if (isset($_POST[$name])) { $selected = array($_POST[$name]); } } if ($extra != '') $extra = ' '.$extra; $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; $att = ''; foreach ($data as $key => $val) { $att .= $key . '="' . $val . '" '; } $form = '<select name="'.$name.'"'.$extra.$multiple. $att . ">\n"; if (count($array) > 0) { foreach ($array as $row) { $values = array_values($row); if (count($values)===2){ $key = (string) $values[0]; $val = (string) $values[1]; //$this->option($values[0], $values[1]); } $sel = (in_array($key, $selected))?' selected="selected"':''; $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n"; } } $form .= '</select>'; return $form; } Form dropdown from db data - El Forum - 12-12-2010 [eluser]asppp[/eluser] Hello, I liked the first method you described abmcr, It works, but in my view prints out (example): Code: <select name="fruits"> And I want it to print out the fruitnames in the "value", like this: Code: <select name="fruits"> How do I do that??? Form dropdown from db data - El Forum - 01-16-2011 [eluser]Unknown[/eluser] [quote author="asppp" date="1292215231"]Hello, I liked the first method you described abmcr, It works, but in my view prints out (example): Code: <select name="fruits"> And I want it to print out the fruitnames in the "value", like this: Code: <select name="fruits"> How do I do that???[/quote] Change line 49 to the following: Code: $form .= '<option value="'.$val.'">'.$val."</option>\n"; |