本文介绍了如何检查同一ID在另一列中是否有多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有团队成员表和团队表。 在team member table中,有3列是teamid,staff_id和stafftype(领导者或技术员)。一队只有一名领导者(staff_type列值)。因此,当我向团队成员表中插入数据时,我需要检查同一团队中是否有任何领导者。
i have team member table and team table. In team member table have , three columns are there teamid, staff_id and stafftype(leader or technician). Only one leader (staff_type column value) will comes in one team. So when i insert data to team member table i need to check whether any leader is there in same team.
如何显示一条错误消息,已经有领导者了团队?
How to show an error message that "already have leader in this team"?
team_id staff_id stafftype 1 2 leader 2 1 other 3 8 other 1 5 Technician 2 3 Other 1 4 Leader //This should not come. becoz already teamid-1 have Leader When trying to save from frond end, need to show error message ie;"already have leader in this team"模式
public function addteam_memb($datas){ $this->db->select('*'); $this->db->from('team_members'); $querySS = $this->db->get()->result(); if(array_search('1', array_column($querySS, 'team_id')) !== false) { return 1; } else { $insert_id = 0; if ( ! empty($datas)) { $this->db->insert('team_members', $datas); $insert_id = $this->db->insert_id(); } } }控制器
public function editteammember(){ $getaddstafftype = $this->input->post( 'getaddstafftype' ); $getaddstaffname = $this->input->post( 'getaddstaffname' ); $getteamid = $this->input->post('getteamid'); $getstatus = $this->input->post('getstatus'); //if ( ! empty($getaddstaffname) ) if ( ! empty($getaddstaffname) && ! empty($getaddstafftype) ) { foreach ($getaddstaffname as $key => $value ) { $data['Staff_id'] = $value; $data['Staff_type'] = $getaddstafftype[$key]; $data['team_id'] = $getteamid[$key]; $data['status'] = "active"; $value = $this->mastertable_model->addteam_memb($data); } if($value == 1) { echo "Already have leader in this team"; } else { //$this->load->view('team_memb_creation'); } }}
推荐答案模型:
public function add_team_memb(){ $this->db->select('NEXT VALUE FOR team_seq as team_id'); $query = $this->db->get(); foreach ($query->result_array() as $row) { $team_id = $row['team_id']; } $this->db->select('*'); $this->db->from('team_member'); $querySS = $this->db->get()->result(); if(array_search('1', array_column($querySS, 'team_id')) !== false) { return 1; } else { $datateam_memb = array( 'team_id' => $team_id, 'staff_id' => 3, 'stafftype' => 'leader', ); $insert_id = 0; if ($this->db->insert("team_member", $datateam_memb)) { $insert_id = $this->db->insert_id(); } return $team_id; } }控制器::
public function addteammember() { $value = $this->Admin_model->add_team_memb(); if($value == 1) { ///pass message in view leader is already in team } else { $this->load->view('team_memb_creation'); } }更多推荐
如何检查同一ID在另一列中是否有多个值?
发布评论