[英]codeigniter join table technique
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和 subject上, ssid和csid是成员的外键。
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. 当我为
John claudius
回响first_name, John claudius
和class_name时 ,它会出现3次,而对于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 first_name姓
a001 alex massawe a001亚历克斯·马萨维
a002 John claudius a002约翰·克劳迪乌斯
class 类
id csid class_name id csid class_name
01 a001 baby_class 01 a001 baby_class
02 a002 Class_one 02 a002 Class_one
subjects 科目
id ssid subject_name id ssid subject_name
01 a002 Mathematics 01 a002数学
02 a002 literature 02 a002文学
03 a002 Communication skills 03 a002沟通技巧
04 a001 Mathematics 04 a001数学
result table 结果表
sid first_name surname class_name sid first_name姓class_name
a002 John claudius Class_one a002约翰·克劳迪厄斯Class_one
a002 John claudius Class_one a002约翰·克劳迪厄斯Class_one
a002 John claudius Class_one a002约翰·克劳迪厄斯Class_one
a001 alex massawe baby_class a001 alex massawe baby_class
Models: 楷模:
function get_particular($sid){
函数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: 控制器:
function particular($sid){
特殊功能($ sid){
$sid=$this->uri->segment(3);$ sid = $ this-> uri-> segment(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 ['surname']。” '.$name['class_name'];
'。$ name ['class_name'];
}}
Try removing the foreach loop just echo the result 尝试删除foreach循环,仅回显结果
echo $names['sid'].' '. $names['first_name'].' '. $names['surname'].' '.$names['class_name'];
If you need to loop, try loop foreach member then echo $names 如果需要循环,请尝试循环foreach成员,然后回显$ names
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.