【问题标题】:How to put codeigniter active record into if elseif statement如何将codeigniter活动记录放入if elseif语句
【发布时间】:2014-03-27 06:57:36
【问题描述】:
$this->db->where('teacher_name_1 IS NOT NULL', NULL, FALSE);
$this->db->where('course_id', $course_id);
$this->db->update('tbl_course_choice', $teacher_name_2);

$this->db->where('teacher_name_2 IS NOT NULL', NULL, FALSE);
$this->db->where('course_id', $course_id);
$this->db->update('tbl_course_choice', $teacher_name_3);

$this->db->where('teacher_name_3 IS NOT NULL', NULL, FALSE);
$this->db->where('course_id', $course_id);
$this->db->update('tbl_course_choice', $teacher_name_4);

$this->db->where('teacher_name_4 IS NOT NULL', NULL, FALSE);
$this->db->where('course_id', $course_id);
$this->db->update('tbl_course_choice', $teacher_name_5);

$this->db->where('teacher_name_5 IS NOT NULL', NULL, FALSE);
$this->db->where('course_id', $course_id);
$this->db->update('tbl_course_choice', $teacher_name_6);

我怎么能把这段代码放在if elseif 语句中。我尝试了几种方法,但都失败了。

例子:

if ($this->db->where('teacher_name_1 IS NOT NULL', NULL, FALSE)) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_2);
}
elseif ($this->db->where('teacher_name_2 IS NOT NULL', NULL, FALSE)) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_3);
}
elseif ($this->db->where('teacher_name_3 IS NOT NULL', NULL, FALSE)) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_4);
}
elseif ($this->db->where('teacher_name_4 IS NOT NULL', NULL, FALSE)) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_5);
} 
elseif ($this->db->where('teacher_name_5 IS NOT NULL', NULL, FALSE)) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_6);
}
else {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_1);
}

怎么了?请帮我弄清楚。谢谢

编辑 经过一些修改后我的完整控制器,但仍然无法正常工作..

$user_name = $this->the_user->username;
$course_id = $this->input->post('course_id');
    $course_code = $this->input->post('course_code');
    $course_title = $this->input->post('course_title');
    $credit_hours = $this->input->post('credit_hours');
    $contact_hours = $this->input->post('contact_hours');
    $section = $this->input->post('section');
    $teacher = $this->input->post('teacher');

    $course_type = $this->input->post('course_typ');
    $pre_requisite = $this->input->post('pre_requisit');
    $year = $this->input->post('yea');
    $semester = $this->input->post('semeste');
    $teacher_name_1 = $user_name;
    $offering_year = $this->input->post('offering_year');
    $offering_session = $this->input->post('offering_session');

    $ql = $this->db->select('course_id')->from('tbl_course_choice')->where('course_id',$course_id)->get();
    if( $ql->num_rows() > 0 ) {
        $teacher_name_1 = array(
        'teacher_name_1' => 'mehdi hasan 1'
        );
        $teacher_name_2 = array(
        'teacher_name_2' => 'mehdi hasan 2'
        );
        $teacher_name_3 = array(
        'teacher_name_3' => 'mehdi hasan three'
        );
        $teacher_name_4 = array(
        'teacher_name_4' => 'mehdi hasan  four'
        );
        $teacher_name_5 = array(
        'teacher_name_5' => 'mehdi hasan  5'
        );
        $teacher_name_6 = array(
        'teacher_name_6' => 'mehdi hasan  6'
        );
    $this->db->where('teacher_name_1 != ', '');
$this->db->where('course_id', $course_id);
$query_2 = $this->db->get('tbl_course_choice');
$teacher_2_count = $query_2->num_rows();

$this->db->where('teacher_name_2 != ', '');
$this->db->where('course_id', $course_id);
$query_3 = $this->db->get('tbl_course_choice');
$teacher_3_count = $query_3->num_rows();

$this->db->where('teacher_name_3 != ', '');
$this->db->where('course_id', $course_id);
$query_4 = $this->db->get('tbl_course_choice');
$teacher_4_count = $query_4->num_rows();

$this->db->where('teacher_name_4 != ', '');
$this->db->where('course_id', $course_id);
$query_5 = $this->db->get('tbl_course_choice');
$teacher_5_count = $query_5->num_rows();

$this->db->where('teacher_name_5 != ', '');
$this->db->where('course_id', $course_id);
$query_6 = $this->db->get('tbl_course_choice');
$teacher_6_count = $query_6->num_rows();

if ($teacher_2_count > 0 ) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_2);
    } elseif ($teacher_3_count > 0 ) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_3);
    } elseif ($teacher_4_count > 0 ) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_4);
    } elseif ($teacher_5_count > 0 ) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_5);
    } elseif ($teacher_6_count > 0 ) {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_6);
    } else {
    $this->db->where('course_id', $course_id);
    $this->db->update('tbl_course_choice', $teacher_name_1);
    }
    } else {
        $a = array(
            'course_id' => $course_id,
            'course_code' => $course_code,
            'course_title' => $course_title,
            'course_type' => $course_type,
            'credit_hours' => $credit_hours,
            'contact_hours' => $contact_hours,
            'section' => $section,
            'teacher' => $teacher, 
            'pre_requisite' => $pre_requisite,
            'year' => $year,
            'semester' => $semester,
            'offering_year' => $offering_year,
            'offering_session' => $offering_session,
            'teacher_name_1' => $teacher_name_1
            );
        $this->db->insert('tbl_course_choice', $a);
    }

