The worst code I've written :( |
[eluser]iainco[/eluser]
Problem: Users select sports using HTML chechboxes and their selections are saved in a database. What is the best way of displaying checkboxes already selected for those which have been saved as selected? Model Code: function fetchSports() View Code: <? foreach($sports as $sport): ?> Controller Code: function index() That's my first try, but it is terribly messy... I think my cold is preventing me from seeing a much more elegant solution! *cries*
[eluser]Colin Williams[/eluser]
The View code looks a lot like the way I've been doing it all along. I mean, if you really want to, you could do a loop that sets a value of either 'checked' or NULL to a string or array value for every checkbox, then just echo the respective var in the loop that prints out the checkbox. But, is it really necessary? Also, the Validation class has a set_checkbox() method: Code: <input type="checkbox" name="mycheck" value="1" <?php echo $this->validation->set_checkbox('mycheck', '1'); ?> />
[eluser]erik.brannstrom[/eluser]
Well, it's not that bad really. 'Cause I guess you've got it working? Anyway, of the top of my head, I would say use Active Records for your query. Looks cleaner and works across different database types. Code: $this->db->select('sportID')->where('userID',$this->session->userdata('ID'))->get('user_sport); The result() method returns an empty array if an empty result set is returned, so no need to check it using if. No need for $count, just write $userSports[] and it will automatically increment. But even better would be using result_array() and you've got your array. And maybe use the form_checkbox method in the form helper. You migh find it useful, you migh not. Check it out and see for yourself: http://ellislab.com/codeigniter/user-gui...elper.html That's it for me. Hope I could be of some help!
|
Welcome Guest, Not a member yet? Register Sign In |