Reputation: 13
I have two tables :
users: id, name, email
user_experience: id_exp, user_id, exp_date
There are multiple users and each user has multiple experiences.
E.g.
users:
1, david, [email protected]
is linked to three experiences
user_experience:
1, 1, 1/1/2012
2, 1, 2,2,2012
3, 1, 3,3,2012
How can I retrieve the data so I can display it in a view like so:
Name / email / Date of experience
david, [email protected] 1/1/2012
2/2/2012
3/3/2012
joe, [email protected] 1/1/2012
2/2/2012
3/3/2012
My code currently:
Controller
$data['nurses'] = $this->users_model->get_nurse();
Model
public function get_nurse() {
$this->db->select('name,email,date');
$this->db->from('users');
$this->db->join('user_experience', 'users.id = user_experience.user_id');
$query = $this->db->get();
return $query->result_array();
}
View
<?php foreach ($rows as $row): ?>
echo $row['first_name'];
echo $row['email'];
echo $row['exp_date'];
This however displays a new line for each user and exp combo. I want the user displayed only once. Thanks!
Upvotes: 0
Views: 3891
Reputation: 363
Your get nurse function shouldn't include experiences as they are separate entities. Have get nurse return just the nurses info, and then for each nurse get_experiences() and display those. So your controller would look something like this.
$data['nurses'] = $this->users_model->get_nurse();
foreach($data['nurses'] as $nurseObj) {
$expM = new ExperienceModel($nurseObj->id);
// Display/pass/generate nurse info
$nurse_experiences = $expM->getExperiences();
foreach($nurse_experiences as $ne) {
// experience info
}
}
Upvotes: 1