dsbezji539113152 2019-07-31 22:32
浏览 48
已采纳

如何使用代码点火器PHP检查表中只有数据值?

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 member table looks like,

team_id   staff_id   stafftype
1          2        leader //see here already have leader for team_id 1
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"

MODAL

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();
        }
    }
}

CONTROLLER

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');        
        }
    } 
}
  • 写回答

1条回答 默认 最新

  • donglun7151 2019-07-31 23:21
    关注

    I'm not entirely sure what you're trying to do with the existing array_search, but I'm guessing that it's an initial attempt at getting the leader check working? If that's not the case let me know

    You're on the right general track, querying the table first to check for conflicts, just not quite the right execution.

    What you need to do is

    • If you're adding a leader check if that team already has a leader
    • If so, handle it somehow
    • Otherwise continue with the insert

    Try something like the below. The input should be an array with keys corresponding to database columns. EG:

    $datas = [
        'team_id' => 1,
        'staff_id' => 3,
        'stafftype' => 'leader'
    ]
    
    // Its a good idea to use constants for things like this
    const STAFFTYPE_LEADER = 'leader';
    
    
    /**
     * Add a team member to the database 
     *
     * Input array must be an array representing a team member to be added,
     * with key names that match database column names.
     *
     * @param array $datas 
     * @throws Exception If adding a leader when one already exists
     */
    public function addteam_memb($datas) {
        // Lower case conversion just for consistency. 
        $staffType = strtolower($datas['Staff_type']);
    
        // Only run the query if we're trying to add a team lead
        // Otherwise there's no need to worry
        if ($staffType === self::STAFFTYPE_LEADER) {
            $this->db->select('*');
            $this->db->from('team_members');
            $this->db->where('team_id', $datas['team_id'])
            $this->db->where('stafftype', self::STAFFTYPE_LEADER)
    
            if ($this->db->count_all_results() >= 1) {
                // Team leader already exists, handle however you would like
                // I would typically recommend an exception, but thats up to you!
                throw new Exception('Team leader exists');
            }
    
            // Reset query before moving on to the insert
            $this->db->reset_query();
        }
    
        // Either we're not adding a leader, or there is no current leader
        // so go ahead with the insert
        $insert_id = 0;
        if ( ! empty($datas)) {
            $this->db->insert('team_members', $datas);
            $insert_id = $this->db->insert_id();
        }
    
    }
    

    It's worth noting that your code so far seems to have a bit of a mismatch between editing and adding that hasn't been addressed here. This function assumes you're only adding new members, not editing existing ones. you'll need something more for that case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