Welcome Guest, Not a member yet? Register   Sign In
The worst code I've written :(
#1

[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()
    {
        $sports = array();
        $sport = $this->db->get('sport');
        
        if($sport->num_rows() > 0)
        {
            $sports = $sport->result();
        }
        
        return $sports;
    }
    
    function fetchUserSports()
    {
        $count = 0;
        $userSports = array();
        $user_sport = $this->db->query('SELECT `sportID` FROM `user_sport` WHERE `userID` = ' . $this->db->escape($this->session->userdata('ID')));
        
        foreach($user_sport->result() as $row)
        {
            $userSports[$count] = $row->sportID;
            $count++;
        }
        
        return $userSports;
    }

View
Code:
<? foreach($sports as $sport): ?>
&lt;?=$sport->title?&gt; &lt;input type="checkbox" name="sports[]" value="&lt;?=$sport-&gt;ID?&gt;" &lt;?=(in_array($sport->ID, $userSports)) ? 'CHECKED': '';?&gt; /><br />
&lt;? endforeach; ?&gt;

Controller
Code:
function index()
    {
        $this->load->view('Profile_View', array('sports' => $this->Profile_Model->fetchSports(), 'userSports' => $this->Profile_Model->fetchUserSports()));
    }

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*
#2

[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:
&lt;input type="checkbox" name="mycheck" value="1" &lt;?php echo $this-&gt;validation->set_checkbox('mycheck', '1'); ?&gt; />
#3

[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!
#4

[eluser]iainco[/eluser]
Thanks guys, got something I'm happier with now Smile




Theme © iAndrew 2016 - Forum software by © MyBB