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?
//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>
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.
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.
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.