0
votes

Guys on my database i have three tables containing data as shown on a link below. On tables class and subjects, ssid and csid are foreign key from members.

I joining tables using left join as shown in model code shown below. when i echo first_name, surname and class_name for John claudius it appears three times while for Alex massawe it appear only once. but if i add information for Alex massawe result will displayed out as many times as information for Alex massawe that are within subjects. I need your help so that information given out will not be repeated if add information on tables subjects for a person whose name already in tables members.

Tables within database

members

sid       first_name     surname
a001     alex               massawe
a002     John             claudius

class

id        csid     class_name
01      a001     baby_class
02      a002     Class_one

subjects

id        ssid     subject_name
01      a002     Mathematics
02      a002     literature
03      a002     Communication skills
04       a001     Mathematics

result table

sid      first_name      surname      class_name
a002      John           claudius        Class_one
a002      John            claudius       Class_one
a002      John           claudius        Class_one
a001      alex            massawe      baby_class

codes



Models:


function get_particular($sid){
$this->db->select('*');
$this->db->from('members m');
$this->db->join('subjects s', 'm.sid=s.ssid', 'left');
$this->db->join('class c', 'm.sid=c.csid', 'left');
$this->db->where('m.sid', $sid);
$query = $this->db->get();
return $query->result_array();
}

Controller:

function particular($sid){
$sid=$this->uri->segment(3);
$this->load->model('names');

$this->data["names"]=$this->names_rank->get_particular($sid);
$this->load->view("view/details", $this->data);
}

view:

foreach($names as $name) {

echo $name['sid'].' '. $name['first_name'].' '. $name['surname'].' '.$name['class_name'];
}

1
But you did not pass model in your view - 4Jean
It is because you have NOT assigned any subject to this sid. And there is no link between the class and subjects too. When you have rows added for Alex Massawe in subjects table, you would see the results properly. – - Rajesh
Why are you using LEFT join .... pls consider removing it. It causes the duplicates. - Petko Kostov
okay, how can i join tables? - Claudius Massery

1 Answers

0
votes

Try removing the foreach loop just echo the result

echo $names['sid'].' '. $names['first_name'].' '. $names['surname'].' '.$names['class_name'];

If you need to loop, try loop foreach member then echo $names