从数组中获取数据以用于电子邮件

时间:2012-09-18 14:22:23

标签: php cakephp

我在表单上有一系列复选框的示例:

<?php

echo $this->Form->input(
    'reason', array(
        'label'=>false,
        'type' => 'select', 
        'multiple' => 'checkbox',
        'options' => array(
            'Contact me' => 'Contact me',
            'Brochure request' => 'Brochure request',
            'Demonstration request' => 'Demonstration request',
            'Training' => 'Training',
            'Support' => 'Support',
            'Other' => 'Other'
        )
    ));

?>

然后将其设置为控制器中的变量,如:

$this->set('reason', $this->data['Contact']['reason']);

然后在电子邮件模板中我尝试过这样做:

<?php var_dump( implode( ', ', $reason ) ); ?>

结果如下:string(28) "Contact me, Brochure request"如何删除这些引号和字符串(28)前缀?

最好的方法是什么?感谢

2 个答案:

答案 0 :(得分:2)

使用string helper/text helper。它有一个内置的toList函数。

// controller
App::uses('String', 'Utility');
echo String::toList($this->request->data['Contact']['reason']);

// or
// controller
$this->set('reason', $this->data['Contact']['reason']);
// view
echo $this->Text->toList($reason);
// outputs 1, 2, 3 and 4

假设reason是格式为的数组:

array(
    (int) 0 => '1',
    (int) 1 => '2',
    (int) 2 => '3',
    (int) 3 => '4'
)

或只做一个简单的foreach($reason as $k => $v) { echo $v; }

答案 1 :(得分:0)

尝试在视图中访问这样的原因:

控制器

$this->set('contact', $this->request->data);

查看

$contact['Contact']['reason'];