0

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!

1
  • "i thought comma delimited...". Let me stop you right there Commented Nov 23, 2011 at 1:38

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Not sure how to match that with id from DB to each checkbox.
When you loop through all the available shows (hopefully you have a SHOW_ID of some sort), you can see if the SHOW_ID matches any of the shows the user has in his favorites. If $favorites = array(10,15,20), then a value of 'checked' will echo (<?= is the same as <? echo) in that line and your checkbox will appear as CHECKED.
Yes I have show id. Thanks I think I get it. I will try it soon. While im here how would I approach updating favourites? Would I have to update every row that matched the user id. If we are using the array to check then why not have a comma delimited format in one field?
It will be much easier in the long run.. if a user has 50 favorites, it will be much simpler to find a certain fav in the database than have to traverse arrays, etc. Having sql do the logic will be faster than php in the future.
1

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

Clicky

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.