Abhishek Mishra
Abhishek Mishra

Reputation: 25

Add multiple if statements in a single php CI fucntion

Nerd question, apologies, Below function works when there is only single if statement but when i add multiple if statements, it won't work, as far i know about php currently i used elseif statement but also id didn't worked

function NewNoticeMSG($insert_id) {
    $notice = $this->CI->notification_model->get_notice($insert_id);
    $stu_setting = $this->CI->setting_model->getSkoolInfo();

    if ($notice['visible_student'] == 'Yes') {
        $students = $this->CI->student_model->get_all_students();

        foreach($students as $student) {
            $MSG = "Dear Student, NOTICE " . $notice['date'] . ": " . $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
            $content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
            '&message=' . rawurlencode($MSG) .
            '&senderId=' . rawurlencode($this->senderId) .
            '&routeId=' . rawurlencode($this->routeId) .
            '&mobileNos=' . rawurlencode($student['mobileno']) . 
            '&smsContentType=' . rawurlencode($this->smsContentType);
            $smsglobal_response = $this->sendSMS($content); 
        }
    }

    if ($notice['visible_parent'] == 'Yes') {
        $students = $this->CI->student_model->get_all_students();

        foreach($students as $student) {
            $MSG = "Dear Parent, NOTICE " . $notice['date'] . ": " . 
            $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
            $content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
            '&message=' . rawurlencode($MSG) .
            '&senderId=' . rawurlencode($this->senderId) .
            '&routeId=' . rawurlencode($this->routeId) .
            '&mobileNos=' . rawurlencode($student['guardian_phone']) . 
            '&smsContentType=' . rawurlencode($this->smsContentType);
            $smsglobal_response = $this->sendSMS($content); 
        }
    } 

    if ($notice['visible_teacher'] == 'Yes') {
        $teachers = $this->CI->teacher_model->get_all_teachers();

        foreach($teachers as $teacher) {
            $MSG = "Dear Teacher, NOTICE " . $notice['date'] . ": " . $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
            $content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
            '&message=' . rawurlencode($MSG) .
            '&senderId=' . rawurlencode($this->senderId) .
            '&routeId=' . rawurlencode($this->routeId) .
            '&mobileNos=' . rawurlencode($teacher['phone']) . 
            '&smsContentType=' . rawurlencode($this->smsContentType);
            $smsglobal_response = $this->sendSMS($content); 
        }
    } 
}

how should i do that??

Upvotes: 0

Views: 103

Answers (2)

ismael ansari
ismael ansari

Reputation: 486

Firstly you need to on error reporting then you should print array what you get then use isset and !empty in if statement,

For example:-

if (isset($notice['visible_student']) && 
!empty($notice['visible_student']) && $notice['visible_student'] == 
'Yes') 

instead of this, "if ($notice['visible_student'] == 'Yes')"

Hope it will help you!

Upvotes: 2

Devdutt Sharma
Devdutt Sharma

Reputation: 379

Please Try with the :

function NewNoticeMSG($insert_id) {

    $notice = $this->CI->notification_model->get_notice($insert_id);
    $stu_setting = $this->CI->setting_model->getSkoolInfo();

    if (isset($notice['visible_student']) && $notice['visible_student'] == 'Yes') {
        $students = $this->CI->student_model->get_all_students();
        foreach($students as $student) {
            $MSG = "Dear Student, NOTICE " . $notice['date'] . ": " . $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
            $content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
                    '&message=' . rawurlencode($MSG) .
                    '&senderId=' . rawurlencode($this->senderId) .
                    '&routeId=' . rawurlencode($this->routeId) .
                    '&mobileNos=' . rawurlencode($student['mobileno']) . 
                    '&smsContentType=' . rawurlencode($this->smsContentType);
            $smsglobal_response = $this->sendSMS($content); 

        }
    }
    if (isset($notice['visible_parent']) && $notice['visible_parent'] == 'Yes') {
        $students = $this->CI->student_model->get_all_students();
        foreach($students as $student) {
            $MSG = "Dear Parent, NOTICE " . $notice['date'] . ": " . 
            $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
            $content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
                    '&message=' . rawurlencode($MSG) .
                    '&senderId=' . rawurlencode($this->senderId) .
                    '&routeId=' . rawurlencode($this->routeId) .
                    '&mobileNos=' . rawurlencode($student['guardian_phone']) . 
                    '&smsContentType=' . rawurlencode($this->smsContentType);
            $smsglobal_response = $this->sendSMS($content); 

        }
    } 
    if (isset($notice['visible_teacher']) && $notice['visible_teacher'] == 'Yes') {
         $teachers = $this->CI->teacher_model->get_all_teachers();
        foreach($teachers as $teacher) {
            $MSG = "Dear Teacher, NOTICE " . $notice['date'] . ": " . $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
            $content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
                    '&message=' . rawurlencode($MSG) .
                    '&senderId=' . rawurlencode($this->senderId) .
                    '&routeId=' . rawurlencode($this->routeId) .
                    '&mobileNos=' . rawurlencode($teacher['phone']) . 
                    '&smsContentType=' . rawurlencode($this->smsContentType);
            $smsglobal_response = $this->sendSMS($content); 

        }
    } 
}

Upvotes: 1

Related Questions