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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化