【问题讨论】:

    标签: php mysql codeigniter activerecord


    【解决方案1】:

    使用此代码

    $this->db->where('teacher_name_2 != ', '');
        $this->db->where('course_id', $course_id);
        $query_2 = $this->db->get('tbl_course_choice');
        $teacher_2_count = $query_2->num_rows();
    
    
        $this->db->where('teacher_name_3 != ', '');
        $this->db->where('course_id', $course_id);
        $query_3 = $this->db->get('tbl_course_choice');
        $teacher_3_count = $query_3->num_rows();
    
    
    
        $this->db->where('teacher_name_4 != ', '');
        $this->db->where('course_id', $course_id);
        $query_4 = $this->db->get('tbl_course_choice');
        $teacher_4_count = $query_4->num_rows();
    
    
    
        $this->db->where('teacher_name_5 != ', '');
        $this->db->where('course_id', $course_id);
        $query_5 = $this->db->get('tbl_course_choice');
        $teacher_5_count = $query_5->num_rows();
    
    
        $this->db->where('teacher_name_6 != ', '');
        $this->db->where('course_id', $course_id);
        $query_6 = $this->db->get('tbl_course_choice');
        $teacher_6_count = $query_6->num_rows();
    
    
    
    
    
    
     if ($teacher_2_count > 0 ) {
            $this->db->where('course_id', $course_id);
            $this->db->update('tbl_course_choice', $teacher_name_2);
            } elseif ($teacher_3_count > 0 ) {
            $this->db->where('course_id', $course_id);
            $this->db->update('tbl_course_choice', $teacher_name_3);
            } elseif ($teacher_4_count > 0 ) {
            $this->db->where('course_id', $course_id);
            $this->db->update('tbl_course_choice', $teacher_name_4);
            } elseif ($teacher_5_count > 0 ) {
            $this->db->where('course_id', $course_id);
            $this->db->update('tbl_course_choice', $teacher_name_5);
            } elseif ($teacher_6_count > 0 ) {
            $this->db->where('course_id', $course_id);
            $this->db->update('tbl_course_choice', $teacher_name_6);
            } else {
            $this->db->where('course_id', $course_id);
            $this->db->update('tbl_course_choice', $teacher_name_1);
            }
    }
    

    【讨论】:

    • 它不工作。每次它只是插入teacher_name_2 值但不能像teacher_name_2 有值一样工作,然后插入teacher_name_3 ...
    • 非常感谢您的合作。我已经通过你的代码检查了它仍然是同样的问题。我上传了我的完整控制器代码,请看上面。
    • 谢谢谢谢你。最后我在你的帮助下解决了。我非常感谢您的帮助。过去 5 天我都在解决这个问题……谢谢。我可以让你的电子邮件地址在未来直接打扰你吗!!!!
    • 假设我的 total_teacher = 3 并且在 Teacher_name_1、teacher_name-2、teacher_name_3 中具有值之后,我怎么能从索引页面中隐藏这个 course_code。我保存了你的身份证明。
    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多