[英]join table technique using codeigniter
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 .在表class 和 subjects上, ssid 和 csid是members的外键。
I joining tables using left join as shown in model code shown below.我使用左连接连接表,如下面的 model 代码所示。 when i echo first_name, surname and class_name for John claudius
it appears three times while for Alex massawe
it appear only once.当我为John claudius
回显first_name、surname 和 class_name时,它出现了三次,而对于Alex massawe
,它只出现了一次。 but if i add information for Alex massawe
result will displayed out as many times as information for Alex massawe
that are within subjects.但如果我添加Alex massawe
的信息,结果将显示与主题内的Alex massawe
信息一样多的次数。 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.我需要您的帮助,以便在为名称已在表成员中的人添加有关表主题的信息时,不会重复给出的信息。
members成员
sid first_name surname sid 名字 姓氏
a001 alex massawe a001 亚历克斯马萨维
a002 John claudius a002 约翰·克劳狄斯
class class
id csid class_name id csid 类名
01 a001 baby_class 01 a001 婴儿班
02 a002 Class_one 02 a002 Class_one
subjects科目
id ssid subject_name id ssid 主题名称
01 a002 Mathematics 01 a002 数学
02 a002 literature 02 a002文学
03 a002 Communication skills 03 a002 沟通技巧
Models:楷模:
function get_particular($sid){ function get_particular($sid){
$this->db->select('*'); $this->db->select('*');
$this->db->from('members m'); $this->db->from('members m');
$this->db->join('subjects s', 'm.sid=s.ssid', 'left'); $this->db->join('subjects s', 'm.sid=s.ssid', 'left');
$this->db->join('class c', 'm.sid=c.csid', 'left'); $this->db->join('class c', 'm.sid=c.csid', 'left');
$this->db->where('m.sid', $sid); $this->db->where('m.sid', $sid);
$query = $this->db->get(); $query = $this->db->get();
return $query->result_array();返回 $query->result_array();
} }
Controller: Controller:
function particular($sid){ function 特别($sid){
$sid=$this->uri->segment(3); $sid=$this->uri->段(3);
$this->load->model('names'); $this->load->model('names');$this->data["names"]=$this->names_rank->get_particular($sid); $this->data["names"]=$this->names_rank->get_particular($sid);
$this->load->view("view/details", $this->data); $this->load->view("view/details", $this->data);
} }
view:看法:
foreach($names as $name) { foreach($names as $name) {
echo $name['sid'].' echo $name['sid'].' '. '. $name['first_name'].' $name['first_name'].' '. '. $name['surname'].' $name['姓氏'].' '.$name['class_name']; '.$name['class_name'];
} }
What if you remove the loop - Changing your view 如果删除循环该怎么办-更改视图
From 从
foreach($names as $name) {
echo $name['sid'].' '. $name['first_name'].' '. $name['surname'].' '.$name['class_name'];
}
To 至
echo $name['sid'].' '. $name['first_name'].' '. $name['surname'].' '.$name['class_name'];
or if you need the loop, then it should loop for each member and echo name. 或者,如果需要循环,则应该为每个成员和回显名称循环。 ie foreach $member echo $names 即foreach $ member echo $ names
Details from tables members (first_name and surname) and class (class_name) are shown in view.表成员(名字和姓氏)和class (类名)的详细信息显示在视图中。 So remove the left join to subjects table from query.因此,从查询中删除到subjects表的左连接。 this join is the reason for repeated records if there are more than one record for same member如果同一成员有多个记录,则此连接是重复记录的原因
$this->db->select('*');
$this->db->from('members m');
$this->db->join('class c', 'm.sid=c.csid', 'left');
$this->db->where('m.sid', $sid);
$query = $this->db->get();
return $query->result_array();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.