0

I am able to set the viewVars for a single record and mail it successfully. A problem occurs when I want to send an email containing more than one record. I find the correct records and I am able to pass them to my mail function. The problem comes in that when I debug the array passed to the mail template, I get a

Notice (8): Undefined variable: vars [APP\View\Emails\html\latest_projects.ctp, line 1]

However, just below the error, it does show me the information I want:

(int) 0 => array(
    'Project' => array(
        'id' => '809',
        'created' => '2014-04-23',
        'project_number' => 'SPN00000809',
    )
),
(int) 1 => array(
    'Project' => array(
        'id' => '810',
        'created' => '2014-04-23',
        'project_number' => 'SPN00000810',
    )
)

*Certain fields omitted for brevity.

How do I loop through this array in the email template? I have tried the standard foreach loop as you would in the view, but then I get the undefined variable supplied foreach problem. Any advice or suggestions?

Cody
  • 112
  • 9
  • With this little information, its not possible to understand your problem... Update question with Controller and View... – Fazal Rasel Apr 23 '14 at 16:15
  • first try to print your array out to see if it is correct. and make your html. then pass the array to email template as @Rajeev Ranjan said – Fury Apr 23 '14 at 17:00

3 Answers3

1
//Pass your variable
$Email->viewVars(array('projects' => $projects));

//In your email body or template
<ul>
    <?php foreach ($projects as $project) { ?>
        <li><?php echo $project['Project']['project_number']; ?></li>
    <?php } ?>
</ul>
Fury
  • 4,643
  • 5
  • 50
  • 80
  • The problem was that the array passed, $dataForView, which is generated by cake, was a combination(?) array - meaning that some keys were associative such as $dataForView['content'] => '', while the other keys were (int)0 => array(); I found that if I unset the associative keys, I was able to loop through the array as normal. So you are right, but I had to normalize the array first. – Cody Apr 24 '14 at 00:14
  • so you get the answer?.. and then whats happened? – Fury Apr 24 '14 at 12:12
0

like said in documentation :-

$Email->viewVars(array('value' => 12345));

you will be able to use it as $value in mail template.

just like that set your array to 'value' you will be able to use $value as array.

Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
  • I understand that, as said in the question, I am able to get the vars for a single record, so if I have 1 record and I set he viewVars, it works. However, when I try to loop through the vars array to echo the value of multiple records I get an error. This is the contents of the mail I receive: http://pastebin.com/bvF7FFLL – Cody Apr 23 '14 at 13:21
  • I can access the correct values by using $dataForView['0']['Project']['fieldName'], but the trouble is looping through the keys of $dataForView. I have tried foreach($dataForView as $key=>$val), but I cannot seem to get it to work. – Cody Apr 23 '14 at 13:29
  • @Cody I can't understand after code seen on pastebin,if i have array and not able to manipulate with it. – Rajeev Ranjan Apr 24 '14 at 05:00
0

The problem was that the array passed, $dataForView, which is generated by cake, was a combination(?) array - meaning that some keys were associative such as $dataForView['content'] => '', while the other keys were (int)0 => array(); The array received looked as such:

array(
  content => '',
  (int) 0 => array(
    Project => array(
      fieldName1 => value,
      fieldName2 => value
    )
  ),
  (int) 1 => array(
    Project => array(
      fieldName1 => value,
      fieldName2 => value
    )
  )
)

I found that if I unset the associative keys (content) I am able to loop through the normalized array as per usual. I did it in this way, it might not be the best way, but it works.

//remove associative key
unset($dataForView['content']);

//loop through array and output values
foreach($dataForView as $key=>$val):
echo $val['Project']['id']; //echo other info as well
endforeach;
debug($dataForView); 

Thank you all for helping.

Cody
  • 112
  • 9