viperfx
viperfx

Reputation: 327

Checkbox set value from DB

So the scenario is this: I currently have a DB with TV Shows with RSS Feeds. The feeds (TV Shows) table has ID, name, network, feed, active. I have setup a field in the accounts table called favourites, i thought comma delimited ID's of the TV Shows might be a good idea, but im not sure anymore.

I am currently looking to implement a controller in CI that will look at the DB in the favourites field, for eg if it was filled with 104,149,150

How can I get those ID's to transfer into the view so I can set the values to the respective Show's?

    <?php foreach ($shows as $show) { ?>

             <div class="grid_1">
              <?  $data = array(
    'name'        => $show['name'],
    'id'          => $show['id'],
    'value'       => $show['name'],
    'checked'     => FALSE,
    );

echo form_checkbox($data); ?>

            </div>

            <div class="item"><?=form_label($show['name']);?></div>

                       <?php } ?>

The view currently outputs the TV shows in the database with a check box next to each show, what is the best way for me to set user preferences for favourites? So that I can set the values of each checkbox according to the user preference

I hope I have explained what I am trying to do well,

Thanks!

Upvotes: 0

Views: 951

Answers (2)

Xopa
Xopa

Reputation: 131

Mike is right, here is an example using CI :)

Clicky

Upvotes: 1

Mike
Mike

Reputation: 783

You can load a view and send variables/parameters to it as such:

$params['data'] = array(1 => 'hello');
$this->load->view('path/view.php', $params);

EDIT: To save favorites, I suggest creating a new table ('favorites') that contains a favorite_id, user_id, show_id .. perhaps a date added, maybe some other things.

In the Shows page, where the checkboxes reside, you could do the following:

<input type="checkbox" <?=(in_array(SHOW_ID, $favorites) ? 'checked' : null);?> />

This means you must send over an array called $favorites which would look something like $params['favorites'] in your controller.

The query to get the favorites would look something like this:

$q = $this->db->query('SELECT show_id FROM favorites WHERE user_id=?', array($user_id));
$params['favorites'] = $q->result_array();

The array($user_id) following the query is called query binding, used for security.

Upvotes: 1

Related Questions