Working Drop Down List Code Review Help - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Working Drop Down List Code Review Help (/showthread.php?tid=35095) |
Working Drop Down List Code Review Help - El Forum - 10-19-2010 [eluser]TerryT[/eluser] I am new to CI and looking for some advice as to best practices from the experts. I want my users to be able to either select all projects or any individual project. The code below works. Can you let me know what needs to be done to it so it represents best practices and is bulletproof? Also what would be typically included for error trapping and messages? Thanks in advance for any advice. Terry Controller code: Code: $data = array(); Model Code: Code: function getProjects() View code: Code: <div class='dropdown'> Working Drop Down List Code Review Help - El Forum - 10-19-2010 [eluser]CroNiX[/eluser] I would use the form helper myself... Controller: Code: $data = array(); Code: function get_projects() Code: <div class='dropdown'> Working Drop Down List Code Review Help - El Forum - 10-19-2010 [eluser]TerryT[/eluser] Thank you for the help. I see this makes the view cleaner. Would you typically put any code in to check and make sure there is something returned in the query? If a user just gets started with the program, he will not have entered a project yet. The dropdown will just be empty, but I don't see that as a problem. Thanks again. Terry Working Drop Down List Code Review Help - El Forum - 10-20-2010 [eluser]TerryT[/eluser] The code suggested worked well. I wanted to add an "All Projects" selection at the top of the drop down. That took an hour to figure out on my own. Posting the code here for any newbies who may encounter the same issue: Code: //form helper for dropdown requires array of id=>value: Terry Working Drop Down List Code Review Help - El Forum - 10-20-2010 [eluser]CroNiX[/eluser] I generally do this: Code: function get_projects() Then in your form for the dropdown, you can: Code: <p><?php echo form_dropdown('my_select', $project_menu, 0); ?></p> [/code] But if you want to make this reusable (since all form dropdowns require this format), make a helper: Code: function get_form_dropdown($table, $id_field, $value_field, $first_option = '') Code: $data['project_menu'] = get_form_dropdown('projects', 'id', 'proj_name', 'All Projects'); And in a different project which uses a dropdown: Code: $data['project_menu'] = get_form_dropdown('users', 'userid', 'username', 'Select a User'); Working Drop Down List Code Review Help - El Forum - 10-21-2010 [eluser]TerryT[/eluser] Appreciate the insight. Although I was proud of figuring it out, your code is far cleaner. Terry |