简体   繁体   English

Foreach 循环未在 codeignter 中显示 0 元素

[英]Foreach loop not displaying the 0 element in codeignter

Goodday,再会,

I need some assistant with my foreach loop.我的 foreach 循环需要一些助手。

I am uploading multiple images/files to my server and then i am trying to send the images as attachments in an email.我正在将多个图像/文件上传到我的服务器,然后我试图将图像作为附件发送到 email 中。

I get the data to the email function and can git it in the loop but for some reason i only see the last item in the array and con see why tjis is happing.我将数据获取到 email function 并且 git 它可以在循环中,但由于某种原因,我只看到数组中的最后一项并明白为什么。

Please see attached the output and code of the function.请参阅随附的 output 和 function 的代码。

function forwardCallEmail($emaildetails)
{
        $data = $emaildetails;
        pre($data['files']);
        echo('<br/>');
        //die;
        $CI = setProtocol();        
        $CI->email->from('');
        $CI->email->subject("");
        $CI->email->message($CI->load->view('calls/forwardCallEmail', $data, TRUE));
        $path = base_url() . "uploads/Calls/";
        foreach ((array) $data['files'] as $files){
            echo($files);
            echo('<br/>');
            $images = explode(',', $files);
            var_dump($images);
            foreach($images as $files);
            echo('<br/>');
            echo $files;
            die;
            $CI->email->attach($path . $files);
            pre($CI);
            die;
        }
        $CI->email->to($data['email']);
        $status = $CI->email->send();

        return $status;

块引用

You can try something like this你可以试试这样的

Replace this part更换这部分

foreach ((array) $data['files'] as $files){
        echo($files);
        echo('<br/>');
        $images = explode(',', $files);
        var_dump($images);
        foreach($images as $files);
        echo('<br/>');
        echo $files;
        die;
        $CI->email->attach($path . $files);
        pre($CI);
        die;
    }

To this one对这个

foreach ((array) $data['files'] as $files){
        echo($files);
        echo('<br/>');
        $images = explode(',', $files);
        var_dump($images);
        foreach($images as $files) {
            echo('<br/>');
            echo $files;
            $CI->email->attach($path . $files);
            pre($CI);
        }
    }

Anyway this example to explain a logic of foreach无论如何这个例子来解释foreach的逻辑

Ok, just as example好的,作为示例

$data = [
    'files' => [
        "image1, image2, image3",
        "image4, image5, image6",
    ]
];

foreach ($data['files'] as $fileKey => $file){

    echo($file);
    $images = explode(',', $file);

    foreach($images as $imageKey => $imageValue) {

        $out[$fileKey][$imageKey] = $imageValue;

    }
}

print_r($out);

And result will be结果将是

(
   [0] => Array
       (
           [0] => image1
           [1] =>  image2
           [2] =>  image3
       )

   [1] => Array
       (
           [0] => image4
           [1] =>  image5
           [2] =>  image6
       )

)

I got it to work, not sure if it is the right way it is working.我让它工作,不确定它是否是正确的工作方式。

Email sending code Email 发码

CI = setProtocol();        
    
    $CI->email->from('helpdesk@ziec.co.za', 'HTCC Helpdesk');
    $CI->email->subject("HTCC Call Assistants");
    $CI->email->message($CI->load->view('calls/forwardCallEmail', $data, TRUE));
    $path = 'c:/xampp/htdocs/Helpdeskv2.1/uploads/Calls/';


    $images = explode(',', $data['files']);

        foreach($images as $file){
            $path = 'c:/xampp/htdocs/Helpdeskv2.1/uploads/Calls/'. $file;
            $CI->email->attach($path);
        }

    $CI->email->to($data['email']);
    $status = $CI->email->send();

    return $status;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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