[eluser]osfan[/eluser]
Hi Ravi,
It is usually better to structure your data in normalised tables within your database, in this case you'd end up with 3 tables; users, subjects, and a linking table user_subjects which would contain user_subjects_id, user_id, subject_id only. There could be many rows with the same user_id, as 1 user could have many subjects. You'd then use a SQL JOIN to retrieve your data in 1 statement. I will leave this as an exercise for you.
If you do end up with csv values in a column, 1 way to approach it is to write code similar to this (off the top of my head).
Code:
// Load your models
$this->load->model('users_model','',TRUE);
$this->load->model('subjects_model','',TRUE);
// Call users_model function to retrieve users
// E.g. SELECT user_id, country_id, subject_id FROM users
$user_entries = $this->users_model->get_users();
// If we have user entries
if ($user_entries) {
// Loop through the user entries
foreach ($user_entries as $user_row) {
$user_id = $user_row->user_id
$country_id = $user_row->country_id;
$subject_id_column = $user_row->subject_id;
$related_subjects = explode(",",$subject_id_column);
// If there is at least 1 entry
if ($related_subjects[0]) {
// Loop through the subject entries
foreach ($related_subjects as $subject_id) {
// Call subjects model to retrieve individual subjects
// E.g. SELECT subject_id, subject_name, subject_title FROM subjects WHERE subject_id = ?
$subject_entries = $this->subjects_model->get_subject($subject_id);
// If you've got subject entries for the subject id in question
if ($subject_entries) {
// Loop through the subject data
foreach ($subject_entries as $subject_row) {
$subject_id = $subject_row->subject_id;
$subject_name = $subject_row->name;
$subject_title = $subject_row->title;
}
}
}
}
}
}
Hope this helps your thinking.