简体   繁体   中英

Codeigniter using multiple arrays in a function?

function SendAbsentSMS($date) {
    $student = $this->CI->stuattendence_model->student_daily_attendance($date);
    $stu_setting = $this->CI->setting_model->getSkoolInfo();
    $MSG = "Dear Parent your ward " . $student['firstname'] . " " . $student['lastname'] . " " . $student['class'] . " " . $student['section'] . " is absent from his class on " . $student['date'] . ". 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); 

}

This is my function which sends SMS to each individual user, when there is single row output i am able to send an SMS (below is the generated array)

Array ( [id] => 25 [firstname] => Abhishek [lastname] => Mishra [email] => abhishekmishrashs@gmail.com [guardian_phone] => 8987190938 [roll_no] => 1 [class] => Class 1 [section] => Section A [date] => 2017-04-19 )

I am looking for a way to use multiple arrays output like below in the above function:

Array ( [0] => Array ( [id] => 25 [firstname] => Abhishek [lastname] => Mishra [email] => abhishekmishrashs@gmail.com [guardian_phone] => 8987190938 [roll_no] => 1 [class] => Class 1 [section] => Section A [date] => 2017-04-19 ) [1] => Array ( [id] => 1 [firstname] => ABHISHEK [lastname] => MISHRA [email] => abhishekmishrashs@gmail.com [guardian_phone] => 8987190938 [roll_no] => 1 [class] => Class 1 [section] => Section A [date] => 2017-04-19 ) )

and no matter how many arrays are there the function should send SMS individually to each users, i am unable to figure it out i can do it if there is single row but have no idea about multiple arrays.

You can try with this code.

$student = $this->CI->stuattendence_model->student_daily_attendance($date);
$stu_setting = $this->CI->setting_model->getSkoolInfo();

    foreach($student as $val){
        $date = date();
        SendAbsentSMS($date,$val);
    }


    function SendAbsentSMS($date,$student) {
         $MSG = "Dear Parent your ward " . $student['firstname'] . " " . $student['lastname'] . " " . $student['class'] . " " . $student['section'] . " is absent from his class on " . $student['date'] . ". 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); 

    }

I assume $student = $this->CI->stuattendence_model->student_daily_attendance($date); returns only 1 row.

Update your model to return multiple rows as an array.

Then use this code.

function SendAbsentSMS($date) {
    $students = $this->CI->stuattendence_model->student_daily_attendance($date);
    $stu_setting = $this->CI->setting_model->getSkoolInfo();
   foreach($students as $student)
   {
    $MSG = "Dear Parent your ward " . $student['firstname'] . " " . $student['lastname'] . " " . $student['class'] . " " . $student['section'] . " is absent from his class on " . $student['date'] . ". 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); 
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM